FFmpeg
Loading...
Searching...
No Matches
vvc.c
Go to the documentation of this file.
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/put_bits.h"
25#include "libavcodec/golomb.h"
26#include "libavcodec/vvc.h"
27#include "libavutil/avassert.h"
29#include "libavutil/mem.h"
30#include "avc.h"
31#include "avio.h"
32#include "avio_internal.h"
33#include "nal.h"
34#include "vvc.h"
35
36enum {
44};
45
46typedef struct VVCCNALUnitArray {
49 uint16_t num_nalus;
50 uint16_t *nal_unit_length;
51 uint8_t **nal_unit;
53
67
83
86{
87 /*
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.
91 */
92 if (vvcc->ptl.general_tier_flag < ptl->general_tier_flag)
93 vvcc->ptl.general_level_idc = ptl->general_level_idc;
94 else
96 FFMAX(vvcc->ptl.general_level_idc, ptl->general_level_idc);
97
98 /*
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.
101 */
102 vvcc->ptl.general_tier_flag =
103 FFMAX(vvcc->ptl.general_tier_flag, ptl->general_tier_flag);
104
105 /*
106 * The profile indication general_profile_idc must indicate a profile to
107 * which the stream associated with this configuration record conforms.
108 *
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
115 * be met.
116 *
117 * Note: set the profile to the highest value for the sake of simplicity.
118 */
120 FFMAX(vvcc->ptl.general_profile_idc, ptl->general_profile_idc);
121
122 /*
123 * Each bit in flags may only be set if all
124 * the parameter sets set that bit.
125 */
127 ptl->ptl_frame_only_constraint_flag;
128 vvcc->ptl.ptl_multilayer_enabled_flag &= ptl->ptl_multilayer_enabled_flag;
129
130 /*
131 * Constraints Info
132 */
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);
137 } else {
139 memset(&vvcc->ptl.general_constraint_info[0], 0, sizeof(vvcc->ptl.general_constraint_info));
140 }
141
142 /*
143 * Each bit in flags may only be set if one of
144 * the parameter sets set that bit.
145 */
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);
150
151 for (int i = vvcc->num_sublayers - 2; i >= 0; i--) {
153 ptl->ptl_sublayer_level_present_flag[i];
155 vvcc->ptl.sublayer_level_idc[i] =
157 ptl->sublayer_level_idc[i]);
158 } else {
159 if (i == vvcc->num_sublayers - 2) {
161 } else {
162 vvcc->ptl.sublayer_level_idc[i] =
163 vvcc->ptl.sublayer_level_idc[i + 1];
164 }
165 }
166 }
167
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++) {
173 ptl->general_sub_profile_idc[i];
174 }
175 }
176}
177
180 unsigned int profileTierPresentFlag,
181 unsigned int max_sub_layers_minus1)
182{
183 VVCPTLRecord general_ptl = { 0 };
184
185 if (profileTierPresentFlag) {
186 general_ptl.general_profile_idc = get_bits(gb, 7);
187 general_ptl.general_tier_flag = get_bits1(gb);
188 }
189 general_ptl.general_level_idc = get_bits(gb, 8);
190
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);
200
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);
204 }
205 align_get_bits(gb);
206 }
207
208 for (int i = max_sub_layers_minus1 - 1; i >= 0; i--)
210
211 align_get_bits(gb);
212
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);
216 }
217
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);
223 }
224 }
225
226 vvcc_update_ptl(vvcc, &general_ptl);
227}
228
231{
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;
236
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;
240
241 /*
242 * vps_video_parameter_set_id u(4)
243 */
244 skip_bits(gb, 4);
245
246 vps_max_layers_minus1 = get_bits(gb, 6);
247 vps_max_sublayers_minus1 = get_bits(gb, 3);
248 if (vps_max_sublayers_minus1 >= VVC_MAX_SUBLAYERS)
249 return AVERROR_INVALIDDATA;
250
251 /*
252 * numTemporalLayers greater than 1 indicates that the stream to which this
253 * configuration record applies is temporally scalable and the contained
254 * number of temporal layers (also referred to as temporal sub-layer or
255 * sub-layer in ISO/IEC 23008-2) is equal to numTemporalLayers. Value 1
256 * indicates that the stream is not temporally scalable. Value 0 indicates
257 * that it is unknown whether the stream is temporally scalable.
258 */
259 vvcc->num_sublayers = FFMAX(vvcc->num_sublayers,
260 vps_max_sublayers_minus1 + 1);
261
262 if (vps_max_layers_minus1 > 0 && vps_max_sublayers_minus1 > 0)
263 vps_default_ptl_dpb_hrd_max_tid_flag = get_bits1(gb);
264 else
265 vps_default_ptl_dpb_hrd_max_tid_flag = 1;
266 if (vps_max_layers_minus1 > 0)
267 vps_all_independent_layers_flag = get_bits1(gb);
268 else
269 vps_all_independent_layers_flag = 1;
270
271 for (int i = 0; i <= vps_max_layers_minus1; i++) {
272 skip_bits(gb, 6); //vps_layer_id[i]
273 if (i > 0 && !vps_all_independent_layers_flag) {
274 if (!get_bits1(gb)) { // vps_independent_layer_flag[i]
275 unsigned int vps_max_tid_ref_present_flag = get_bits1(gb);
276 for (int j = 0; j < i; j++) {
277 unsigned int vps_direct_ref_layer_flag = get_bits1(gb);
278 if (vps_max_tid_ref_present_flag && vps_direct_ref_layer_flag)
279 skip_bits(gb, 3); // vps_max_tid_il_ref_pics_plus1
280 }
281 }
282 }
283 }
284
285 if (vps_max_layers_minus1 > 0) {
286 unsigned int vps_each_layer_is_an_ols_flag;
287 if (vps_all_independent_layers_flag)
288 vps_each_layer_is_an_ols_flag = get_bits1(gb);
289 else
290 vps_each_layer_is_an_ols_flag = 0;
291 if (!vps_each_layer_is_an_ols_flag) {
292 unsigned int vps_ols_mode_idc;
293 if (!vps_all_independent_layers_flag)
294 vps_ols_mode_idc = get_bits(gb, 2);
295 else
296 vps_ols_mode_idc = 2;
297 if (vps_ols_mode_idc == 2) {
298 unsigned int vps_num_output_layer_sets_minus2 = get_bits(gb, 8);
299 for (int i = 1; i <= vps_num_output_layer_sets_minus2 + 1; i++) {
300 for (int j = 0; j <= vps_max_layers_minus1; j++) {
301 skip_bits1(gb); // vps_ols_output_layer_flag[i][j]
302 }
303 }
304 }
305 }
306 vps_num_ptls_minus1 = get_bits(gb, 8);
307 }
308
309 for (int i = 0; i <= vps_num_ptls_minus1; i++) {
310 if (i > 0)
311 vps_pt_present_flag[i] = get_bits1(gb);
312 else
313 vps_pt_present_flag[i] = 1;
314
315 if (!vps_default_ptl_dpb_hrd_max_tid_flag) {
316 vps_ptl_max_tid[i] = get_bits(gb, 3);
317 if (vps_ptl_max_tid[i] > vps_max_sublayers_minus1)
318 return AVERROR_INVALIDDATA;
319 } else
320 vps_ptl_max_tid[i] = vps_max_sublayers_minus1;
321 }
322
323 align_get_bits(gb);
324
325 for (int i = 0; i <= vps_num_ptls_minus1; i++)
326 vvcc_parse_ptl(gb, vvcc, vps_pt_present_flag[i], vps_ptl_max_tid[i]);
327 vvcc->ptl_present_flag = 1;
328
329 /* nothing useful for vvcc past this point */
330 return 0;
331}
332
335{
336 unsigned int sps_max_sublayers_minus1, sps_log2_ctu_size_minus5;
337 unsigned int sps_subpic_same_size_flag, sps_pic_height_max_in_luma_samples,
338 sps_pic_width_max_in_luma_samples;
339 unsigned int sps_independent_subpics_flag;
340
341 skip_bits(gb, 8); // sps_seq_parameter_set_id && sps_video_parameter_set_id
342 sps_max_sublayers_minus1 = get_bits(gb, 3);
343 if (sps_max_sublayers_minus1 >= VVC_MAX_SUBLAYERS)
344 return AVERROR_INVALIDDATA;
345
346 /*
347 * numTemporalLayers greater than 1 indicates that the stream to which this
348 * configuration record applies is temporally scalable and the contained
349 * number of temporal layers (also referred to as temporal sub-layer or
350 * sub-layer in ISO/IEC 23008-2) is equal to numTemporalLayers. Value 1
351 * indicates that the stream is not temporally scalable. Value 0 indicates
352 * that it is unknown whether the stream is temporally scalable.
353 */
354 vvcc->num_sublayers = FFMAX(vvcc->num_sublayers,
355 sps_max_sublayers_minus1 + 1);
356
357 vvcc->chroma_format_idc = get_bits(gb, 2);
358 sps_log2_ctu_size_minus5 = get_bits(gb, 2);
359
360 if (get_bits1(gb)) { // sps_ptl_dpb_hrd_params_present_flag
361 vvcc->ptl_present_flag = 1;
362 vvcc_parse_ptl(gb, vvcc, 1, sps_max_sublayers_minus1);
363 }
364
365 skip_bits1(gb); // sps_gdr_enabled_flag
366 if (get_bits(gb, 1)) // sps_ref_pic_resampling_enabled_flag
367 skip_bits1(gb); // sps_res_change_in_clvs_allowed_flag
368
369 sps_pic_width_max_in_luma_samples = get_ue_golomb_long(gb);
370 vvcc->max_picture_width =
371 FFMAX(vvcc->max_picture_width, sps_pic_width_max_in_luma_samples);
372 sps_pic_height_max_in_luma_samples = get_ue_golomb_long(gb);
373 vvcc->max_picture_height =
374 FFMAX(vvcc->max_picture_height, sps_pic_height_max_in_luma_samples);
375
376 if (get_bits1(gb)) {
377 get_ue_golomb_long(gb); // sps_conf_win_left_offset
378 get_ue_golomb_long(gb); // sps_conf_win_right_offset
379 get_ue_golomb_long(gb); // sps_conf_win_top_offset
380 get_ue_golomb_long(gb); // sps_conf_win_bottom_offset
381 }
382
383 if (get_bits1(gb)) { // sps_subpic_info_present_flag
384 const unsigned int sps_num_subpics_minus1 = get_ue_golomb_long(gb);
385 const int ctb_log2_size_y = sps_log2_ctu_size_minus5 + 5;
386 const int ctb_size_y = 1 << ctb_log2_size_y;
387 const int tmp_width_val = AV_CEIL_RSHIFT(sps_pic_width_max_in_luma_samples, ctb_log2_size_y);
388 const int tmp_height_val = AV_CEIL_RSHIFT(sps_pic_height_max_in_luma_samples, ctb_log2_size_y);
389 const int wlen = av_ceil_log2(tmp_width_val);
390 const int hlen = av_ceil_log2(tmp_height_val);
391 unsigned int sps_subpic_id_len;
392 if (sps_num_subpics_minus1 > 0) { // sps_num_subpics_minus1
393 sps_independent_subpics_flag = get_bits1(gb);
394 sps_subpic_same_size_flag = get_bits1(gb);
395 }
396 for (int i = 0; sps_num_subpics_minus1 > 0 && i <= sps_num_subpics_minus1; i++) {
397 if (!sps_subpic_same_size_flag || i == 0) {
398 if (i > 0 && sps_pic_width_max_in_luma_samples > ctb_size_y)
399 skip_bits(gb, wlen);
400 if (i > 0 && sps_pic_height_max_in_luma_samples > ctb_size_y)
401 skip_bits(gb, hlen);
403 skip_bits(gb, wlen);
405 skip_bits(gb, hlen);
406 }
407 if (!sps_independent_subpics_flag) {
408 skip_bits(gb, 2); // sps_subpic_treated_as_pic_flag && sps_loop_filter_across_subpic_enabled_flag
409 }
410 }
411 sps_subpic_id_len = get_ue_golomb_long(gb) + 1;
412 if (get_bits1(gb)) { // sps_subpic_id_mapping_explicitly_signalled_flag
413 if (get_bits1(gb)) // sps_subpic_id_mapping_present_flag
414 for (int i = 0; i <= sps_num_subpics_minus1; i++) {
415 skip_bits_long(gb, sps_subpic_id_len); // sps_subpic_id[i]
416 }
417 }
418 }
420
421 /* nothing useful for vvcc past this point */
422 return 0;
423}
424
427{
428
429 // Nothing of importance to parse in PPS
430 /* nothing useful for vvcc past this point */
431 return 0;
432}
433
434static void nal_unit_parse_header(GetBitContext *gb, uint8_t *nal_type)
435{
436 /*
437 * forbidden_zero_bit u(1)
438 * nuh_reserved_zero_bit u(1)
439 * nuh_layer_id u(6)
440 */
441 skip_bits(gb, 8);
442 *nal_type = get_bits(gb, 5);
443
444 /*
445 * nuh_temporal_id_plus1 u(3)
446 */
447 skip_bits(gb, 3);
448}
449
450static int vvcc_array_add_nal_unit(uint8_t *nal_buf, uint32_t nal_size,
451 uint8_t nal_type, int ps_array_completeness,
453{
454 int ret;
455 uint16_t num_nalus;
456
457 num_nalus = array->num_nalus;
458
459 ret = av_reallocp_array(&array->nal_unit, num_nalus + 1, sizeof(uint8_t *));
460 if (ret < 0)
461 return ret;
462
463 ret =
464 av_reallocp_array(&array->nal_unit_length, num_nalus + 1,
465 sizeof(uint16_t));
466 if (ret < 0)
467 return ret;
468
469 array->nal_unit[num_nalus] = nal_buf;
470 array->nal_unit_length[num_nalus] = nal_size;
471 array->NAL_unit_type = nal_type;
472 array->num_nalus++;
473
474 /*
475 * When the sample entry name is 'vvc1', the following applies:
476 * • The value of array_completeness shall be equal to 1 for arrays of SPS,
477 * and PPS NAL units.
478 * • If a VVC bitstream includes DCI NAL unit(s), the value of
479 * array_completeness shall be equal to 1 for the array of DCI units.
480 * Otherwise, NAL_unit_type shall not indicate DCI NAL units.
481 * • If a VVC bitstream includes VPS NAL unit(s), the value of
482 * array_completeness shall be equal to 1 for the array of VPS NAL units.
483 * Otherwise, NAL_unit_type shall not indicate VPS NAL units.
484 * When the value of array_completeness is equal to 1 for an array of a
485 * particular NAL_unit_type value, NAL units of that NAL_unit_type value
486 * cannot be updated without causing a different sample entry to be used.
487 * When the sample entry name is 'vvi1', the value of array_completeness
488 * of at least one of the following arrays shall be equal to 0:
489 • The array of DCI NAL units, if present.
490 • The array of VPS NAL units, if present.
491 • The array of SPS NAL units
492 • The array of PPS NAL units.
493 */
494 if (nal_type == VVC_VPS_NUT || nal_type == VVC_SPS_NUT ||
495 nal_type == VVC_PPS_NUT || nal_type == VVC_DCI_NUT )
496 array->array_completeness = ps_array_completeness;
497
498 return 0;
499}
500
501static int vvcc_add_nal_unit(uint8_t *nal_buf, uint32_t nal_size,
502 int ps_array_completeness,
504 unsigned array_idx)
505{
506 int ret = 0;
507 GetBitContext gbc;
508 uint8_t nal_type;
509 uint8_t *rbsp_buf;
510 uint32_t rbsp_size;
511
512 rbsp_buf = ff_nal_unit_extract_rbsp(nal_buf, nal_size, &rbsp_size, 2);
513 if (!rbsp_buf) {
514 ret = AVERROR(ENOMEM);
515 goto end;
516 }
517
518 ret = init_get_bits8(&gbc, rbsp_buf, rbsp_size);
519 if (ret < 0)
520 goto end;
521
522 nal_unit_parse_header(&gbc, &nal_type);
523
524 /*
525 * Note: only 'declarative' SEI messages are allowed in
526 * vvcc. Perhaps the SEI playload type should be checked
527 * and non-declarative SEI messages discarded?
528 */
529 ret = vvcc_array_add_nal_unit(nal_buf, nal_size, nal_type,
530 ps_array_completeness,
531 &vvcc->arrays[array_idx]);
532 if (ret < 0)
533 goto end;
534 if (vvcc->arrays[array_idx].num_nalus == 1)
535 vvcc->num_of_arrays++;
536
537 if (nal_type == VVC_VPS_NUT)
538 ret = vvcc_parse_vps(&gbc, vvcc);
539 else if (nal_type == VVC_SPS_NUT)
540 ret = vvcc_parse_sps(&gbc, vvcc);
541 else if (nal_type == VVC_PPS_NUT)
542 ret = vvcc_parse_pps(&gbc, vvcc);
543 else if (nal_type == VVC_OPI_NUT) {
544 // not yet supported
545 }
546 if (ret < 0)
547 goto end;
548
549 end:
550 av_free(rbsp_buf);
551 return ret;
552}
553
555{
556 memset(vvcc, 0, sizeof(VVCDecoderConfigurationRecord));
557 vvcc->lengthSizeMinusOne = 3; // 4 bytes
560}
561
563{
564 for (unsigned i = 0; i < FF_ARRAY_ELEMS(vvcc->arrays); i++) {
565 VVCCNALUnitArray *const array = &vvcc->arrays[i];
566
567 array->num_nalus = 0;
568 av_freep(&array->nal_unit);
569 av_freep(&array->nal_unit_length);
570 }
571
572 vvcc->num_of_arrays = 0;
573}
574
576{
577 uint16_t vps_count = 0, sps_count = 0, pps_count = 0;
578 /*
579 * It's unclear how to properly compute these fields, so
580 * let's always set them to values meaning 'unspecified'.
581 */
582 vvcc->avg_frame_rate = 0;
583 vvcc->constant_frame_rate = 1;
584
586 "lengthSizeMinusOne: %" PRIu8 "\n",
587 vvcc->lengthSizeMinusOne);
589 "ptl_present_flag: %" PRIu8 "\n",
590 vvcc->ptl_present_flag);
592 "ols_idx: %" PRIu16 "\n", vvcc->ols_idx);
594 "num_sublayers: %" PRIu8 "\n",
595 vvcc->num_sublayers);
597 "constant_frame_rate: %" PRIu8 "\n",
598 vvcc->constant_frame_rate);
600 "chroma_format_idc: %" PRIu8 "\n",
601 vvcc->chroma_format_idc);
602
604 "bit_depth_minus8: %" PRIu8 "\n",
605 vvcc->bit_depth_minus8);
607 "num_bytes_constraint_info: %" PRIu8 "\n",
610 "general_profile_idc: %" PRIu8 "\n",
613 "general_tier_flag: %" PRIu8 "\n",
614 vvcc->ptl.general_tier_flag);
616 "general_level_idc: %" PRIu8 "\n",
617 vvcc->ptl.general_level_idc);
619 "ptl_frame_only_constraint_flag: %" PRIu8 "\n",
622 "ptl_multilayer_enabled_flag: %" PRIu8 "\n",
624 for (int i = 0; i < vvcc->ptl.num_bytes_constraint_info; i++) {
626 "general_constraint_info[%d]: %" PRIu8 "\n", i,
628 }
629
630 for (int i = 0; i < vvcc->num_sublayers - 1; i++) {
632 "ptl_sublayer_level_present_flag[%d]: %" PRIu8 "\n", i,
635 "sublayer_level_idc[%d]: %" PRIu8 "\n", i,
636 vvcc->ptl.sublayer_level_idc[i]);
637 }
638
640 "num_sub_profiles: %" PRIu8 "\n",
642
643 for (unsigned i = 0; i < vvcc->ptl.ptl_num_sub_profiles; i++) {
645 "general_sub_profile_idc[%u]: %" PRIx32 "\n", i,
647 }
648
650 "max_picture_width: %" PRIu16 "\n",
651 vvcc->max_picture_width);
653 "max_picture_height: %" PRIu16 "\n",
654 vvcc->max_picture_height);
656 "avg_frame_rate: %" PRIu16 "\n",
657 vvcc->avg_frame_rate);
658
660 "num_of_arrays: %" PRIu8 "\n",
661 vvcc->num_of_arrays);
662 for (unsigned i = 0; i < FF_ARRAY_ELEMS(vvcc->arrays); i++) {
663 const VVCCNALUnitArray *const array = &vvcc->arrays[i];
664
665 if (array->num_nalus == 0)
666 continue;
667
669 "array_completeness[%u]: %" PRIu8 "\n", i,
670 array->array_completeness);
672 "NAL_unit_type[%u]: %" PRIu8 "\n", i,
673 array->NAL_unit_type);
675 "num_nalus[%u]: %" PRIu16 "\n", i,
676 array->num_nalus);
677 for (unsigned j = 0; j < array->num_nalus; j++)
679 "nal_unit_length[%u][%u]: %"
680 PRIu16 "\n", i, j, array->nal_unit_length[j]);
681 }
682
683 /*
684 * We need at least one of each: SPS and PPS.
685 */
686 vps_count = vvcc->arrays[VPS_INDEX].num_nalus;
687 sps_count = vvcc->arrays[SPS_INDEX].num_nalus;
688 pps_count = vvcc->arrays[PPS_INDEX].num_nalus;
689 if (vps_count > VVC_MAX_VPS_COUNT)
690 return AVERROR_INVALIDDATA;
691 if (!sps_count || sps_count > VVC_MAX_SPS_COUNT)
692 return AVERROR_INVALIDDATA;
693 if (!pps_count || pps_count > VVC_MAX_PPS_COUNT)
694 return AVERROR_INVALIDDATA;
695
696 /* bit(5) reserved = ‘11111’b;
697 unsigned int (2) LengthSizeMinusOne
698 unsigned int (1) ptl_present_flag */
699 avio_w8(pb, vvcc->lengthSizeMinusOne << 1 | vvcc->ptl_present_flag | 0xf8);
700
701 if (vvcc->ptl_present_flag) {
702 uint8_t buf[64];
703 PutBitContext pbc;
704
705 init_put_bits(&pbc, buf, sizeof(buf));
706 /*
707 * unsigned int(9) ols_idx;
708 * unsigned int(3) num_sublayers;
709 * unsigned int(2) constant_frame_rate;
710 * unsigned int(2) chroma_format_idc; */
711 avio_wb16(pb,
712 vvcc->ols_idx << 7 | vvcc->num_sublayers << 4 | vvcc->
713 constant_frame_rate << 2 | vvcc->chroma_format_idc);
714
715 /* unsigned int(3) bit_depth_minus8;
716 bit(5) reserved = ‘11111’b; */
717 avio_w8(pb, vvcc->bit_depth_minus8 << 5 | 0x1f);
718
719 //VVCPTLRecord
720
721 /* bit(2) reserved = ‘00’b;
722 unsigned int (6) num_bytes_constraint_info */
723 avio_w8(pb, vvcc->ptl.num_bytes_constraint_info & 0x3f);
724
725 /* unsigned int (7) general_profile_idc
726 unsigned int (1) general_tier_flag */
727 avio_w8(pb,
728 vvcc->ptl.general_profile_idc << 1 | vvcc->ptl.general_tier_flag);
729
730 /* unsigned int (8) general_level_idc */
731 avio_w8(pb, vvcc->ptl.general_level_idc);
732
733 /*
734 * unsigned int (1) ptl_frame_only_constraint_flag
735 * unsigned int (1) ptl_multilayer_enabled_flag
736 * unsigned int (8*num_bytes_constraint_info -2) general_constraint_info */
740 for (int i = 0; i < vvcc->ptl.num_bytes_constraint_info - 1; i++)
741 put_bits(&pbc, 8, vvcc->ptl.general_constraint_info[i]);
742 put_bits(&pbc, 6, vvcc->ptl.general_constraint_info[vvcc->ptl.num_bytes_constraint_info - 1] & 0x3f);
743 flush_put_bits(&pbc);
744 avio_write(pb, buf, put_bytes_output(&pbc));
745
746 if (vvcc->num_sublayers > 1) {
747 uint8_t ptl_sublayer_level_present_flags = 0;
748 for (int i = vvcc->num_sublayers - 2; i >= 0; i--) {
749 ptl_sublayer_level_present_flags =
750 (ptl_sublayer_level_present_flags << 1 | vvcc->ptl.
751 ptl_sublayer_level_present_flag[i]);
752 }
753 avio_w8(pb, ptl_sublayer_level_present_flags);
754 }
755
756 for (int i = vvcc->num_sublayers - 2; i >= 0; i--) {
758 avio_w8(pb, vvcc->ptl.sublayer_level_idc[i]);
759 }
760
761 /* unsigned int(8) num_sub_profiles; */
763
764 for (int j = 0; j < vvcc->ptl.ptl_num_sub_profiles; j++) {
765 /* unsigned int(32) general_sub_profile_idc[j]; */
767 }
768
769 //End of VvcPTLRecord
770
771 /*
772 * unsigned int(16) max_picture_width;*/
773 avio_wb16(pb, vvcc->max_picture_width);
774
775 /*
776 * unsigned int(16) max_picture_height;*/
777 avio_wb16(pb, vvcc->max_picture_height);
778
779 /*
780 * unsigned int(16) avg_frame_rate; */
781 avio_wb16(pb, vvcc->avg_frame_rate);
782 }
783
784 /* unsigned int(8) num_of_arrays; */
785 avio_w8(pb, vvcc->num_of_arrays);
786
787 for (unsigned i = 0; i < FF_ARRAY_ELEMS(vvcc->arrays); i++) {
788 const VVCCNALUnitArray *const array = &vvcc->arrays[i];
789
790 if (!array->num_nalus)
791 continue;
792 /*
793 * bit(1) array_completeness;
794 * unsigned int(2) reserved = 0;
795 * unsigned int(5) NAL_unit_type;
796 */
797 avio_w8(pb, array->array_completeness << 7 |
798 array->NAL_unit_type & 0x1f);
799 /* unsigned int(16) num_nalus; */
800 if (array->NAL_unit_type != VVC_DCI_NUT &&
801 array->NAL_unit_type != VVC_OPI_NUT)
802 avio_wb16(pb, array->num_nalus);
803 for (int j = 0; j < array->num_nalus; j++) {
804 /* unsigned int(16) nal_unit_length; */
805 avio_wb16(pb, array->nal_unit_length[j]);
806
807 /* bit(8*nal_unit_length) nal_unit; */
808 avio_write(pb, array->nal_unit[j],
809 array->nal_unit_length[j]);
810 }
811 }
812
813 return 0;
814}
815
816int ff_vvc_annexb2mp4(AVIOContext *pb, const uint8_t *buf_in,
817 int size, int filter_ps, int *ps_count)
818{
819 int num_ps = 0, ret = 0;
820 uint8_t *buf, *end, *start = NULL;
821
822 if (!filter_ps) {
823 ret = ff_nal_parse_units(pb, buf_in, size);
824 goto end;
825 }
826
827 ret = ff_nal_parse_units_buf(buf_in, &start, &size);
828 if (ret < 0)
829 goto end;
830
831 ret = 0;
832 buf = start;
833 end = start + size;
834
835 while (end - buf > 4) {
836 uint32_t len = FFMIN(AV_RB32(buf), end - buf - 4);
837 uint8_t type = (buf[5] >> 3);
838
839 buf += 4;
840
841 switch (type) {
842 case VVC_VPS_NUT:
843 case VVC_SPS_NUT:
844 case VVC_PPS_NUT:
845 num_ps++;
846 break;
847 default:
848 ret += 4 + len;
849 avio_wb32(pb, len);
850 avio_write(pb, buf, len);
851 break;
852 }
853
854 buf += len;
855 }
856
857 end:
858 av_free(start);
859 if (ps_count)
860 *ps_count = num_ps;
861 return ret;
862}
863
864int ff_vvc_annexb2mp4_buf(const uint8_t *buf_in, uint8_t **buf_out,
865 int *size, int filter_ps, int *ps_count)
866{
867 AVIOContext *pb;
868 int ret;
869
870 ret = avio_open_dyn_buf(&pb);
871 if (ret < 0)
872 return ret;
873
874 ret = ff_vvc_annexb2mp4(pb, buf_in, *size, filter_ps, ps_count);
875 if (ret < 0) {
877 return ret;
878 }
879
880 *size = avio_close_dyn_buf(pb, buf_out);
881
882 return 0;
883}
884
885int ff_isom_write_vvcc(AVIOContext *pb, const uint8_t *data,
886 int size, int ps_array_completeness)
887{
889 uint8_t *buf, *end, *start;
890 int ret;
891
892 if (size < 6) {
893 /* We can't write a valid vvcc from the provided data */
894 return AVERROR_INVALIDDATA;
895 } else if ((*data & 0xf8) == 0xf8) {
896 /* Data is already vvcc-formatted */
897 avio_write(pb, data, size);
898 return 0;
899 } else if (!(AV_RB24(data) == 1 || AV_RB32(data) == 1)) {
900 /* Not a valid Annex B start code prefix */
901 return AVERROR_INVALIDDATA;
902 }
903
904 ret = ff_nal_parse_units_buf(data, &start, &size);
905 if (ret < 0)
906 return ret;
907
908 vvcc_init(&vvcc);
909
910 buf = start;
911 end = start + size;
912
913 while (end - buf > 4) {
914 uint32_t len = FFMIN(AV_RB32(buf), end - buf - 4);
915 uint8_t type = (buf[5] >> 3);
916
917 buf += 4;
918
919 for (unsigned i = 0; i < FF_ARRAY_ELEMS(vvcc.arrays); i++) {
920 static const uint8_t array_idx_to_type[] =
923
924 if (type == array_idx_to_type[i]) {
925 ret = vvcc_add_nal_unit(buf, len, ps_array_completeness,
926 &vvcc, i);
927 if (ret < 0)
928 goto end;
929 break;
930 }
931 }
932
933 buf += len;
934 }
935
936 ret = vvcc_write(pb, &vvcc);
937
938 end:
939 vvcc_close(&vvcc);
940 av_free(start);
941 return ret;
942}
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
Buffered I/O operations.
void avio_w8(AVIOContext *s, int b)
Definition aviobuf.c:184
void avio_wb32(AVIOContext *s, unsigned int val)
Definition aviobuf.c:368
void avio_wb16(AVIOContext *s, unsigned int val)
Definition aviobuf.c:446
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition aviobuf.c:1369
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition aviobuf.c:206
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition aviobuf.c:1324
void ffio_free_dyn_buf(AVIOContext **s)
Free a dynamic buffer.
Definition aviobuf.c:1397
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define AV_CEIL_RSHIFT(a, b)
Definition common.h:60
#define av_ceil_log2
Definition common.h:97
#define NULL
Definition coverity.c:32
@ SPS_INDEX
Definition evc.c:31
@ PPS_INDEX
Definition evc.c:32
@ NB_ARRAYS
Definition evc.c:35
bitstream reader API header.
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition get_bits.h:424
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition get_bits.h:280
static unsigned int get_bits1(GetBitContext *s)
Definition get_bits.h:391
static void skip_bits(GetBitContext *s, int n)
Definition get_bits.h:383
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition get_bits.h:544
static const uint8_t * align_get_bits(GetBitContext *s)
Definition get_bits.h:560
static void skip_bits1(GetBitContext *s)
Definition get_bits.h:416
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition get_bits.h:337
exp golomb vlc stuff
static unsigned get_ue_golomb_long(GetBitContext *gb)
Read an unsigned Exp-Golomb code in the range 0 to UINT32_MAX-1.
Definition golomb.h:104
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition log.h:236
int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
Allocate, reallocate an array through a pointer to a pointer.
Definition mem.c:326
const H265RawProfileTierLevel * ptl
@ SEI_SUFFIX_INDEX
Definition hevc.c:39
@ SEI_PREFIX_INDEX
Definition hevc.c:38
@ VPS_INDEX
Definition hevc.c:35
#define AV_RB32(p)
#define AV_RB24(x)
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition j2kenc.c:154
uint32_t type
Definition jpegmpfenc.c:80
@ VVC_MAX_VPS_COUNT
Definition vvc.h:95
@ VVC_MAX_PPS_COUNT
Definition vvc.h:99
@ VVC_MAX_SPS_COUNT
Definition vvc.h:97
@ VVC_MAX_PTLS
Definition vvc.h:89
@ VVC_MAX_SUBLAYERS
Definition vvc.h:83
@ VVC_MAX_SUB_PROFILES
Definition vvc.h:102
@ VVC_SPS_NUT
Definition vvc.h:44
@ VVC_PREFIX_SEI_NUT
Definition vvc.h:52
@ VVC_OPI_NUT
Definition vvc.h:41
@ VVC_SUFFIX_SEI_NUT
Definition vvc.h:53
@ VVC_DCI_NUT
Definition vvc.h:42
@ VVC_VPS_NUT
Definition vvc.h:43
@ VVC_PPS_NUT
Definition vvc.h:45
internal header for H.266/VVC (de)muxer utilities
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
Memory handling functions.
const char data[16]
Definition mxf.c:149
int ff_nal_parse_units(AVIOContext *pb, const uint8_t *buf_in, int size)
Definition nal.c:113
int ff_nal_parse_units_buf(const uint8_t *buf_in, uint8_t **buf, int *size)
Definition nal.c:133
uint8_t * ff_nal_unit_extract_rbsp(const uint8_t *src, uint32_t src_len, uint32_t *dst_len, int header_len)
Definition nal.c:163
bitstream writer API
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition put_bits.h:62
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition put_bits.h:153
static int put_bytes_output(const PutBitContext *s)
Definition put_bits.h:99
#define FF_ARRAY_ELEMS(a)
Bytestream IO Context.
Definition avio.h:160
uint16_t num_nalus
Definition vvc.c:49
uint8_t array_completeness
Definition vvc.c:47
uint8_t ** nal_unit
Definition vvc.c:51
uint16_t * nal_unit_length
Definition vvc.c:50
uint8_t NAL_unit_type
Definition vvc.c:48
VVCCNALUnitArray arrays[NB_ARRAYS]
Definition vvc.c:81
uint16_t max_picture_height
Definition vvc.c:78
uint8_t general_profile_idc
Definition vvc.c:56
uint8_t ptl_num_sub_profiles
Definition vvc.c:64
uint8_t ptl_sublayer_level_present_flag[VVC_MAX_SUBLAYERS - 1]
Definition vvc.c:62
uint8_t ptl_frame_only_constraint_flag
Definition vvc.c:59
uint8_t general_level_idc
Definition vvc.c:58
uint8_t ptl_multilayer_enabled_flag
Definition vvc.c:60
uint8_t general_constraint_info[9]
Definition vvc.c:61
uint8_t sublayer_level_idc[VVC_MAX_SUBLAYERS - 1]
Definition vvc.c:63
uint32_t general_sub_profile_idc[VVC_MAX_SUB_PROFILES]
Definition vvc.c:65
uint8_t general_tier_flag
Definition vvc.c:57
uint8_t num_bytes_constraint_info
Definition vvc.c:55
#define av_free(p)
#define av_freep(p)
#define av_log(a,...)
static int array[MAX_W *MAX_W]
int size
int len
static int vvcc_add_nal_unit(uint8_t *nal_buf, uint32_t nal_size, int ps_array_completeness, VVCDecoderConfigurationRecord *vvcc, unsigned array_idx)
Definition vvc.c:501
static int vvcc_parse_sps(GetBitContext *gb, VVCDecoderConfigurationRecord *vvcc)
Definition vvc.c:333
static void vvcc_close(VVCDecoderConfigurationRecord *vvcc)
Definition vvc.c:562
int ff_vvc_annexb2mp4_buf(const uint8_t *buf_in, uint8_t **buf_out, int *size, int filter_ps, int *ps_count)
Writes Annex B formatted H.266/VVC NAL units to a data buffer.
Definition vvc.c:864
static int vvcc_array_add_nal_unit(uint8_t *nal_buf, uint32_t nal_size, uint8_t nal_type, int ps_array_completeness, VVCCNALUnitArray *array)
Definition vvc.c:450
static void vvcc_update_ptl(VVCDecoderConfigurationRecord *vvcc, VVCPTLRecord *ptl)
Definition vvc.c:84
int ff_isom_write_vvcc(AVIOContext *pb, const uint8_t *data, int size, int ps_array_completeness)
Writes H.266/VVC extradata (parameter sets, declarative SEI NAL units) to the provided AVIOContext.
Definition vvc.c:885
static void nal_unit_parse_header(GetBitContext *gb, uint8_t *nal_type)
Definition vvc.c:434
static int vvcc_parse_vps(GetBitContext *gb, VVCDecoderConfigurationRecord *vvcc)
Definition vvc.c:229
static void vvcc_parse_ptl(GetBitContext *gb, VVCDecoderConfigurationRecord *vvcc, unsigned int profileTierPresentFlag, unsigned int max_sub_layers_minus1)
Definition vvc.c:178
static int vvcc_write(AVIOContext *pb, VVCDecoderConfigurationRecord *vvcc)
Definition vvc.c:575
static int vvcc_parse_pps(GetBitContext *gb, VVCDecoderConfigurationRecord *vvcc)
Definition vvc.c:425
static void vvcc_init(VVCDecoderConfigurationRecord *vvcc)
Definition vvc.c:554
@ OPI_INDEX
Definition vvc.c:37
@ NB_ARRAYS
Definition vvc.c:43
int ff_vvc_annexb2mp4(AVIOContext *pb, const uint8_t *buf_in, int size, int filter_ps, int *ps_count)
Writes Annex B formatted H.266/VVC NAL units to the provided AVIOContext.
Definition vvc.c:816