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 - 1) {
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
249 /*
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.
256 */
257 vvcc->num_sublayers = FFMAX(vvcc->num_sublayers,
258 vps_max_sublayers_minus1 + 1);
259
260 if (vps_max_layers_minus1 > 0 && vps_max_sublayers_minus1 > 0)
261 vps_default_ptl_dpb_hrd_max_tid_flag = get_bits1(gb);
262 else
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);
266 else
267 vps_all_independent_layers_flag = 1;
268
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
278 }
279 }
280 }
281 }
282
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);
287 else
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);
293 else
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]
300 }
301 }
302 }
303 }
304 vps_num_ptls_minus1 = get_bits(gb, 8);
305 }
306
307 for (int i = 0; i <= vps_num_ptls_minus1; i++) {
308 if (i > 0)
309 vps_pt_present_flag[i] = get_bits1(gb);
310 else
311 vps_pt_present_flag[i] = 1;
312
313 if (!vps_default_ptl_dpb_hrd_max_tid_flag)
314 vps_ptl_max_tid[i] = get_bits(gb, 3);
315 else
316 vps_ptl_max_tid[i] = vps_max_sublayers_minus1;
317 }
318
319 align_get_bits(gb);
320
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;
324
325 /* nothing useful for vvcc past this point */
326 return 0;
327}
328
331{
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;
336
337 skip_bits(gb, 8); // sps_seq_parameter_set_id && sps_video_parameter_set_id
338 sps_max_sublayers_minus1 = get_bits(gb, 3);
339
340 /*
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.
347 */
348 vvcc->num_sublayers = FFMAX(vvcc->num_sublayers,
349 sps_max_sublayers_minus1 + 1);
350
351 vvcc->chroma_format_idc = get_bits(gb, 2);
352 sps_log2_ctu_size_minus5 = get_bits(gb, 2);
353
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);
357 }
358
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
362
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);
369
370 if (get_bits1(gb)) {
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
375 }
376
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);
389 }
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)
393 skip_bits(gb, wlen);
394 if (i > 0 && sps_pic_height_max_in_luma_samples > ctb_size_y)
395 skip_bits(gb, hlen);
397 skip_bits(gb, wlen);
399 skip_bits(gb, hlen);
400 }
401 if (!sps_independent_subpics_flag) {
402 skip_bits(gb, 2); // sps_subpic_treated_as_pic_flag && sps_loop_filter_across_subpic_enabled_flag
403 }
404 }
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]
410 }
411 }
412 }
414
415 /* nothing useful for vvcc past this point */
416 return 0;
417}
418
421{
422
423 // Nothing of importance to parse in PPS
424 /* nothing useful for vvcc past this point */
425 return 0;
426}
427
428static void nal_unit_parse_header(GetBitContext *gb, uint8_t *nal_type)
429{
430 /*
431 * forbidden_zero_bit u(1)
432 * nuh_reserved_zero_bit u(1)
433 * nuh_layer_id u(6)
434 */
435 skip_bits(gb, 8);
436 *nal_type = get_bits(gb, 5);
437
438 /*
439 * nuh_temporal_id_plus1 u(3)
440 */
441 skip_bits(gb, 3);
442}
443
444static int vvcc_array_add_nal_unit(uint8_t *nal_buf, uint32_t nal_size,
445 uint8_t nal_type, int ps_array_completeness,
447{
448 int ret;
449 uint16_t num_nalus;
450
451 num_nalus = array->num_nalus;
452
453 ret = av_reallocp_array(&array->nal_unit, num_nalus + 1, sizeof(uint8_t *));
454 if (ret < 0)
455 return ret;
456
457 ret =
458 av_reallocp_array(&array->nal_unit_length, num_nalus + 1,
459 sizeof(uint16_t));
460 if (ret < 0)
461 return ret;
462
463 array->nal_unit[num_nalus] = nal_buf;
464 array->nal_unit_length[num_nalus] = nal_size;
465 array->NAL_unit_type = nal_type;
466 array->num_nalus++;
467
468 /*
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,
471 * and PPS NAL units.
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.
487 */
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;
491
492 return 0;
493}
494
495static int vvcc_add_nal_unit(uint8_t *nal_buf, uint32_t nal_size,
496 int ps_array_completeness,
498 unsigned array_idx)
499{
500 int ret = 0;
501 GetBitContext gbc;
502 uint8_t nal_type;
503 uint8_t *rbsp_buf;
504 uint32_t rbsp_size;
505
506 rbsp_buf = ff_nal_unit_extract_rbsp(nal_buf, nal_size, &rbsp_size, 2);
507 if (!rbsp_buf) {
508 ret = AVERROR(ENOMEM);
509 goto end;
510 }
511
512 ret = init_get_bits8(&gbc, rbsp_buf, rbsp_size);
513 if (ret < 0)
514 goto end;
515
516 nal_unit_parse_header(&gbc, &nal_type);
517
518 /*
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?
522 */
523 ret = vvcc_array_add_nal_unit(nal_buf, nal_size, nal_type,
524 ps_array_completeness,
525 &vvcc->arrays[array_idx]);
526 if (ret < 0)
527 goto end;
528 if (vvcc->arrays[array_idx].num_nalus == 1)
529 vvcc->num_of_arrays++;
530
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) {
538 // not yet supported
539 }
540 if (ret < 0)
541 goto end;
542
543 end:
544 av_free(rbsp_buf);
545 return ret;
546}
547
549{
550 memset(vvcc, 0, sizeof(VVCDecoderConfigurationRecord));
551 vvcc->lengthSizeMinusOne = 3; // 4 bytes
554}
555
557{
558 for (unsigned i = 0; i < FF_ARRAY_ELEMS(vvcc->arrays); i++) {
559 VVCCNALUnitArray *const array = &vvcc->arrays[i];
560
561 array->num_nalus = 0;
562 av_freep(&array->nal_unit);
563 av_freep(&array->nal_unit_length);
564 }
565
566 vvcc->num_of_arrays = 0;
567}
568
570{
571 uint16_t vps_count = 0, sps_count = 0, pps_count = 0;
572 /*
573 * It's unclear how to properly compute these fields, so
574 * let's always set them to values meaning 'unspecified'.
575 */
576 vvcc->avg_frame_rate = 0;
577 vvcc->constant_frame_rate = 1;
578
580 "lengthSizeMinusOne: %" PRIu8 "\n",
581 vvcc->lengthSizeMinusOne);
583 "ptl_present_flag: %" PRIu8 "\n",
584 vvcc->ptl_present_flag);
586 "ols_idx: %" PRIu16 "\n", vvcc->ols_idx);
588 "num_sublayers: %" PRIu8 "\n",
589 vvcc->num_sublayers);
591 "constant_frame_rate: %" PRIu8 "\n",
592 vvcc->constant_frame_rate);
594 "chroma_format_idc: %" PRIu8 "\n",
595 vvcc->chroma_format_idc);
596
598 "bit_depth_minus8: %" PRIu8 "\n",
599 vvcc->bit_depth_minus8);
601 "num_bytes_constraint_info: %" PRIu8 "\n",
604 "general_profile_idc: %" PRIu8 "\n",
607 "general_tier_flag: %" PRIu8 "\n",
608 vvcc->ptl.general_tier_flag);
610 "general_level_idc: %" PRIu8 "\n",
611 vvcc->ptl.general_level_idc);
613 "ptl_frame_only_constraint_flag: %" PRIu8 "\n",
616 "ptl_multilayer_enabled_flag: %" PRIu8 "\n",
618 for (int i = 0; i < vvcc->ptl.num_bytes_constraint_info; i++) {
620 "general_constraint_info[%d]: %" PRIu8 "\n", i,
622 }
623
624 for (int i = 0; i < vvcc->num_sublayers - 1; i++) {
626 "ptl_sublayer_level_present_flag[%d]: %" PRIu8 "\n", i,
629 "sublayer_level_idc[%d]: %" PRIu8 "\n", i,
630 vvcc->ptl.sublayer_level_idc[i]);
631 }
632
634 "num_sub_profiles: %" PRIu8 "\n",
636
637 for (unsigned i = 0; i < vvcc->ptl.ptl_num_sub_profiles; i++) {
639 "general_sub_profile_idc[%u]: %" PRIx32 "\n", i,
641 }
642
644 "max_picture_width: %" PRIu16 "\n",
645 vvcc->max_picture_width);
647 "max_picture_height: %" PRIu16 "\n",
648 vvcc->max_picture_height);
650 "avg_frame_rate: %" PRIu16 "\n",
651 vvcc->avg_frame_rate);
652
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];
658
659 if (array->num_nalus == 0)
660 continue;
661
663 "array_completeness[%u]: %" PRIu8 "\n", i,
664 array->array_completeness);
666 "NAL_unit_type[%u]: %" PRIu8 "\n", i,
667 array->NAL_unit_type);
669 "num_nalus[%u]: %" PRIu16 "\n", i,
670 array->num_nalus);
671 for (unsigned j = 0; j < array->num_nalus; j++)
673 "nal_unit_length[%u][%u]: %"
674 PRIu16 "\n", i, j, array->nal_unit_length[j]);
675 }
676
677 /*
678 * We need at least one of each: SPS and PPS.
679 */
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;
689
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);
694
695 if (vvcc->ptl_present_flag) {
696 uint8_t buf[64];
697 PutBitContext pbc;
698
699 init_put_bits(&pbc, buf, sizeof(buf));
700 /*
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; */
705 avio_wb16(pb,
706 vvcc->ols_idx << 7 | vvcc->num_sublayers << 4 | vvcc->
707 constant_frame_rate << 2 | vvcc->chroma_format_idc);
708
709 /* unsigned int(3) bit_depth_minus8;
710 bit(5) reserved = ‘11111’b; */
711 avio_w8(pb, vvcc->bit_depth_minus8 << 5 | 0x1f);
712
713 //VVCPTLRecord
714
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);
718
719 /* unsigned int (7) general_profile_idc
720 unsigned int (1) general_tier_flag */
721 avio_w8(pb,
722 vvcc->ptl.general_profile_idc << 1 | vvcc->ptl.general_tier_flag);
723
724 /* unsigned int (8) general_level_idc */
725 avio_w8(pb, vvcc->ptl.general_level_idc);
726
727 /*
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 */
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));
739
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]);
746 }
747 avio_w8(pb, ptl_sublayer_level_present_flags);
748 }
749
750 for (int i = vvcc->num_sublayers - 2; i >= 0; i--) {
752 avio_w8(pb, vvcc->ptl.sublayer_level_idc[i]);
753 }
754
755 /* unsigned int(8) num_sub_profiles; */
757
758 for (int j = 0; j < vvcc->ptl.ptl_num_sub_profiles; j++) {
759 /* unsigned int(32) general_sub_profile_idc[j]; */
761 }
762
763 //End of VvcPTLRecord
764
765 /*
766 * unsigned int(16) max_picture_width;*/
767 avio_wb16(pb, vvcc->max_picture_width);
768
769 /*
770 * unsigned int(16) max_picture_height;*/
771 avio_wb16(pb, vvcc->max_picture_height);
772
773 /*
774 * unsigned int(16) avg_frame_rate; */
775 avio_wb16(pb, vvcc->avg_frame_rate);
776 }
777
778 /* unsigned int(8) num_of_arrays; */
779 avio_w8(pb, vvcc->num_of_arrays);
780
781 for (unsigned i = 0; i < FF_ARRAY_ELEMS(vvcc->arrays); i++) {
782 const VVCCNALUnitArray *const array = &vvcc->arrays[i];
783
784 if (!array->num_nalus)
785 continue;
786 /*
787 * bit(1) array_completeness;
788 * unsigned int(2) reserved = 0;
789 * unsigned int(5) NAL_unit_type;
790 */
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]);
800
801 /* bit(8*nal_unit_length) nal_unit; */
802 avio_write(pb, array->nal_unit[j],
803 array->nal_unit_length[j]);
804 }
805 }
806
807 return 0;
808}
809
810int ff_vvc_annexb2mp4(AVIOContext *pb, const uint8_t *buf_in,
811 int size, int filter_ps, int *ps_count)
812{
813 int num_ps = 0, ret = 0;
814 uint8_t *buf, *end, *start = NULL;
815
816 if (!filter_ps) {
817 ret = ff_nal_parse_units(pb, buf_in, size);
818 goto end;
819 }
820
821 ret = ff_nal_parse_units_buf(buf_in, &start, &size);
822 if (ret < 0)
823 goto end;
824
825 ret = 0;
826 buf = start;
827 end = start + size;
828
829 while (end - buf > 4) {
830 uint32_t len = FFMIN(AV_RB32(buf), end - buf - 4);
831 uint8_t type = (buf[5] >> 3);
832
833 buf += 4;
834
835 switch (type) {
836 case VVC_VPS_NUT:
837 case VVC_SPS_NUT:
838 case VVC_PPS_NUT:
839 num_ps++;
840 break;
841 default:
842 ret += 4 + len;
843 avio_wb32(pb, len);
844 avio_write(pb, buf, len);
845 break;
846 }
847
848 buf += len;
849 }
850
851 end:
852 av_free(start);
853 if (ps_count)
854 *ps_count = num_ps;
855 return ret;
856}
857
858int ff_vvc_annexb2mp4_buf(const uint8_t *buf_in, uint8_t **buf_out,
859 int *size, int filter_ps, int *ps_count)
860{
861 AVIOContext *pb;
862 int ret;
863
864 ret = avio_open_dyn_buf(&pb);
865 if (ret < 0)
866 return ret;
867
868 ret = ff_vvc_annexb2mp4(pb, buf_in, *size, filter_ps, ps_count);
869 if (ret < 0) {
871 return ret;
872 }
873
874 *size = avio_close_dyn_buf(pb, buf_out);
875
876 return 0;
877}
878
879int ff_isom_write_vvcc(AVIOContext *pb, const uint8_t *data,
880 int size, int ps_array_completeness)
881{
883 uint8_t *buf, *end, *start;
884 int ret;
885
886 if (size < 6) {
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);
892 return 0;
893 } else if (!(AV_RB24(data) == 1 || AV_RB32(data) == 1)) {
894 /* Not a valid Annex B start code prefix */
895 return AVERROR_INVALIDDATA;
896 }
897
898 ret = ff_nal_parse_units_buf(data, &start, &size);
899 if (ret < 0)
900 return ret;
901
902 vvcc_init(&vvcc);
903
904 buf = start;
905 end = start + size;
906
907 while (end - buf > 4) {
908 uint32_t len = FFMIN(AV_RB32(buf), end - buf - 4);
909 uint8_t type = (buf[5] >> 3);
910
911 buf += 4;
912
913 for (unsigned i = 0; i < FF_ARRAY_ELEMS(vvcc.arrays); i++) {
914 static const uint8_t array_idx_to_type[] =
917
918 if (type == array_idx_to_type[i]) {
919 ret = vvcc_add_nal_unit(buf, len, ps_array_completeness,
920 &vvcc, i);
921 if (ret < 0)
922 goto end;
923 break;
924 }
925 }
926
927 buf += len;
928 }
929
930 ret = vvcc_write(pb, &vvcc);
931
932 end:
933 vvcc_close(&vvcc);
934 av_free(start);
935 return ret;
936}
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:1410
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:1365
void ffio_free_dyn_buf(AVIOContext **s)
Free a dynamic buffer.
Definition aviobuf.c:1438
#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:225
const H265RawProfileTierLevel * ptl
@ SEI_SUFFIX_INDEX
Definition hevc.c:39
@ SEI_PREFIX_INDEX
Definition hevc.c:38
@ VPS_INDEX
Definition hevc.c:35
cl_device_type type
#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
@ 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:495
static int vvcc_parse_sps(GetBitContext *gb, VVCDecoderConfigurationRecord *vvcc)
Definition vvc.c:329
static void vvcc_close(VVCDecoderConfigurationRecord *vvcc)
Definition vvc.c:556
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:858
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:444
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:879
static void nal_unit_parse_header(GetBitContext *gb, uint8_t *nal_type)
Definition vvc.c:428
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:569
static int vvcc_parse_pps(GetBitContext *gb, VVCDecoderConfigurationRecord *vvcc)
Definition vvc.c:419
static void vvcc_init(VVCDecoderConfigurationRecord *vvcc)
Definition vvc.c:548
@ 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:810