2 * H.266/VVC helper functions for muxers
4 * Copyright (C) 2022, Thomas Siedel
6 * This file is part of FFmpeg.
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.
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.
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
23 #include "libavcodec/get_bits.h"
24 #include "libavcodec/put_bits.h"
25 #include "libavcodec/golomb.h"
26 #include "libavcodec/vvc.h"
27 #include "libavutil/avassert.h"
28 #include "libavutil/intreadwrite.h"
29 #include "libavutil/mem.h"
32 #include "avio_internal.h"
46 typedef struct VVCCNALUnitArray
{
47 uint8_t array_completeness
;
48 uint8_t NAL_unit_type
;
50 uint16_t *nal_unit_length
;
54 typedef struct VVCPTLRecord
{
55 uint8_t num_bytes_constraint_info
;
56 uint8_t general_profile_idc
;
57 uint8_t general_tier_flag
;
58 uint8_t general_level_idc
;
59 uint8_t ptl_frame_only_constraint_flag
;
60 uint8_t ptl_multilayer_enabled_flag
;
61 uint8_t general_constraint_info
[9];
62 uint8_t ptl_sublayer_level_present_flag
[VVC_MAX_SUBLAYERS
- 1];
63 uint8_t sublayer_level_idc
[VVC_MAX_SUBLAYERS
- 1];
64 uint8_t ptl_num_sub_profiles
;
65 uint32_t general_sub_profile_idc
[VVC_MAX_SUB_PROFILES
];
68 typedef struct VVCDecoderConfigurationRecord
{
69 uint8_t lengthSizeMinusOne
;
70 uint8_t ptl_present_flag
;
72 uint8_t num_sublayers
;
73 uint8_t constant_frame_rate
;
74 uint8_t chroma_format_idc
;
75 uint8_t bit_depth_minus8
;
77 uint16_t max_picture_width
;
78 uint16_t max_picture_height
;
79 uint16_t avg_frame_rate
;
80 uint8_t num_of_arrays
;
81 VVCCNALUnitArray arrays
[NB_ARRAYS
];
82 } VVCDecoderConfigurationRecord
;
84 static void vvcc_update_ptl(VVCDecoderConfigurationRecord
*vvcc
,
88 * The level indication general_level_idc must indicate a level of
89 * capability equal to or greater than the highest level indicated for the
90 * highest tier in all the parameter sets.
92 if (vvcc
->ptl
.general_tier_flag
< ptl
->general_tier_flag
)
93 vvcc
->ptl
.general_level_idc
= ptl
->general_level_idc
;
95 vvcc
->ptl
.general_level_idc
=
96 FFMAX(vvcc
->ptl
.general_level_idc
, ptl
->general_level_idc
);
99 * The tier indication general_tier_flag must indicate a tier equal to or
100 * greater than the highest tier indicated in all the parameter sets.
102 vvcc
->ptl
.general_tier_flag
=
103 FFMAX(vvcc
->ptl
.general_tier_flag
, ptl
->general_tier_flag
);
106 * The profile indication general_profile_idc must indicate a profile to
107 * which the stream associated with this configuration record conforms.
109 * If the sequence parameter sets are marked with different profiles, then
110 * the stream may need examination to determine which profile, if any, the
111 * entire stream conforms to. If the entire stream is not examined, or the
112 * examination reveals that there is no profile to which the entire stream
113 * conforms, then the entire stream must be split into two or more
114 * sub-streams with separate configuration records in which these rules can
117 * Note: set the profile to the highest value for the sake of simplicity.
119 vvcc
->ptl
.general_profile_idc
=
120 FFMAX(vvcc
->ptl
.general_profile_idc
, ptl
->general_profile_idc
);
123 * Each bit in flags may only be set if all
124 * the parameter sets set that bit.
126 vvcc
->ptl
.ptl_frame_only_constraint_flag
&=
127 ptl
->ptl_frame_only_constraint_flag
;
128 vvcc
->ptl
.ptl_multilayer_enabled_flag
&= ptl
->ptl_multilayer_enabled_flag
;
133 if (ptl
->num_bytes_constraint_info
) {
134 vvcc
->ptl
.num_bytes_constraint_info
= ptl
->num_bytes_constraint_info
;
135 memcpy(&vvcc
->ptl
.general_constraint_info
[0],
136 &ptl
->general_constraint_info
[0], ptl
->num_bytes_constraint_info
);
138 vvcc
->ptl
.num_bytes_constraint_info
= 1;
139 memset(&vvcc
->ptl
.general_constraint_info
[0], 0, sizeof(vvcc
->ptl
.general_constraint_info
));
143 * Each bit in flags may only be set if one of
144 * the parameter sets set that bit.
146 memset(vvcc
->ptl
.ptl_sublayer_level_present_flag
, 0,
147 sizeof(uint8_t) * vvcc
->num_sublayers
- 1);
148 memset(vvcc
->ptl
.sublayer_level_idc
, 0,
149 sizeof(uint8_t) * vvcc
->num_sublayers
- 1);
151 for (int i
= vvcc
->num_sublayers
- 2; i
>= 0; i
--) {
152 vvcc
->ptl
.ptl_sublayer_level_present_flag
[i
] |=
153 ptl
->ptl_sublayer_level_present_flag
[i
];
154 if (vvcc
->ptl
.ptl_sublayer_level_present_flag
[i
]) {
155 vvcc
->ptl
.sublayer_level_idc
[i
] =
156 FFMAX(vvcc
->ptl
.sublayer_level_idc
[i
],
157 ptl
->sublayer_level_idc
[i
]);
159 if (i
== vvcc
->num_sublayers
- 1) {
160 vvcc
->ptl
.sublayer_level_idc
[i
] = vvcc
->ptl
.general_level_idc
;
162 vvcc
->ptl
.sublayer_level_idc
[i
] =
163 vvcc
->ptl
.sublayer_level_idc
[i
+ 1];
168 vvcc
->ptl
.ptl_num_sub_profiles
=
169 FFMAX(vvcc
->ptl
.ptl_num_sub_profiles
, ptl
->ptl_num_sub_profiles
);
170 if (vvcc
->ptl
.ptl_num_sub_profiles
) {
171 for (int i
= 0; i
< vvcc
->ptl
.ptl_num_sub_profiles
; i
++) {
172 vvcc
->ptl
.general_sub_profile_idc
[i
] =
173 ptl
->general_sub_profile_idc
[i
];
178 static void vvcc_parse_ptl(GetBitContext
*gb
,
179 VVCDecoderConfigurationRecord
*vvcc
,
180 unsigned int profileTierPresentFlag
,
181 unsigned int max_sub_layers_minus1
)
183 VVCPTLRecord general_ptl
= { 0 };
185 if (profileTierPresentFlag
) {
186 general_ptl
.general_profile_idc
= get_bits(gb
, 7);
187 general_ptl
.general_tier_flag
= get_bits1(gb
);
189 general_ptl
.general_level_idc
= get_bits(gb
, 8);
191 general_ptl
.ptl_frame_only_constraint_flag
= get_bits1(gb
);
192 general_ptl
.ptl_multilayer_enabled_flag
= get_bits1(gb
);
193 if (profileTierPresentFlag
) { // parse constraint info
194 general_ptl
.num_bytes_constraint_info
= get_bits1(gb
); // gci_present_flag
195 if (general_ptl
.num_bytes_constraint_info
) {
196 int gci_num_reserved_bits
, j
;
197 for (j
= 0; j
< 8; j
++)
198 general_ptl
.general_constraint_info
[j
] = get_bits(gb
, 8);
199 general_ptl
.general_constraint_info
[j
++] = get_bits(gb
, 7);
201 gci_num_reserved_bits
= get_bits(gb
, 8);
202 general_ptl
.num_bytes_constraint_info
= j
;
203 skip_bits(gb
, gci_num_reserved_bits
);
208 for (int i
= max_sub_layers_minus1
- 1; i
>= 0; i
--)
209 general_ptl
.ptl_sublayer_level_present_flag
[i
] = get_bits1(gb
);
213 for (int i
= max_sub_layers_minus1
- 1; i
>= 0; i
--) {
214 if (general_ptl
.ptl_sublayer_level_present_flag
[i
])
215 general_ptl
.sublayer_level_idc
[i
] = get_bits(gb
, 8);
218 if (profileTierPresentFlag
) {
219 general_ptl
.ptl_num_sub_profiles
= get_bits(gb
, 8);
220 if (general_ptl
.ptl_num_sub_profiles
) {
221 for (int i
= 0; i
< general_ptl
.ptl_num_sub_profiles
; i
++)
222 general_ptl
.general_sub_profile_idc
[i
] = get_bits_long(gb
, 32);
226 vvcc_update_ptl(vvcc
, &general_ptl
);
229 static int vvcc_parse_vps(GetBitContext
*gb
,
230 VVCDecoderConfigurationRecord
*vvcc
)
232 unsigned int vps_max_layers_minus1
;
233 unsigned int vps_max_sublayers_minus1
;
234 unsigned int vps_default_ptl_dpb_hrd_max_tid_flag
;
235 unsigned int vps_all_independent_layers_flag
;
237 unsigned int vps_pt_present_flag
[VVC_MAX_PTLS
];
238 unsigned int vps_ptl_max_tid
[VVC_MAX_PTLS
];
239 unsigned int vps_num_ptls_minus1
= 0;
242 * vps_video_parameter_set_id u(4)
246 vps_max_layers_minus1
= get_bits(gb
, 6);
247 vps_max_sublayers_minus1
= get_bits(gb
, 3);
250 * numTemporalLayers greater than 1 indicates that the stream to which this
251 * configuration record applies is temporally scalable and the contained
252 * number of temporal layers (also referred to as temporal sub-layer or
253 * sub-layer in ISO/IEC 23008-2) is equal to numTemporalLayers. Value 1
254 * indicates that the stream is not temporally scalable. Value 0 indicates
255 * that it is unknown whether the stream is temporally scalable.
257 vvcc
->num_sublayers
= FFMAX(vvcc
->num_sublayers
,
258 vps_max_sublayers_minus1
+ 1);
260 if (vps_max_layers_minus1
> 0 && vps_max_sublayers_minus1
> 0)
261 vps_default_ptl_dpb_hrd_max_tid_flag
= get_bits1(gb
);
263 vps_default_ptl_dpb_hrd_max_tid_flag
= 0;
264 if (vps_max_layers_minus1
> 0)
265 vps_all_independent_layers_flag
= get_bits1(gb
);
267 vps_all_independent_layers_flag
= 1;
269 for (int i
= 0; i
<= vps_max_layers_minus1
; i
++) {
270 skip_bits(gb
, 6); //vps_layer_id[i]
271 if (i
> 0 && !vps_all_independent_layers_flag
) {
272 if (!get_bits1(gb
)) { // vps_independent_layer_flag[i]
273 unsigned int vps_max_tid_ref_present_flag
= get_bits1(gb
);
274 for (int j
= 0; j
< i
; j
++) {
275 unsigned int vps_direct_ref_layer_flag
= get_bits1(gb
);
276 if (vps_max_tid_ref_present_flag
&& vps_direct_ref_layer_flag
)
277 skip_bits(gb
, 3); // vps_max_tid_il_ref_pics_plus1
283 if (vps_max_layers_minus1
> 0) {
284 unsigned int vps_each_layer_is_an_ols_flag
;
285 if (vps_all_independent_layers_flag
)
286 vps_each_layer_is_an_ols_flag
= get_bits1(gb
);
288 vps_each_layer_is_an_ols_flag
= 0;
289 if (!vps_each_layer_is_an_ols_flag
) {
290 unsigned int vps_ols_mode_idc
;
291 if (!vps_all_independent_layers_flag
)
292 vps_ols_mode_idc
= get_bits(gb
, 2);
294 vps_ols_mode_idc
= 2;
295 if (vps_ols_mode_idc
== 2) {
296 unsigned int vps_num_output_layer_sets_minus2
= get_bits(gb
, 8);
297 for (int i
= 1; i
<= vps_num_output_layer_sets_minus2
+ 1; i
++) {
298 for (int j
= 0; j
<= vps_max_layers_minus1
; j
++) {
299 skip_bits1(gb
); // vps_ols_output_layer_flag[i][j]
304 vps_num_ptls_minus1
= get_bits(gb
, 8);
307 for (int i
= 0; i
<= vps_num_ptls_minus1
; i
++) {
309 vps_pt_present_flag
[i
] = get_bits1(gb
);
311 vps_pt_present_flag
[i
] = 1;
313 if (!vps_default_ptl_dpb_hrd_max_tid_flag
)
314 vps_ptl_max_tid
[i
] = get_bits(gb
, 3);
316 vps_ptl_max_tid
[i
] = vps_max_sublayers_minus1
;
321 for (int i
= 0; i
<= vps_num_ptls_minus1
; i
++)
322 vvcc_parse_ptl(gb
, vvcc
, vps_pt_present_flag
[i
], vps_ptl_max_tid
[i
]);
323 vvcc
->ptl_present_flag
= 1;
325 /* nothing useful for vvcc past this point */
329 static int vvcc_parse_sps(GetBitContext
*gb
,
330 VVCDecoderConfigurationRecord
*vvcc
)
332 unsigned int sps_max_sublayers_minus1
, sps_log2_ctu_size_minus5
;
333 unsigned int sps_subpic_same_size_flag
, sps_pic_height_max_in_luma_samples
,
334 sps_pic_width_max_in_luma_samples
;
335 unsigned int sps_independent_subpics_flag
;
337 skip_bits(gb
, 8); // sps_seq_parameter_set_id && sps_video_parameter_set_id
338 sps_max_sublayers_minus1
= get_bits(gb
, 3);
341 * numTemporalLayers greater than 1 indicates that the stream to which this
342 * configuration record applies is temporally scalable and the contained
343 * number of temporal layers (also referred to as temporal sub-layer or
344 * sub-layer in ISO/IEC 23008-2) is equal to numTemporalLayers. Value 1
345 * indicates that the stream is not temporally scalable. Value 0 indicates
346 * that it is unknown whether the stream is temporally scalable.
348 vvcc
->num_sublayers
= FFMAX(vvcc
->num_sublayers
,
349 sps_max_sublayers_minus1
+ 1);
351 vvcc
->chroma_format_idc
= get_bits(gb
, 2);
352 sps_log2_ctu_size_minus5
= get_bits(gb
, 2);
354 if (get_bits1(gb
)) { // sps_ptl_dpb_hrd_params_present_flag
355 vvcc
->ptl_present_flag
= 1;
356 vvcc_parse_ptl(gb
, vvcc
, 1, sps_max_sublayers_minus1
);
359 skip_bits1(gb
); // sps_gdr_enabled_flag
360 if (get_bits(gb
, 1)) // sps_ref_pic_resampling_enabled_flag
361 skip_bits1(gb
); // sps_res_change_in_clvs_allowed_flag
363 sps_pic_width_max_in_luma_samples
= get_ue_golomb_long(gb
);
364 vvcc
->max_picture_width
=
365 FFMAX(vvcc
->max_picture_width
, sps_pic_width_max_in_luma_samples
);
366 sps_pic_height_max_in_luma_samples
= get_ue_golomb_long(gb
);
367 vvcc
->max_picture_height
=
368 FFMAX(vvcc
->max_picture_height
, sps_pic_height_max_in_luma_samples
);
371 get_ue_golomb_long(gb
); // sps_conf_win_left_offset
372 get_ue_golomb_long(gb
); // sps_conf_win_right_offset
373 get_ue_golomb_long(gb
); // sps_conf_win_top_offset
374 get_ue_golomb_long(gb
); // sps_conf_win_bottom_offset
377 if (get_bits1(gb
)) { // sps_subpic_info_present_flag
378 const unsigned int sps_num_subpics_minus1
= get_ue_golomb_long(gb
);
379 const int ctb_log2_size_y
= sps_log2_ctu_size_minus5
+ 5;
380 const int ctb_size_y
= 1 << ctb_log2_size_y
;
381 const int tmp_width_val
= AV_CEIL_RSHIFT(sps_pic_width_max_in_luma_samples
, ctb_log2_size_y
);
382 const int tmp_height_val
= AV_CEIL_RSHIFT(sps_pic_height_max_in_luma_samples
, ctb_log2_size_y
);
383 const int wlen
= av_ceil_log2(tmp_width_val
);
384 const int hlen
= av_ceil_log2(tmp_height_val
);
385 unsigned int sps_subpic_id_len
;
386 if (sps_num_subpics_minus1
> 0) { // sps_num_subpics_minus1
387 sps_independent_subpics_flag
= get_bits1(gb
);
388 sps_subpic_same_size_flag
= get_bits1(gb
);
390 for (int i
= 0; sps_num_subpics_minus1
> 0 && i
<= sps_num_subpics_minus1
; i
++) {
391 if (!sps_subpic_same_size_flag
|| i
== 0) {
392 if (i
> 0 && sps_pic_width_max_in_luma_samples
> ctb_size_y
)
394 if (i
> 0 && sps_pic_height_max_in_luma_samples
> ctb_size_y
)
396 if (i
< sps_num_subpics_minus1
&& sps_pic_width_max_in_luma_samples
> ctb_size_y
)
398 if (i
< sps_num_subpics_minus1
&& sps_pic_height_max_in_luma_samples
> ctb_size_y
)
401 if (!sps_independent_subpics_flag
) {
402 skip_bits(gb
, 2); // sps_subpic_treated_as_pic_flag && sps_loop_filter_across_subpic_enabled_flag
405 sps_subpic_id_len
= get_ue_golomb_long(gb
) + 1;
406 if (get_bits1(gb
)) { // sps_subpic_id_mapping_explicitly_signalled_flag
407 if (get_bits1(gb
)) // sps_subpic_id_mapping_present_flag
408 for (int i
= 0; i
<= sps_num_subpics_minus1
; i
++) {
409 skip_bits_long(gb
, sps_subpic_id_len
); // sps_subpic_id[i]
413 vvcc
->bit_depth_minus8
= get_ue_golomb_long(gb
);
415 /* nothing useful for vvcc past this point */
419 static int vvcc_parse_pps(GetBitContext
*gb
,
420 VVCDecoderConfigurationRecord
*vvcc
)
423 // Nothing of importance to parse in PPS
424 /* nothing useful for vvcc past this point */
428 static void nal_unit_parse_header(GetBitContext
*gb
, uint8_t *nal_type
)
431 * forbidden_zero_bit u(1)
432 * nuh_reserved_zero_bit u(1)
436 *nal_type
= get_bits(gb
, 5);
439 * nuh_temporal_id_plus1 u(3)
444 static int vvcc_array_add_nal_unit(uint8_t *nal_buf
, uint32_t nal_size
,
445 uint8_t nal_type
, int ps_array_completeness
,
446 VVCCNALUnitArray
*array
)
451 num_nalus
= array
->num_nalus
;
453 ret
= av_reallocp_array(&array
->nal_unit
, num_nalus
+ 1, sizeof(uint8_t *));
458 av_reallocp_array(&array
->nal_unit_length
, num_nalus
+ 1,
463 array
->nal_unit
[num_nalus
] = nal_buf
;
464 array
->nal_unit_length
[num_nalus
] = nal_size
;
465 array
->NAL_unit_type
= nal_type
;
469 * When the sample entry name is 'vvc1', the following applies:
470 * • The value of array_completeness shall be equal to 1 for arrays of SPS,
472 * • If a VVC bitstream includes DCI NAL unit(s), the value of
473 * array_completeness shall be equal to 1 for the array of DCI units.
474 * Otherwise, NAL_unit_type shall not indicate DCI NAL units.
475 * • If a VVC bitstream includes VPS NAL unit(s), the value of
476 * array_completeness shall be equal to 1 for the array of VPS NAL units.
477 * Otherwise, NAL_unit_type shall not indicate VPS NAL units.
478 * When the value of array_completeness is equal to 1 for an array of a
479 * particular NAL_unit_type value, NAL units of that NAL_unit_type value
480 * cannot be updated without causing a different sample entry to be used.
481 * When the sample entry name is 'vvi1', the value of array_completeness
482 * of at least one of the following arrays shall be equal to 0:
483 • The array of DCI NAL units, if present.
484 • The array of VPS NAL units, if present.
485 • The array of SPS NAL units
486 • The array of PPS NAL units.
488 if (nal_type
== VVC_VPS_NUT
|| nal_type
== VVC_SPS_NUT
||
489 nal_type
== VVC_PPS_NUT
|| nal_type
== VVC_DCI_NUT
)
490 array
->array_completeness
= ps_array_completeness
;
495 static int vvcc_add_nal_unit(uint8_t *nal_buf
, uint32_t nal_size
,
496 int ps_array_completeness
,
497 VVCDecoderConfigurationRecord
*vvcc
,
506 rbsp_buf
= ff_nal_unit_extract_rbsp(nal_buf
, nal_size
, &rbsp_size
, 2);
508 ret
= AVERROR(ENOMEM
);
512 ret
= init_get_bits8(&gbc
, rbsp_buf
, rbsp_size
);
516 nal_unit_parse_header(&gbc
, &nal_type
);
519 * Note: only 'declarative' SEI messages are allowed in
520 * vvcc. Perhaps the SEI playload type should be checked
521 * and non-declarative SEI messages discarded?
523 ret
= vvcc_array_add_nal_unit(nal_buf
, nal_size
, nal_type
,
524 ps_array_completeness
,
525 &vvcc
->arrays
[array_idx
]);
528 if (vvcc
->arrays
[array_idx
].num_nalus
== 1)
529 vvcc
->num_of_arrays
++;
531 if (nal_type
== VVC_VPS_NUT
)
532 ret
= vvcc_parse_vps(&gbc
, vvcc
);
533 else if (nal_type
== VVC_SPS_NUT
)
534 ret
= vvcc_parse_sps(&gbc
, vvcc
);
535 else if (nal_type
== VVC_PPS_NUT
)
536 ret
= vvcc_parse_pps(&gbc
, vvcc
);
537 else if (nal_type
== VVC_OPI_NUT
) {
548 static void vvcc_init(VVCDecoderConfigurationRecord
*vvcc
)
550 memset(vvcc
, 0, sizeof(VVCDecoderConfigurationRecord
));
551 vvcc
->lengthSizeMinusOne
= 3; // 4 bytes
552 vvcc
->ptl
.ptl_frame_only_constraint_flag
=
553 vvcc
->ptl
.ptl_multilayer_enabled_flag
= 1;
556 static void vvcc_close(VVCDecoderConfigurationRecord
*vvcc
)
558 for (unsigned i
= 0; i
< FF_ARRAY_ELEMS(vvcc
->arrays
); i
++) {
559 VVCCNALUnitArray
*const array
= &vvcc
->arrays
[i
];
561 array
->num_nalus
= 0;
562 av_freep(&array
->nal_unit
);
563 av_freep(&array
->nal_unit_length
);
566 vvcc
->num_of_arrays
= 0;
569 static int vvcc_write(AVIOContext
*pb
, VVCDecoderConfigurationRecord
*vvcc
)
571 uint16_t vps_count
= 0, sps_count
= 0, pps_count
= 0;
573 * It's unclear how to properly compute these fields, so
574 * let's always set them to values meaning 'unspecified'.
576 vvcc
->avg_frame_rate
= 0;
577 vvcc
->constant_frame_rate
= 1;
579 av_log(NULL
, AV_LOG_TRACE
,
580 "lengthSizeMinusOne: %" PRIu8
"\n",
581 vvcc
->lengthSizeMinusOne
);
582 av_log(NULL
, AV_LOG_TRACE
,
583 "ptl_present_flag: %" PRIu8
"\n",
584 vvcc
->ptl_present_flag
);
585 av_log(NULL
, AV_LOG_TRACE
,
586 "ols_idx: %" PRIu16
"\n", vvcc
->ols_idx
);
587 av_log(NULL
, AV_LOG_TRACE
,
588 "num_sublayers: %" PRIu8
"\n",
589 vvcc
->num_sublayers
);
590 av_log(NULL
, AV_LOG_TRACE
,
591 "constant_frame_rate: %" PRIu8
"\n",
592 vvcc
->constant_frame_rate
);
593 av_log(NULL
, AV_LOG_TRACE
,
594 "chroma_format_idc: %" PRIu8
"\n",
595 vvcc
->chroma_format_idc
);
597 av_log(NULL
, AV_LOG_TRACE
,
598 "bit_depth_minus8: %" PRIu8
"\n",
599 vvcc
->bit_depth_minus8
);
600 av_log(NULL
, AV_LOG_TRACE
,
601 "num_bytes_constraint_info: %" PRIu8
"\n",
602 vvcc
->ptl
.num_bytes_constraint_info
);
603 av_log(NULL
, AV_LOG_TRACE
,
604 "general_profile_idc: %" PRIu8
"\n",
605 vvcc
->ptl
.general_profile_idc
);
606 av_log(NULL
, AV_LOG_TRACE
,
607 "general_tier_flag: %" PRIu8
"\n",
608 vvcc
->ptl
.general_tier_flag
);
609 av_log(NULL
, AV_LOG_TRACE
,
610 "general_level_idc: %" PRIu8
"\n",
611 vvcc
->ptl
.general_level_idc
);
612 av_log(NULL
, AV_LOG_TRACE
,
613 "ptl_frame_only_constraint_flag: %" PRIu8
"\n",
614 vvcc
->ptl
.ptl_frame_only_constraint_flag
);
615 av_log(NULL
, AV_LOG_TRACE
,
616 "ptl_multilayer_enabled_flag: %" PRIu8
"\n",
617 vvcc
->ptl
.ptl_multilayer_enabled_flag
);
618 for (int i
= 0; i
< vvcc
->ptl
.num_bytes_constraint_info
; i
++) {
619 av_log(NULL
, AV_LOG_TRACE
,
620 "general_constraint_info[%d]: %" PRIu8
"\n", i
,
621 vvcc
->ptl
.general_constraint_info
[i
]);
624 for (int i
= 0; i
< vvcc
->num_sublayers
- 1; i
++) {
625 av_log(NULL
, AV_LOG_TRACE
,
626 "ptl_sublayer_level_present_flag[%d]: %" PRIu8
"\n", i
,
627 vvcc
->ptl
.ptl_sublayer_level_present_flag
[i
]);
628 av_log(NULL
, AV_LOG_TRACE
,
629 "sublayer_level_idc[%d]: %" PRIu8
"\n", i
,
630 vvcc
->ptl
.sublayer_level_idc
[i
]);
633 av_log(NULL
, AV_LOG_TRACE
,
634 "num_sub_profiles: %" PRIu8
"\n",
635 vvcc
->ptl
.ptl_num_sub_profiles
);
637 for (unsigned i
= 0; i
< vvcc
->ptl
.ptl_num_sub_profiles
; i
++) {
638 av_log(NULL
, AV_LOG_TRACE
,
639 "general_sub_profile_idc[%u]: %" PRIx32
"\n", i
,
640 vvcc
->ptl
.general_sub_profile_idc
[i
]);
643 av_log(NULL
, AV_LOG_TRACE
,
644 "max_picture_width: %" PRIu16
"\n",
645 vvcc
->max_picture_width
);
646 av_log(NULL
, AV_LOG_TRACE
,
647 "max_picture_height: %" PRIu16
"\n",
648 vvcc
->max_picture_height
);
649 av_log(NULL
, AV_LOG_TRACE
,
650 "avg_frame_rate: %" PRIu16
"\n",
651 vvcc
->avg_frame_rate
);
653 av_log(NULL
, AV_LOG_TRACE
,
654 "num_of_arrays: %" PRIu8
"\n",
655 vvcc
->num_of_arrays
);
656 for (unsigned i
= 0; i
< FF_ARRAY_ELEMS(vvcc
->arrays
); i
++) {
657 const VVCCNALUnitArray
*const array
= &vvcc
->arrays
[i
];
659 if (array
->num_nalus
== 0)
662 av_log(NULL
, AV_LOG_TRACE
,
663 "array_completeness[%u]: %" PRIu8
"\n", i
,
664 array
->array_completeness
);
665 av_log(NULL
, AV_LOG_TRACE
,
666 "NAL_unit_type[%u]: %" PRIu8
"\n", i
,
667 array
->NAL_unit_type
);
668 av_log(NULL
, AV_LOG_TRACE
,
669 "num_nalus[%u]: %" PRIu16
"\n", i
,
671 for (unsigned j
= 0; j
< array
->num_nalus
; j
++)
672 av_log(NULL
, AV_LOG_TRACE
,
673 "nal_unit_length[%u][%u]: %"
674 PRIu16
"\n", i
, j
, array
->nal_unit_length
[j
]);
678 * We need at least one of each: SPS and PPS.
680 vps_count
= vvcc
->arrays
[VPS_INDEX
].num_nalus
;
681 sps_count
= vvcc
->arrays
[SPS_INDEX
].num_nalus
;
682 pps_count
= vvcc
->arrays
[PPS_INDEX
].num_nalus
;
683 if (vps_count
> VVC_MAX_VPS_COUNT
)
684 return AVERROR_INVALIDDATA
;
685 if (!sps_count
|| sps_count
> VVC_MAX_SPS_COUNT
)
686 return AVERROR_INVALIDDATA
;
687 if (!pps_count
|| pps_count
> VVC_MAX_PPS_COUNT
)
688 return AVERROR_INVALIDDATA
;
690 /* bit(5) reserved = ‘11111’b;
691 unsigned int (2) LengthSizeMinusOne
692 unsigned int (1) ptl_present_flag */
693 avio_w8(pb
, vvcc
->lengthSizeMinusOne
<< 1 | vvcc
->ptl_present_flag
| 0xf8);
695 if (vvcc
->ptl_present_flag
) {
699 init_put_bits(&pbc
, buf
, sizeof(buf
));
701 * unsigned int(9) ols_idx;
702 * unsigned int(3) num_sublayers;
703 * unsigned int(2) constant_frame_rate;
704 * unsigned int(2) chroma_format_idc; */
706 vvcc
->ols_idx
<< 7 | vvcc
->num_sublayers
<< 4 | vvcc
->
707 constant_frame_rate
<< 2 | vvcc
->chroma_format_idc
);
709 /* unsigned int(3) bit_depth_minus8;
710 bit(5) reserved = ‘11111’b; */
711 avio_w8(pb
, vvcc
->bit_depth_minus8
<< 5 | 0x1f);
715 /* bit(2) reserved = ‘00’b;
716 unsigned int (6) num_bytes_constraint_info */
717 avio_w8(pb
, vvcc
->ptl
.num_bytes_constraint_info
& 0x3f);
719 /* unsigned int (7) general_profile_idc
720 unsigned int (1) general_tier_flag */
722 vvcc
->ptl
.general_profile_idc
<< 1 | vvcc
->ptl
.general_tier_flag
);
724 /* unsigned int (8) general_level_idc */
725 avio_w8(pb
, vvcc
->ptl
.general_level_idc
);
728 * unsigned int (1) ptl_frame_only_constraint_flag
729 * unsigned int (1) ptl_multilayer_enabled_flag
730 * unsigned int (8*num_bytes_constraint_info -2) general_constraint_info */
731 put_bits(&pbc
, 1, vvcc
->ptl
.ptl_frame_only_constraint_flag
);
732 put_bits(&pbc
, 1, vvcc
->ptl
.ptl_multilayer_enabled_flag
);
733 av_assert0(vvcc
->ptl
.num_bytes_constraint_info
);
734 for (int i
= 0; i
< vvcc
->ptl
.num_bytes_constraint_info
- 1; i
++)
735 put_bits(&pbc
, 8, vvcc
->ptl
.general_constraint_info
[i
]);
736 put_bits(&pbc
, 6, vvcc
->ptl
.general_constraint_info
[vvcc
->ptl
.num_bytes_constraint_info
- 1] & 0x3f);
737 flush_put_bits(&pbc
);
738 avio_write(pb
, buf
, put_bytes_output(&pbc
));
740 if (vvcc
->num_sublayers
> 1) {
741 uint8_t ptl_sublayer_level_present_flags
= 0;
742 for (int i
= vvcc
->num_sublayers
- 2; i
>= 0; i
--) {
743 ptl_sublayer_level_present_flags
=
744 (ptl_sublayer_level_present_flags
<< 1 | vvcc
->ptl
.
745 ptl_sublayer_level_present_flag
[i
]);
747 avio_w8(pb
, ptl_sublayer_level_present_flags
);
750 for (int i
= vvcc
->num_sublayers
- 2; i
>= 0; i
--) {
751 if (vvcc
->ptl
.ptl_sublayer_level_present_flag
[i
])
752 avio_w8(pb
, vvcc
->ptl
.sublayer_level_idc
[i
]);
755 /* unsigned int(8) num_sub_profiles; */
756 avio_w8(pb
, vvcc
->ptl
.ptl_num_sub_profiles
);
758 for (int j
= 0; j
< vvcc
->ptl
.ptl_num_sub_profiles
; j
++) {
759 /* unsigned int(32) general_sub_profile_idc[j]; */
760 avio_wb32(pb
, vvcc
->ptl
.general_sub_profile_idc
[j
]);
763 //End of VvcPTLRecord
766 * unsigned int(16) max_picture_width;*/
767 avio_wb16(pb
, vvcc
->max_picture_width
);
770 * unsigned int(16) max_picture_height;*/
771 avio_wb16(pb
, vvcc
->max_picture_height
);
774 * unsigned int(16) avg_frame_rate; */
775 avio_wb16(pb
, vvcc
->avg_frame_rate
);
778 /* unsigned int(8) num_of_arrays; */
779 avio_w8(pb
, vvcc
->num_of_arrays
);
781 for (unsigned i
= 0; i
< FF_ARRAY_ELEMS(vvcc
->arrays
); i
++) {
782 const VVCCNALUnitArray
*const array
= &vvcc
->arrays
[i
];
784 if (!array
->num_nalus
)
787 * bit(1) array_completeness;
788 * unsigned int(2) reserved = 0;
789 * unsigned int(5) NAL_unit_type;
791 avio_w8(pb
, array
->array_completeness
<< 7 |
792 array
->NAL_unit_type
& 0x1f);
793 /* unsigned int(16) num_nalus; */
794 if (array
->NAL_unit_type
!= VVC_DCI_NUT
&&
795 array
->NAL_unit_type
!= VVC_OPI_NUT
)
796 avio_wb16(pb
, array
->num_nalus
);
797 for (int j
= 0; j
< array
->num_nalus
; j
++) {
798 /* unsigned int(16) nal_unit_length; */
799 avio_wb16(pb
, array
->nal_unit_length
[j
]);
801 /* bit(8*nal_unit_length) nal_unit; */
802 avio_write(pb
, array
->nal_unit
[j
],
803 array
->nal_unit_length
[j
]);
810 int ff_vvc_annexb2mp4(AVIOContext
*pb
, const uint8_t *buf_in
,
811 int size
, int filter_ps
, int *ps_count
)
813 int num_ps
= 0, ret
= 0;
814 uint8_t *buf
, *end
, *start
= NULL
;
817 ret
= ff_nal_parse_units(pb
, buf_in
, size
);
821 ret
= ff_nal_parse_units_buf(buf_in
, &start
, &size
);
829 while (end
- buf
> 4) {
830 uint32_t len
= FFMIN(AV_RB32(buf
), end
- buf
- 4);
831 uint8_t type
= (buf
[5] >> 3);
844 avio_write(pb
, buf
, len
);
858 int ff_vvc_annexb2mp4_buf(const uint8_t *buf_in
, uint8_t **buf_out
,
859 int *size
, int filter_ps
, int *ps_count
)
864 ret
= avio_open_dyn_buf(&pb
);
868 ret
= ff_vvc_annexb2mp4(pb
, buf_in
, *size
, filter_ps
, ps_count
);
870 ffio_free_dyn_buf(&pb
);
874 *size
= avio_close_dyn_buf(pb
, buf_out
);
879 int ff_isom_write_vvcc(AVIOContext
*pb
, const uint8_t *data
,
880 int size
, int ps_array_completeness
)
882 VVCDecoderConfigurationRecord vvcc
;
883 uint8_t *buf
, *end
, *start
;
887 /* We can't write a valid vvcc from the provided data */
888 return AVERROR_INVALIDDATA
;
889 } else if ((*data
& 0xf8) == 0xf8) {
890 /* Data is already vvcc-formatted */
891 avio_write(pb
, data
, size
);
893 } else if (!(AV_RB24(data
) == 1 || AV_RB32(data
) == 1)) {
894 /* Not a valid Annex B start code prefix */
895 return AVERROR_INVALIDDATA
;
898 ret
= ff_nal_parse_units_buf(data
, &start
, &size
);
907 while (end
- buf
> 4) {
908 uint32_t len
= FFMIN(AV_RB32(buf
), end
- buf
- 4);
909 uint8_t type
= (buf
[5] >> 3);
913 for (unsigned i
= 0; i
< FF_ARRAY_ELEMS(vvcc
.arrays
); i
++) {
914 static const uint8_t array_idx_to_type
[] =
915 { VVC_OPI_NUT
, VVC_VPS_NUT
, VVC_SPS_NUT
,
916 VVC_PPS_NUT
, VVC_PREFIX_SEI_NUT
, VVC_SUFFIX_SEI_NUT
};
918 if (type
== array_idx_to_type
[i
]) {
919 ret
= vvcc_add_nal_unit(buf
, len
, ps_array_completeness
,
930 ret
= vvcc_write(pb
, &vvcc
);