FFmpeg
Loading...
Searching...
No Matches
hevc.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2014 Tim Walker <tdskywalker@gmail.com>
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include "libavcodec/get_bits.h"
22#include "libavcodec/golomb.h"
25#include "libavutil/mem.h"
26#include "avc.h"
27#include "avio.h"
28#include "avio_internal.h"
29#include "hevc.h"
30#include "nal.h"
31
32#define MAX_SPATIAL_SEGMENTATION 4096 // max. value of u(12) field
33
34enum {
41};
42
43
44#define FLAG_ARRAY_COMPLETENESS (1 << 0)
45#define FLAG_IS_NALFF (1 << 1)
46#define FLAG_IS_LHVC (1 << 2)
47
48typedef struct HVCCNALUnit {
49 uint8_t nuh_layer_id;
51 uint16_t nalUnitLength;
52 const uint8_t *nalUnit;
53
54 // VPS
57
64
88
97
100{
101 /*
102 * The value of general_profile_space in all the parameter sets must be
103 * identical.
104 */
105 hvcc->general_profile_space = ptl->profile_space;
106
107 /*
108 * The level indication general_level_idc must indicate a level of
109 * capability equal to or greater than the highest level indicated for the
110 * highest tier in all the parameter sets.
111 */
112 if (hvcc->general_tier_flag < ptl->tier_flag)
113 hvcc->general_level_idc = ptl->level_idc;
114 else
115 hvcc->general_level_idc = FFMAX(hvcc->general_level_idc, ptl->level_idc);
116
117 /*
118 * The tier indication general_tier_flag must indicate a tier equal to or
119 * greater than the highest tier indicated in all the parameter sets.
120 */
121 hvcc->general_tier_flag = FFMAX(hvcc->general_tier_flag, ptl->tier_flag);
122
123 /*
124 * The profile indication general_profile_idc must indicate a profile to
125 * which the stream associated with this configuration record conforms.
126 *
127 * If the sequence parameter sets are marked with different profiles, then
128 * the stream may need examination to determine which profile, if any, the
129 * entire stream conforms to. If the entire stream is not examined, or the
130 * examination reveals that there is no profile to which the entire stream
131 * conforms, then the entire stream must be split into two or more
132 * sub-streams with separate configuration records in which these rules can
133 * be met.
134 *
135 * Note: set the profile to the highest value for the sake of simplicity.
136 */
137 hvcc->general_profile_idc = FFMAX(hvcc->general_profile_idc, ptl->profile_idc);
138
139 /*
140 * Each bit in general_profile_compatibility_flags may only be set if all
141 * the parameter sets set that bit.
142 */
143 hvcc->general_profile_compatibility_flags &= ptl->profile_compatibility_flags;
144
145 /*
146 * Each bit in general_constraint_indicator_flags may only be set if all
147 * the parameter sets set that bit.
148 */
149 hvcc->general_constraint_indicator_flags &= ptl->constraint_indicator_flags;
150}
151
154 int profile_present_flag,
155 unsigned int max_sub_layers_minus1)
156{
157 unsigned int i;
158 HVCCProfileTierLevel general_ptl;
159 uint8_t sub_layer_profile_present_flag[HEVC_MAX_SUB_LAYERS];
160 uint8_t sub_layer_level_present_flag[HEVC_MAX_SUB_LAYERS];
161
162 if (profile_present_flag) {
163 general_ptl.profile_space = get_bits(gb, 2);
164 general_ptl.tier_flag = get_bits1(gb);
165 general_ptl.profile_idc = get_bits(gb, 5);
166 general_ptl.profile_compatibility_flags = get_bits_long(gb, 32);
167 general_ptl.constraint_indicator_flags = get_bits64(gb, 48);
168 general_ptl.level_idc = get_bits(gb, 8);
169 hvcc_update_ptl(hvcc, &general_ptl);
170 } else
171 skip_bits(gb, 8); // general_level_idc
172
173 for (i = 0; i < max_sub_layers_minus1; i++) {
174 sub_layer_profile_present_flag[i] = get_bits1(gb);
175 sub_layer_level_present_flag[i] = get_bits1(gb);
176 }
177
178 if (max_sub_layers_minus1 > 0)
179 for (i = max_sub_layers_minus1; i < 8; i++)
180 skip_bits(gb, 2); // reserved_zero_2bits[i]
181
182 for (i = 0; i < max_sub_layers_minus1; i++) {
183 if (sub_layer_profile_present_flag[i]) {
184 /*
185 * sub_layer_profile_space[i] u(2)
186 * sub_layer_tier_flag[i] u(1)
187 * sub_layer_profile_idc[i] u(5)
188 * sub_layer_profile_compatibility_flag[i][0..31] u(32)
189 * sub_layer_progressive_source_flag[i] u(1)
190 * sub_layer_interlaced_source_flag[i] u(1)
191 * sub_layer_non_packed_constraint_flag[i] u(1)
192 * sub_layer_frame_only_constraint_flag[i] u(1)
193 * sub_layer_reserved_zero_44bits[i] u(44)
194 */
195 skip_bits_long(gb, 32);
196 skip_bits_long(gb, 32);
197 skip_bits (gb, 24);
198 }
199
200 if (sub_layer_level_present_flag[i])
201 skip_bits(gb, 8);
202 }
203}
204
206 unsigned int cpb_cnt_minus1,
207 uint8_t sub_pic_hrd_params_present_flag)
208{
209 unsigned int i;
210
211 for (i = 0; i <= cpb_cnt_minus1; i++) {
212 get_ue_golomb_long(gb); // bit_rate_value_minus1
213 get_ue_golomb_long(gb); // cpb_size_value_minus1
214
215 if (sub_pic_hrd_params_present_flag) {
216 get_ue_golomb_long(gb); // cpb_size_du_value_minus1
217 get_ue_golomb_long(gb); // bit_rate_du_value_minus1
218 }
219
220 skip_bits1(gb); // cbr_flag
221 }
222}
223
224static int skip_hrd_parameters(GetBitContext *gb, uint8_t cprms_present_flag,
225 unsigned int max_sub_layers_minus1)
226{
227 unsigned int i;
228 uint8_t sub_pic_hrd_params_present_flag = 0;
229 uint8_t nal_hrd_parameters_present_flag = 0;
230 uint8_t vcl_hrd_parameters_present_flag = 0;
231
232 if (cprms_present_flag) {
233 nal_hrd_parameters_present_flag = get_bits1(gb);
234 vcl_hrd_parameters_present_flag = get_bits1(gb);
235
236 if (nal_hrd_parameters_present_flag ||
237 vcl_hrd_parameters_present_flag) {
238 sub_pic_hrd_params_present_flag = get_bits1(gb);
239
240 if (sub_pic_hrd_params_present_flag)
241 /*
242 * tick_divisor_minus2 u(8)
243 * du_cpb_removal_delay_increment_length_minus1 u(5)
244 * sub_pic_cpb_params_in_pic_timing_sei_flag u(1)
245 * dpb_output_delay_du_length_minus1 u(5)
246 */
247 skip_bits(gb, 19);
248
249 /*
250 * bit_rate_scale u(4)
251 * cpb_size_scale u(4)
252 */
253 skip_bits(gb, 8);
254
255 if (sub_pic_hrd_params_present_flag)
256 skip_bits(gb, 4); // cpb_size_du_scale
257
258 /*
259 * initial_cpb_removal_delay_length_minus1 u(5)
260 * au_cpb_removal_delay_length_minus1 u(5)
261 * dpb_output_delay_length_minus1 u(5)
262 */
263 skip_bits(gb, 15);
264 }
265 }
266
267 for (i = 0; i <= max_sub_layers_minus1; i++) {
268 unsigned int cpb_cnt_minus1 = 0;
269 uint8_t low_delay_hrd_flag = 0;
270 uint8_t fixed_pic_rate_within_cvs_flag = 0;
271 uint8_t fixed_pic_rate_general_flag = get_bits1(gb);
272
273 if (!fixed_pic_rate_general_flag)
274 fixed_pic_rate_within_cvs_flag = get_bits1(gb);
275
276 if (fixed_pic_rate_within_cvs_flag)
277 get_ue_golomb_long(gb); // elemental_duration_in_tc_minus1
278 else
279 low_delay_hrd_flag = get_bits1(gb);
280
281 if (!low_delay_hrd_flag) {
282 cpb_cnt_minus1 = get_ue_golomb_long(gb);
283 if (cpb_cnt_minus1 > 31)
284 return AVERROR_INVALIDDATA;
285 }
286
287 if (nal_hrd_parameters_present_flag)
288 skip_sub_layer_hrd_parameters(gb, cpb_cnt_minus1,
289 sub_pic_hrd_params_present_flag);
290
291 if (vcl_hrd_parameters_present_flag)
292 skip_sub_layer_hrd_parameters(gb, cpb_cnt_minus1,
293 sub_pic_hrd_params_present_flag);
294 }
295
296 return 0;
297}
298
300{
301 skip_bits_long(gb, 32); // num_units_in_tick
302 skip_bits_long(gb, 32); // time_scale
303
304 if (get_bits1(gb)) // poc_proportional_to_timing_flag
305 get_ue_golomb_long(gb); // num_ticks_poc_diff_one_minus1
306}
307
310 unsigned int max_sub_layers_minus1)
311{
312 unsigned int min_spatial_segmentation_idc;
313
314 if (get_bits1(gb)) // aspect_ratio_info_present_flag
315 if (get_bits(gb, 8) == 255) // aspect_ratio_idc
316 skip_bits_long(gb, 32); // sar_width u(16), sar_height u(16)
317
318 if (get_bits1(gb)) // overscan_info_present_flag
319 skip_bits1(gb); // overscan_appropriate_flag
320
321 if (get_bits1(gb)) { // video_signal_type_present_flag
322 skip_bits(gb, 4); // video_format u(3), video_full_range_flag u(1)
323
324 if (get_bits1(gb)) // colour_description_present_flag
325 /*
326 * colour_primaries u(8)
327 * transfer_characteristics u(8)
328 * matrix_coeffs u(8)
329 */
330 skip_bits(gb, 24);
331 }
332
333 if (get_bits1(gb)) { // chroma_loc_info_present_flag
334 get_ue_golomb_long(gb); // chroma_sample_loc_type_top_field
335 get_ue_golomb_long(gb); // chroma_sample_loc_type_bottom_field
336 }
337
338 /*
339 * neutral_chroma_indication_flag u(1)
340 * field_seq_flag u(1)
341 * frame_field_info_present_flag u(1)
342 */
343 skip_bits(gb, 3);
344
345 if (get_bits1(gb)) { // default_display_window_flag
346 get_ue_golomb_long(gb); // def_disp_win_left_offset
347 get_ue_golomb_long(gb); // def_disp_win_right_offset
348 get_ue_golomb_long(gb); // def_disp_win_top_offset
349 get_ue_golomb_long(gb); // def_disp_win_bottom_offset
350 }
351
352 if (get_bits1(gb)) { // vui_timing_info_present_flag
354
355 if (get_bits1(gb)) // vui_hrd_parameters_present_flag
356 skip_hrd_parameters(gb, 1, max_sub_layers_minus1);
357 }
358
359 if (get_bits1(gb)) { // bitstream_restriction_flag
360 /*
361 * tiles_fixed_structure_flag u(1)
362 * motion_vectors_over_pic_boundaries_flag u(1)
363 * restricted_ref_pic_lists_flag u(1)
364 */
365 skip_bits(gb, 3);
366
367 min_spatial_segmentation_idc = get_ue_golomb_long(gb);
368
369 /*
370 * unsigned int(12) min_spatial_segmentation_idc;
371 *
372 * The min_spatial_segmentation_idc indication must indicate a level of
373 * spatial segmentation equal to or less than the lowest level of
374 * spatial segmentation indicated in all the parameter sets.
375 */
377 min_spatial_segmentation_idc);
378
379 get_ue_golomb_long(gb); // max_bytes_per_pic_denom
380 get_ue_golomb_long(gb); // max_bits_per_min_cu_denom
381 get_ue_golomb_long(gb); // log2_max_mv_length_horizontal
382 get_ue_golomb_long(gb); // log2_max_mv_length_vertical
383 }
384}
385
387{
388 get_ue_golomb_long(gb); // max_dec_pic_buffering_minus1
389 get_ue_golomb_long(gb); // max_num_reorder_pics
390 get_ue_golomb_long(gb); // max_latency_increase_plus1
391}
392
395 uint8_t vps_max_layers_minus1,
396 uint8_t vps_base_layer_internal_flag)
397{
398 uint8_t num_scalability_types = 0;
399 uint8_t max_layers_minus_1 = FFMIN(62, vps_max_layers_minus1);
400 uint8_t splitting_flag, vps_nuh_layer_id_present_flag;
401 uint8_t scalability_mask_flag[16] = { 0 };
402 uint8_t dimension_id_len[16] = { 0 };
403 uint8_t layer_id_in_nuh[64] = { 0 };
404 int i, j;
405
406 if (vps_max_layers_minus1 > 0 && vps_base_layer_internal_flag)
407 hvcc_parse_ptl(gb, hvcc, 0, nal->vps_max_sub_layers_minus1);
408
409 splitting_flag = get_bits(gb, 1);
410
411 for (i = 0; i < 16; i++)
412 if (get_bits(gb, 1))
413 scalability_mask_flag[num_scalability_types++] = i;
414
415 for (j = 0; j < (num_scalability_types - splitting_flag); j++)
416 dimension_id_len[j] = get_bits(gb, 3) + 1;
417
418 vps_nuh_layer_id_present_flag = get_bits(gb, 1);
419
420 for (i = 1; i <= max_layers_minus_1; i++) {
421 if (vps_nuh_layer_id_present_flag)
422 layer_id_in_nuh[i] = get_bits(gb, 6);
423 else
424 layer_id_in_nuh[i] = i;
425
426 if (!splitting_flag) {
427 for (j = 0; j < num_scalability_types; j++) {
428 int dimension_id = get_bits(gb, dimension_id_len[j]);
429
430 if (dimension_id == 1 /* AUX_ALPHA */ && scalability_mask_flag[j] == 3 /* AuxId */)
431 hvcc->alpha_layer_nuh_id = layer_id_in_nuh[i];
432 }
433 }
434 }
435
436 if (splitting_flag) {
437 uint8_t dim_bit_offset[17] = { 0 };
438
439 dim_bit_offset[0] = 0;
440 for (j = 1; j < num_scalability_types; j++)
441 dim_bit_offset[j] = dim_bit_offset[j-1] + dimension_id_len[j-1];
442 dim_bit_offset[num_scalability_types] = 6;
443
444 if (num_scalability_types > 0 && dim_bit_offset[num_scalability_types - 1] >= 6)
445 return -1; // invalid bitstream
446
447 for (i = 1; i <= max_layers_minus_1; i++) {
448 for (j = 0; j < num_scalability_types; j++) {
449 int dimension_id = (layer_id_in_nuh[i] & ((1 << dim_bit_offset[j+1]) - 1)) >> dim_bit_offset[j];
450
451 if (dimension_id == 1 /* AUX_ALPHA */ && scalability_mask_flag[j] == 3 /* AuxId */)
452 hvcc->alpha_layer_nuh_id = layer_id_in_nuh[i];
453 }
454 }
455 }
456
457 return 0;
458}
459
462{
463 uint8_t vps_base_layer_internal_flag, vps_max_layers_minus1;
464 uint8_t vps_sub_layer_ordering_info_present_flag, vps_max_layer_id;
465 int vps_num_layer_sets_minus1;
466 int i;
467
468 nal->parameter_set_id = get_bits(gb, 4);
469
470 vps_base_layer_internal_flag = get_bits(gb, 1);
471 skip_bits(gb, 1); // vps_base_layer_available_flag
472 vps_max_layers_minus1 = get_bits(gb, 6);
473
474 nal->vps_max_sub_layers_minus1 = get_bits(gb, 3);
475
476 /*
477 * numTemporalLayers greater than 1 indicates that the stream to which this
478 * configuration record applies is temporally scalable and the contained
479 * number of temporal layers (also referred to as temporal sub-layer or
480 * sub-layer in ISO/IEC 23008-2) is equal to numTemporalLayers. Value 1
481 * indicates that the stream is not temporally scalable. Value 0 indicates
482 * that it is unknown whether the stream is temporally scalable.
483 */
485 nal->vps_max_sub_layers_minus1 + 1);
486
487 /*
488 * vps_temporal_id_nesting_flag u(1)
489 * vps_reserved_0xffff_16bits u(16)
490 */
491 skip_bits(gb, 17);
492
493 hvcc_parse_ptl(gb, hvcc, 1, nal->vps_max_sub_layers_minus1);
494
495 vps_sub_layer_ordering_info_present_flag = get_bits(gb, 1);
496 for (i = (vps_sub_layer_ordering_info_present_flag ? 0 : nal->vps_max_sub_layers_minus1);
497 i <= nal->vps_max_sub_layers_minus1; i++) {
498 get_ue_golomb(gb); // vps_max_dec_pic_buffering_minus1
499 get_ue_golomb(gb); // vps_max_num_reorder_pics
500 get_ue_golomb(gb); // vps_max_latency_increase_plus1
501 }
502
503 vps_max_layer_id = get_bits(gb, 6);
504 vps_num_layer_sets_minus1 = get_ue_golomb(gb);
505 skip_bits_long(gb, (vps_max_layer_id + 1) * vps_num_layer_sets_minus1); // layer_id_included_flag[i][j]
506
507 if (get_bits(gb, 1)) { // vps_timing_info_present_flag
508 int vps_num_hrd_parameters;
509
510 skip_bits_long(gb, 64); // vps_num_units_in_tick, vps_time_scale
511
512 if (get_bits(gb, 1)) // vps_poc_proportional_to_timing_flag
513 get_ue_golomb(gb); // vps_num_ticks_poc_diff_one_minus1
514
515 vps_num_hrd_parameters = get_ue_golomb(gb); // vps_num_hrd_parameters
516
517 for (i = 0; i < vps_num_hrd_parameters; i++) {
518 int cprms_present_flag;
519
520 get_ue_golomb(gb); // hrd_layer_set_idx[i]
521 if (i > 0)
522 cprms_present_flag = get_bits(gb, 1);
523 else
524 cprms_present_flag = 1;
525
526 skip_hrd_parameters(gb, cprms_present_flag, nal->vps_max_sub_layers_minus1);
527 }
528 }
529
530 if (get_bits(gb, 1)) { // vps_extension_flag
531 align_get_bits(gb);
532 if (hvcc_parse_vps_extension(gb, nal, hvcc,
533 vps_max_layers_minus1,
534 vps_base_layer_internal_flag) < 0)
535 return 0;
536 }
537
538 /* nothing useful for hvcC past this point */
539 return 0;
540}
541
543{
544 int i, j, k, num_coeffs;
545
546 for (i = 0; i < 4; i++)
547 for (j = 0; j < (i == 3 ? 2 : 6); j++)
548 if (!get_bits1(gb)) // scaling_list_pred_mode_flag[i][j]
549 get_ue_golomb_long(gb); // scaling_list_pred_matrix_id_delta[i][j]
550 else {
551 num_coeffs = FFMIN(64, 1 << (4 + (i << 1)));
552
553 if (i > 1)
554 get_se_golomb_long(gb); // scaling_list_dc_coef_minus8[i-2][j]
555
556 for (k = 0; k < num_coeffs; k++)
557 get_se_golomb_long(gb); // scaling_list_delta_coef
558 }
559}
560
561static int parse_rps(GetBitContext *gb, unsigned int rps_idx,
562 unsigned int num_rps,
563 unsigned int num_delta_pocs[HEVC_MAX_SHORT_TERM_REF_PIC_SETS])
564{
565 unsigned int i;
566
567 if (rps_idx && get_bits1(gb)) { // inter_ref_pic_set_prediction_flag
568 /* this should only happen for slice headers, and this isn't one */
569 if (rps_idx >= num_rps)
570 return AVERROR_INVALIDDATA;
571
572 skip_bits1 (gb); // delta_rps_sign
573 get_ue_golomb_long(gb); // abs_delta_rps_minus1
574
575 num_delta_pocs[rps_idx] = 0;
576
577 /*
578 * From libavcodec/hevc_ps.c:
579 *
580 * if (is_slice_header) {
581 * //foo
582 * } else
583 * rps_ridx = &sps->st_rps[rps - sps->st_rps - 1];
584 *
585 * where:
586 * rps: &sps->st_rps[rps_idx]
587 * sps->st_rps: &sps->st_rps[0]
588 * is_slice_header: rps_idx == num_rps
589 *
590 * thus:
591 * if (num_rps != rps_idx)
592 * rps_ridx = &sps->st_rps[rps_idx - 1];
593 *
594 * NumDeltaPocs[RefRpsIdx]: num_delta_pocs[rps_idx - 1]
595 */
596 for (i = 0; i <= num_delta_pocs[rps_idx - 1]; i++) {
597 uint8_t use_delta_flag = 0;
598 uint8_t used_by_curr_pic_flag = get_bits1(gb);
599 if (!used_by_curr_pic_flag)
600 use_delta_flag = get_bits1(gb);
601
602 if (used_by_curr_pic_flag || use_delta_flag)
603 num_delta_pocs[rps_idx]++;
604 }
605 } else {
606 unsigned int num_negative_pics = get_ue_golomb_long(gb);
607 unsigned int num_positive_pics = get_ue_golomb_long(gb);
608
609 if ((num_positive_pics + (uint64_t)num_negative_pics) * 2 > get_bits_left(gb))
610 return AVERROR_INVALIDDATA;
611
612 num_delta_pocs[rps_idx] = num_negative_pics + num_positive_pics;
613
614 for (i = 0; i < num_negative_pics; i++) {
615 get_ue_golomb_long(gb); // delta_poc_s0_minus1[rps_idx]
616 skip_bits1 (gb); // used_by_curr_pic_s0_flag[rps_idx]
617 }
618
619 for (i = 0; i < num_positive_pics; i++) {
620 get_ue_golomb_long(gb); // delta_poc_s1_minus1[rps_idx]
621 skip_bits1 (gb); // used_by_curr_pic_s1_flag[rps_idx]
622 }
623 }
624
625 return 0;
626}
627
630{
631 unsigned int i, sps_max_sub_layers_minus1, log2_max_pic_order_cnt_lsb_minus4;
632 unsigned int num_short_term_ref_pic_sets, num_delta_pocs[HEVC_MAX_SHORT_TERM_REF_PIC_SETS];
633 unsigned int sps_ext_or_max_sub_layers_minus1, multi_layer_ext_sps_flag;
634
635 unsigned int sps_video_parameter_set_id = get_bits(gb, 4);
636
637 if (nal->nuh_layer_id == 0) {
638 sps_ext_or_max_sub_layers_minus1 = 0;
639 sps_max_sub_layers_minus1 = get_bits(gb, 3);
640 } else {
641 sps_ext_or_max_sub_layers_minus1 = get_bits(gb, 3);
642 if (sps_ext_or_max_sub_layers_minus1 == 7) {
643 const HVCCNALUnitArray *array = &hvcc->arrays[VPS_INDEX];
644 const HVCCNALUnit *vps = NULL;
645
646 for (i = 0; i < array->numNalus; i++)
647 if (sps_video_parameter_set_id == array->nal[i].parameter_set_id) {
648 vps = &array->nal[i];
649 break;
650 }
651 if (!vps)
652 return AVERROR_INVALIDDATA;
653
654 sps_max_sub_layers_minus1 = vps->vps_max_sub_layers_minus1;
655 } else
656 sps_max_sub_layers_minus1 = sps_ext_or_max_sub_layers_minus1;
657 }
658 multi_layer_ext_sps_flag = nal->nuh_layer_id &&
659 sps_ext_or_max_sub_layers_minus1 == 7;
660
661 /*
662 * numTemporalLayers greater than 1 indicates that the stream to which this
663 * configuration record applies is temporally scalable and the contained
664 * number of temporal layers (also referred to as temporal sub-layer or
665 * sub-layer in ISO/IEC 23008-2) is equal to numTemporalLayers. Value 1
666 * indicates that the stream is not temporally scalable. Value 0 indicates
667 * that it is unknown whether the stream is temporally scalable.
668 */
670 sps_max_sub_layers_minus1 + 1);
671
672 if (!multi_layer_ext_sps_flag) {
673 hvcc->temporalIdNested = get_bits1(gb);
674 hvcc_parse_ptl(gb, hvcc, 1, sps_max_sub_layers_minus1);
675 }
676
677 nal->parameter_set_id = get_ue_golomb_long(gb);
678
679 if (multi_layer_ext_sps_flag) {
680 if (get_bits1(gb)) // update_rep_format_flag
681 skip_bits(gb, 8); // sps_rep_format_idx
682 } else {
684
685 if (hvcc->chromaFormat == 3)
686 skip_bits1(gb); // separate_colour_plane_flag
687
688 get_ue_golomb_long(gb); // pic_width_in_luma_samples
689 get_ue_golomb_long(gb); // pic_height_in_luma_samples
690
691 if (get_bits1(gb)) { // conformance_window_flag
692 get_ue_golomb_long(gb); // conf_win_left_offset
693 get_ue_golomb_long(gb); // conf_win_right_offset
694 get_ue_golomb_long(gb); // conf_win_top_offset
695 get_ue_golomb_long(gb); // conf_win_bottom_offset
696 }
697
700 }
701 log2_max_pic_order_cnt_lsb_minus4 = get_ue_golomb_long(gb);
702
703 if (!multi_layer_ext_sps_flag) {
704 /* sps_sub_layer_ordering_info_present_flag */
705 i = get_bits1(gb) ? 0 : sps_max_sub_layers_minus1;
706 for (; i <= sps_max_sub_layers_minus1; i++)
708 }
709
710 get_ue_golomb_long(gb); // log2_min_luma_coding_block_size_minus3
711 get_ue_golomb_long(gb); // log2_diff_max_min_luma_coding_block_size
712 get_ue_golomb_long(gb); // log2_min_transform_block_size_minus2
713 get_ue_golomb_long(gb); // log2_diff_max_min_transform_block_size
714 get_ue_golomb_long(gb); // max_transform_hierarchy_depth_inter
715 get_ue_golomb_long(gb); // max_transform_hierarchy_depth_intra
716
717 if (get_bits1(gb)) { // scaling_list_enabled_flag
718 int sps_infer_scaling_list_flag = 0;
719 if (multi_layer_ext_sps_flag)
720 sps_infer_scaling_list_flag = get_bits1(gb);
721 if (sps_infer_scaling_list_flag)
722 skip_bits(gb, 6); // sps_scaling_list_ref_layer_id
723 else if (get_bits1(gb)) // sps_scaling_list_data_present_flag
725 }
726
727 skip_bits1(gb); // amp_enabled_flag
728 skip_bits1(gb); // sample_adaptive_offset_enabled_flag
729
730 if (get_bits1(gb)) { // pcm_enabled_flag
731 skip_bits (gb, 4); // pcm_sample_bit_depth_luma_minus1
732 skip_bits (gb, 4); // pcm_sample_bit_depth_chroma_minus1
733 get_ue_golomb_long(gb); // log2_min_pcm_luma_coding_block_size_minus3
734 get_ue_golomb_long(gb); // log2_diff_max_min_pcm_luma_coding_block_size
735 skip_bits1 (gb); // pcm_loop_filter_disabled_flag
736 }
737
738 num_short_term_ref_pic_sets = get_ue_golomb_long(gb);
739 if (num_short_term_ref_pic_sets > HEVC_MAX_SHORT_TERM_REF_PIC_SETS)
740 return AVERROR_INVALIDDATA;
741
742 for (i = 0; i < num_short_term_ref_pic_sets; i++) {
743 int ret = parse_rps(gb, i, num_short_term_ref_pic_sets, num_delta_pocs);
744 if (ret < 0)
745 return ret;
746 }
747
748 if (get_bits1(gb)) { // long_term_ref_pics_present_flag
749 unsigned num_long_term_ref_pics_sps = get_ue_golomb_long(gb);
750 if (num_long_term_ref_pics_sps > 31U)
751 return AVERROR_INVALIDDATA;
752 for (i = 0; i < num_long_term_ref_pics_sps; i++) { // num_long_term_ref_pics_sps
753 int len = FFMIN(log2_max_pic_order_cnt_lsb_minus4 + 4, 16);
754 skip_bits (gb, len); // lt_ref_pic_poc_lsb_sps[i]
755 skip_bits1(gb); // used_by_curr_pic_lt_sps_flag[i]
756 }
757 }
758
759 skip_bits1(gb); // sps_temporal_mvp_enabled_flag
760 skip_bits1(gb); // strong_intra_smoothing_enabled_flag
761
762 if (get_bits1(gb)) // vui_parameters_present_flag
763 hvcc_parse_vui(gb, hvcc, sps_max_sub_layers_minus1);
764
765 /* nothing useful for hvcC past this point */
766 return 0;
767}
768
771{
772 uint8_t tiles_enabled_flag, entropy_coding_sync_enabled_flag;
773
774 nal->parameter_set_id = get_ue_golomb_long(gb); // pps_pic_parameter_set_id
775 get_ue_golomb_long(gb); // pps_seq_parameter_set_id
776
777 /*
778 * dependent_slice_segments_enabled_flag u(1)
779 * output_flag_present_flag u(1)
780 * num_extra_slice_header_bits u(3)
781 * sign_data_hiding_enabled_flag u(1)
782 * cabac_init_present_flag u(1)
783 */
784 skip_bits(gb, 7);
785
786 get_ue_golomb_long(gb); // num_ref_idx_l0_default_active_minus1
787 get_ue_golomb_long(gb); // num_ref_idx_l1_default_active_minus1
788 get_se_golomb_long(gb); // init_qp_minus26
789
790 /*
791 * constrained_intra_pred_flag u(1)
792 * transform_skip_enabled_flag u(1)
793 */
794 skip_bits(gb, 2);
795
796 if (get_bits1(gb)) // cu_qp_delta_enabled_flag
797 get_ue_golomb_long(gb); // diff_cu_qp_delta_depth
798
799 get_se_golomb_long(gb); // pps_cb_qp_offset
800 get_se_golomb_long(gb); // pps_cr_qp_offset
801
802 /*
803 * pps_slice_chroma_qp_offsets_present_flag u(1)
804 * weighted_pred_flag u(1)
805 * weighted_bipred_flag u(1)
806 * transquant_bypass_enabled_flag u(1)
807 */
808 skip_bits(gb, 4);
809
810 tiles_enabled_flag = get_bits1(gb);
811 entropy_coding_sync_enabled_flag = get_bits1(gb);
812
813 if (entropy_coding_sync_enabled_flag && tiles_enabled_flag)
814 hvcc->parallelismType = 0; // mixed-type parallel decoding
815 else if (entropy_coding_sync_enabled_flag)
816 hvcc->parallelismType = 3; // wavefront-based parallel decoding
817 else if (tiles_enabled_flag)
818 hvcc->parallelismType = 2; // tile-based parallel decoding
819 else
820 hvcc->parallelismType = 1; // slice-based parallel decoding
821
822 /* nothing useful for hvcC past this point */
823 return 0;
824}
825
826static void nal_unit_parse_header(GetBitContext *gb, uint8_t *nal_type,
827 uint8_t *nuh_layer_id)
828{
829 skip_bits1(gb); // forbidden_zero_bit
830
831 *nal_type = get_bits(gb, 6);
832 *nuh_layer_id = get_bits(gb, 6);
833
834 /*
835 * nuh_temporal_id_plus1 u(3)
836 */
837 skip_bits(gb, 3);
838}
839
840static int hvcc_array_add_nal_unit(const uint8_t *nal_buf, uint32_t nal_size,
842{
844 int ret;
845 uint16_t numNalus = array->numNalus;
846
847 if (numNalus >= UINT16_MAX)
848 return AVERROR_INVALIDDATA;
849
850 ret = av_reallocp_array(&array->nal, numNalus + 1, sizeof(*array->nal));
851 if (ret < 0)
852 return ret;
853
854 nal = &array->nal[numNalus];
855 nal->nalUnit = nal_buf;
856 nal->nalUnitLength = nal_size;
857 array->numNalus++;
858
859 return 0;
860}
861
862static int hvcc_add_nal_unit(const uint8_t *nal_buf, uint32_t nal_size,
864 int flags, unsigned array_idx)
865{
866 int ret = 0;
867 int is_nalff = !!(flags & FLAG_IS_NALFF);
868 int is_lhvc = !!(flags & FLAG_IS_LHVC);
869 int ps_array_completeness = !!(flags & FLAG_ARRAY_COMPLETENESS);
870 HVCCNALUnitArray *const array = &hvcc->arrays[array_idx];
872 GetBitContext gbc;
873 uint8_t nal_type, nuh_layer_id;
874 uint8_t *rbsp_buf;
875 uint32_t rbsp_size;
876
877 rbsp_buf = ff_nal_unit_extract_rbsp(nal_buf, nal_size, &rbsp_size, 2);
878 if (!rbsp_buf) {
879 ret = AVERROR(ENOMEM);
880 goto end;
881 }
882
883 ret = init_get_bits8(&gbc, rbsp_buf, rbsp_size);
884 if (ret < 0)
885 goto end;
886
887 nal_unit_parse_header(&gbc, &nal_type, &nuh_layer_id);
888 if (!is_lhvc && nuh_layer_id > 0 && nuh_layer_id != hvcc->alpha_layer_nuh_id)
889 goto end;
890
891 /*
892 * Note: only 'declarative' SEI messages are allowed in
893 * hvcC. Perhaps the SEI playload type should be checked
894 * and non-declarative SEI messages discarded?
895 */
896 ret = hvcc_array_add_nal_unit(nal_buf, nal_size, array);
897 if (ret < 0)
898 goto end;
899 if (array->numNalus == 1) {
900 hvcc->numOfArrays++;
901 array->NAL_unit_type = nal_type;
902
903 /*
904 * When the sample entry name is ‘hvc1’, the default and mandatory value of
905 * array_completeness is 1 for arrays of all types of parameter sets, and 0
906 * for all other arrays. When the sample entry name is ‘hev1’, the default
907 * value of array_completeness is 0 for all arrays.
908 */
909 if (nal_type == HEVC_NAL_VPS || nal_type == HEVC_NAL_SPS ||
910 nal_type == HEVC_NAL_PPS)
911 array->array_completeness = ps_array_completeness;
912 }
913
914 nal = &array->nal[array->numNalus-1];
915 nal->nuh_layer_id = nuh_layer_id;
916
917 /* Don't parse parameter sets. We already have the needed information*/
918 if (is_nalff)
919 goto end;
920
921 if (nal_type == HEVC_NAL_VPS)
922 ret = hvcc_parse_vps(&gbc, nal, hvcc);
923 else if (nal_type == HEVC_NAL_SPS)
924 ret = hvcc_parse_sps(&gbc, nal, hvcc);
925 else if (nal_type == HEVC_NAL_PPS)
926 ret = hvcc_parse_pps(&gbc, nal, hvcc);
927 if (ret < 0)
928 goto end;
929
930end:
931 av_free(rbsp_buf);
932 return ret;
933}
934
936{
937 memset(hvcc, 0, sizeof(HEVCDecoderConfigurationRecord));
938 hvcc->configurationVersion = 1;
939 hvcc->lengthSizeMinusOne = 3; // 4 bytes
940
941 /*
942 * The following fields have all their valid bits set by default,
943 * the ProfileTierLevel parsing code will unset them when needed.
944 */
945 hvcc->general_profile_compatibility_flags = 0xffffffff;
946 hvcc->general_constraint_indicator_flags = 0xffffffffffff;
947
948 /*
949 * Initialize this field with an invalid value which can be used to detect
950 * whether we didn't see any VUI (in which case it should be reset to zero).
951 */
953}
954
956{
957 for (unsigned i = 0; i < FF_ARRAY_ELEMS(hvcc->arrays); i++) {
958 HVCCNALUnitArray *const array = &hvcc->arrays[i];
959 array->numNalus = 0;
960 av_freep(&array->nal);
961 }
962}
963
964static int hvcc_write(void *logctx, AVIOContext *pb,
966{
967 uint16_t numNalus[NB_ARRAYS] = { 0 };
968 int is_lhvc = !!(flags & FLAG_IS_LHVC);
969 int numOfArrays = 0;
970
971 /*
972 * We only support writing HEVCDecoderConfigurationRecord version 1.
973 */
974 hvcc->configurationVersion = 1;
975
976 /*
977 * If min_spatial_segmentation_idc is invalid, reset to 0 (unspecified).
978 */
981
982 /*
983 * parallelismType indicates the type of parallelism that is used to meet
984 * the restrictions imposed by min_spatial_segmentation_idc when the value
985 * of min_spatial_segmentation_idc is greater than 0.
986 */
988 hvcc->parallelismType = 0;
989
990 /*
991 * It's unclear how to properly compute these fields, so
992 * let's always set them to values meaning 'unspecified'.
993 */
994 hvcc->avgFrameRate = 0;
995 /*
996 * lhvC doesn't store this field. It instead reserves the bits, setting them
997 * to '11'b.
998 */
999 hvcc->constantFrameRate = is_lhvc * 0x3;
1000
1001 /*
1002 * Skip all NALUs with nuh_layer_id == 0 if writing lhvC. We do it here and
1003 * not before parsing them as some parameter sets with nuh_layer_id > 0
1004 * may reference base layer parameters sets.
1005 */
1006 for (unsigned i = 0; i < FF_ARRAY_ELEMS(hvcc->arrays); i++) {
1007 const HVCCNALUnitArray *const array = &hvcc->arrays[i];
1008
1009 if (array->numNalus == 0)
1010 continue;
1011
1012 for (unsigned j = 0; j < array->numNalus; j++)
1013 numNalus[i] += !is_lhvc || (array->nal[j].nuh_layer_id != 0);
1014 numOfArrays += (numNalus[i] > 0);
1015 }
1016
1017 av_log(logctx, AV_LOG_TRACE, "%s\n", is_lhvc ? "lhvC" : "hvcC");
1018 av_log(logctx, AV_LOG_TRACE, "configurationVersion: %"PRIu8"\n",
1019 hvcc->configurationVersion);
1020 if (!is_lhvc) {
1021 av_log(logctx, AV_LOG_TRACE, "general_profile_space: %"PRIu8"\n",
1022 hvcc->general_profile_space);
1023 av_log(logctx, AV_LOG_TRACE, "general_tier_flag: %"PRIu8"\n",
1024 hvcc->general_tier_flag);
1025 av_log(logctx, AV_LOG_TRACE, "general_profile_idc: %"PRIu8"\n",
1026 hvcc->general_profile_idc);
1027 av_log(logctx, AV_LOG_TRACE, "general_profile_compatibility_flags: 0x%08"PRIx32"\n",
1029 av_log(logctx, AV_LOG_TRACE, "general_constraint_indicator_flags: 0x%012"PRIx64"\n",
1031 av_log(logctx, AV_LOG_TRACE, "general_level_idc: %"PRIu8"\n",
1032 hvcc->general_level_idc);
1033 }
1034 av_log(logctx, AV_LOG_TRACE, "min_spatial_segmentation_idc: %"PRIu16"\n",
1036 av_log(logctx, AV_LOG_TRACE, "parallelismType: %"PRIu8"\n",
1037 hvcc->parallelismType);
1038 if (!is_lhvc) {
1039 av_log(logctx, AV_LOG_TRACE, "chromaFormat: %"PRIu8"\n",
1040 hvcc->chromaFormat);
1041 av_log(logctx, AV_LOG_TRACE, "bitDepthLumaMinus8: %"PRIu8"\n",
1042 hvcc->bitDepthLumaMinus8);
1043 av_log(logctx, AV_LOG_TRACE, "bitDepthChromaMinus8: %"PRIu8"\n",
1044 hvcc->bitDepthChromaMinus8);
1045 av_log(logctx, AV_LOG_TRACE, "avgFrameRate: %"PRIu16"\n",
1046 hvcc->avgFrameRate);
1047 av_log(logctx, AV_LOG_TRACE, "constantFrameRate: %"PRIu8"\n",
1048 hvcc->constantFrameRate);
1049 }
1050 av_log(logctx, AV_LOG_TRACE, "numTemporalLayers: %"PRIu8"\n",
1051 hvcc->numTemporalLayers);
1052 av_log(logctx, AV_LOG_TRACE, "temporalIdNested: %"PRIu8"\n",
1053 hvcc->temporalIdNested);
1054 av_log(logctx, AV_LOG_TRACE, "lengthSizeMinusOne: %"PRIu8"\n",
1055 hvcc->lengthSizeMinusOne);
1056 av_log(logctx, AV_LOG_TRACE, "numOfArrays: %d\n",
1057 numOfArrays);
1058 for (unsigned i = 0, j = 0; i < FF_ARRAY_ELEMS(hvcc->arrays); i++) {
1059 const HVCCNALUnitArray *const array = &hvcc->arrays[i];
1060
1061 if (numNalus[i] == 0)
1062 continue;
1063
1064 av_log(logctx, AV_LOG_TRACE, "array_completeness[%u]: %"PRIu8"\n",
1065 j, array->array_completeness);
1066 av_log(logctx, AV_LOG_TRACE, "NAL_unit_type[%u]: %"PRIu8"\n",
1067 j, array->NAL_unit_type);
1068 av_log(logctx, AV_LOG_TRACE, "numNalus[%u]: %"PRIu16"\n",
1069 j, numNalus[i]);
1070 for (unsigned k = 0; k < array->numNalus; k++) {
1071 if (is_lhvc && array->nal[k].nuh_layer_id == 0)
1072 continue;
1073
1074 av_log(logctx, AV_LOG_TRACE,
1075 "nuh_layer_id[%u][%u]: %"PRIu8"\n",
1076 j, k, array->nal[k].nuh_layer_id);
1077 av_log(logctx, AV_LOG_TRACE,
1078 "nalUnitLength[%u][%u]: %"PRIu16"\n",
1079 j, k, array->nal[k].nalUnitLength);
1080 }
1081 j++;
1082 }
1083
1084 /*
1085 * We need at least one of each: VPS, SPS and PPS.
1086 */
1088 (!numNalus[VPS_INDEX] || numNalus[VPS_INDEX] > HEVC_MAX_VPS_COUNT) && !is_lhvc)
1089 return AVERROR_INVALIDDATA;
1091 (!numNalus[SPS_INDEX] || numNalus[SPS_INDEX] > HEVC_MAX_SPS_COUNT ||
1092 !numNalus[PPS_INDEX] || numNalus[PPS_INDEX] > HEVC_MAX_PPS_COUNT))
1093 return AVERROR_INVALIDDATA;
1094
1095 /* unsigned int(8) configurationVersion = 1; */
1096 avio_w8(pb, hvcc->configurationVersion);
1097
1098 if (!is_lhvc) {
1099 /*
1100 * unsigned int(2) general_profile_space;
1101 * unsigned int(1) general_tier_flag;
1102 * unsigned int(5) general_profile_idc;
1103 */
1104 avio_w8(pb, hvcc->general_profile_space << 6 |
1105 hvcc->general_tier_flag << 5 |
1106 hvcc->general_profile_idc);
1107
1108 /* unsigned int(32) general_profile_compatibility_flags; */
1110
1111 /* unsigned int(48) general_constraint_indicator_flags; */
1114
1115 /* unsigned int(8) general_level_idc; */
1116 avio_w8(pb, hvcc->general_level_idc);
1117 }
1118
1119 /*
1120 * bit(4) reserved = '1111'b;
1121 * unsigned int(12) min_spatial_segmentation_idc;
1122 */
1123 avio_wb16(pb, hvcc->min_spatial_segmentation_idc | 0xf000);
1124
1125 /*
1126 * bit(6) reserved = '111111'b;
1127 * unsigned int(2) parallelismType;
1128 */
1129 avio_w8(pb, hvcc->parallelismType | 0xfc);
1130
1131 if (!is_lhvc) {
1132 /*
1133 * bit(6) reserved = '111111'b;
1134 * unsigned int(2) chromaFormat;
1135 */
1136 avio_w8(pb, hvcc->chromaFormat | 0xfc);
1137
1138 /*
1139 * bit(5) reserved = '11111'b;
1140 * unsigned int(3) bitDepthLumaMinus8;
1141 */
1142 avio_w8(pb, hvcc->bitDepthLumaMinus8 | 0xf8);
1143
1144 /*
1145 * bit(5) reserved = '11111'b;
1146 * unsigned int(3) bitDepthChromaMinus8;
1147 */
1148 avio_w8(pb, hvcc->bitDepthChromaMinus8 | 0xf8);
1149
1150 /* bit(16) avgFrameRate; */
1151 avio_wb16(pb, hvcc->avgFrameRate);
1152 }
1153
1154 /*
1155 * if (!is_lhvc)
1156 * bit(2) constantFrameRate;
1157 * else
1158 * bit(2) reserved = '11'b;
1159 * bit(3) numTemporalLayers;
1160 * bit(1) temporalIdNested;
1161 * unsigned int(2) lengthSizeMinusOne;
1162 */
1163 avio_w8(pb, hvcc->constantFrameRate << 6 |
1164 hvcc->numTemporalLayers << 3 |
1165 hvcc->temporalIdNested << 2 |
1166 hvcc->lengthSizeMinusOne);
1167
1168 /* unsigned int(8) numOfArrays; */
1169 avio_w8(pb, numOfArrays);
1170
1171 for (unsigned i = 0; i < FF_ARRAY_ELEMS(hvcc->arrays); i++) {
1172 const HVCCNALUnitArray *const array = &hvcc->arrays[i];
1173
1174 if (!numNalus[i])
1175 continue;
1176 /*
1177 * bit(1) array_completeness;
1178 * unsigned int(1) reserved = 0;
1179 * unsigned int(6) NAL_unit_type;
1180 */
1181 avio_w8(pb, array->array_completeness << 7 |
1182 array->NAL_unit_type & 0x3f);
1183
1184 /* unsigned int(16) numNalus; */
1185 avio_wb16(pb, numNalus[i]);
1186
1187 for (unsigned j = 0; j < array->numNalus; j++) {
1188 HVCCNALUnit *nal = &array->nal[j];
1189
1190 if (is_lhvc && nal->nuh_layer_id == 0)
1191 continue;
1192
1193 /* unsigned int(16) nalUnitLength; */
1194 avio_wb16(pb, nal->nalUnitLength);
1195
1196 /* bit(8*nalUnitLength) nalUnit; */
1197 avio_write(pb, nal->nalUnit, nal->nalUnitLength);
1198 }
1199 }
1200
1201 return 0;
1202}
1203
1204int ff_hevc_annexb2mp4(AVIOContext *pb, const uint8_t *buf_in,
1205 int size, int filter_ps, int *ps_count)
1206{
1207 int num_ps = 0, ret = 0;
1208 uint8_t *buf, *end, *start = NULL;
1209
1210 if (!filter_ps) {
1211 ret = ff_nal_parse_units(pb, buf_in, size);
1212 goto end;
1213 }
1214
1215 ret = ff_nal_parse_units_buf(buf_in, &start, &size);
1216 if (ret < 0)
1217 goto end;
1218
1219 ret = 0;
1220 buf = start;
1221 end = start + size;
1222
1223 while (end - buf > 4) {
1224 uint32_t len = FFMIN(AV_RB32(buf), end - buf - 4);
1225 uint8_t type = (buf[4] >> 1) & 0x3f;
1226
1227 buf += 4;
1228
1229 switch (type) {
1230 case HEVC_NAL_VPS:
1231 case HEVC_NAL_SPS:
1232 case HEVC_NAL_PPS:
1233 num_ps++;
1234 break;
1235 default:
1236 ret += 4 + len;
1237 avio_wb32(pb, len);
1238 avio_write(pb, buf, len);
1239 break;
1240 }
1241
1242 buf += len;
1243 }
1244
1245end:
1246 av_free(start);
1247 if (ps_count)
1248 *ps_count = num_ps;
1249 return ret;
1250}
1251
1252int ff_hevc_annexb2mp4_buf(const uint8_t *buf_in, uint8_t **buf_out,
1253 int *size, int filter_ps, int *ps_count)
1254{
1255 AVIOContext *pb;
1256 int ret;
1257
1258 ret = avio_open_dyn_buf(&pb);
1259 if (ret < 0)
1260 return ret;
1261
1262 ret = ff_hevc_annexb2mp4(pb, buf_in, *size, filter_ps, ps_count);
1263 if (ret < 0) {
1264 ffio_free_dyn_buf(&pb);
1265 return ret;
1266 }
1267
1268 *size = avio_close_dyn_buf(pb, buf_out);
1269
1270 return 0;
1271}
1272
1273static int hvcc_parse_nal_unit(const uint8_t *buf, uint32_t len, int type,
1275 int flags)
1276{
1277 for (unsigned i = 0; i < FF_ARRAY_ELEMS(hvcc->arrays); i++) {
1278 static const uint8_t array_idx_to_type[] =
1281
1282 if (type == array_idx_to_type[i]) {
1283 int ret = hvcc_add_nal_unit(buf, len, hvcc, flags, i);
1284 if (ret < 0)
1285 return ret;
1286 break;
1287 }
1288 }
1289
1290 return 0;
1291}
1292
1293static int write_configuration_record(void *logctx, AVIOContext *pb, const uint8_t *data,
1294 int size, int flags)
1295{
1297 uint8_t *buf, *end, *start = NULL;
1298 int ret;
1299
1300 if (size < 6) {
1301 /* We can't write a valid hvcC from the provided data */
1302 return AVERROR_INVALIDDATA;
1303 } else if (*data == 1) {
1304 /* Data is already hvcC-formatted. Parse the arrays to skip any NALU
1305 with nuh_layer_id > 0 */
1306 GetBitContext gbc;
1307 int num_arrays;
1308
1309 if (size < 23)
1310 return AVERROR_INVALIDDATA;
1311
1312 ret = init_get_bits8(&gbc, data, size);
1313 if (ret < 0)
1314 return ret;
1315
1316 hvcc_init(&hvcc);
1317 skip_bits(&gbc, 8); // hvcc.configurationVersion
1318 hvcc.general_profile_space = get_bits(&gbc, 2);
1319 hvcc.general_tier_flag = get_bits1(&gbc);
1320 hvcc.general_profile_idc = get_bits(&gbc, 5);
1323 hvcc.general_level_idc = get_bits(&gbc, 8);
1324 skip_bits(&gbc, 4); // reserved
1325 hvcc.min_spatial_segmentation_idc = get_bits(&gbc, 12);
1326 skip_bits(&gbc, 6); // reserved
1327 hvcc.parallelismType = get_bits(&gbc, 2);
1328 skip_bits(&gbc, 6); // reserved
1329 hvcc.chromaFormat = get_bits(&gbc, 2);
1330 skip_bits(&gbc, 5); // reserved
1331 hvcc.bitDepthLumaMinus8 = get_bits(&gbc, 3);
1332 skip_bits(&gbc, 5); // reserved
1333 hvcc.bitDepthChromaMinus8 = get_bits(&gbc, 3);
1334 hvcc.avgFrameRate = get_bits(&gbc, 16);
1335 hvcc.constantFrameRate = get_bits(&gbc, 2);
1336 hvcc.numTemporalLayers = get_bits(&gbc, 3);
1337 hvcc.temporalIdNested = get_bits1(&gbc);
1338 hvcc.lengthSizeMinusOne = get_bits(&gbc, 2);
1339
1341
1342 num_arrays = get_bits(&gbc, 8);
1343 for (int i = 0; i < num_arrays; i++) {
1344 int type, num_nalus;
1345
1346 skip_bits(&gbc, 2);
1347 type = get_bits(&gbc, 6);
1348 num_nalus = get_bits(&gbc, 16);
1349 for (int j = 0; j < num_nalus; j++) {
1350 int len = get_bits(&gbc, 16);
1351
1352 if (len > (get_bits_left(&gbc) / 8))
1353 goto end;
1354
1355 ret = hvcc_parse_nal_unit(data + get_bits_count(&gbc) / 8,
1356 len, type, &hvcc, flags);
1357 if (ret < 0)
1358 goto end;
1359
1360 skip_bits_long(&gbc, len * 8);
1361 }
1362 }
1363
1364 ret = hvcc_write(logctx, pb, &hvcc, flags);
1365 goto end;
1366 } else if (!(AV_RB24(data) == 1 || AV_RB32(data) == 1)) {
1367 /* Not a valid Annex B start code prefix */
1368 return AVERROR_INVALIDDATA;
1369 }
1370
1371 ret = ff_nal_parse_units_buf(data, &start, &size);
1372 if (ret < 0)
1373 return ret;
1374
1375 hvcc_init(&hvcc);
1376
1377 buf = start;
1378 end = start + size;
1379
1380 while (end - buf > 4) {
1381 uint32_t len = FFMIN(AV_RB32(buf), end - buf - 4);
1382 uint8_t type = (buf[4] >> 1) & 0x3f;
1383
1384 buf += 4;
1385
1386 ret = hvcc_parse_nal_unit(buf, len, type, &hvcc, flags);
1387 if (ret < 0)
1388 goto end;
1389
1390 buf += len;
1391 }
1392
1393 ret = hvcc_write(logctx, pb, &hvcc, flags);
1394
1395end:
1396 hvcc_close(&hvcc);
1397 av_free(start);
1398 return ret;
1399}
1400
1401int ff_isom_write_hvcc(AVIOContext *pb, const uint8_t *data,
1402 int size, int ps_array_completeness, void *logctx)
1403{
1404 return write_configuration_record(logctx, pb, data, size,
1405 !!ps_array_completeness * FLAG_ARRAY_COMPLETENESS);
1406}
1407
1408int ff_isom_write_lhvc(AVIOContext *pb, const uint8_t *data,
1409 int size, int ps_array_completeness, void *logctx)
1410{
1411 return write_configuration_record(logctx, pb, data, size,
1412 (!!ps_array_completeness * FLAG_ARRAY_COMPLETENESS) | FLAG_IS_LHVC);
1413}
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 flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
static int FUNC vps(CodedBitstreamContext *ctx, RWContext *rw, H265RawVPS *current)
static int FUNC nal(CodedBitstreamContext *ctx, RWContext *rw, LCEVCRawNAL *current, int nal_unit_type)
#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 int get_bits_left(GetBitContext *gb)
Definition get_bits.h:688
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 uint64_t get_bits64(GetBitContext *s, int n)
Read 0-64 bits.
Definition get_bits.h:456
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 int get_bits_count(const GetBitContext *s)
Definition get_bits.h:254
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 int get_ue_golomb(GetBitContext *gb)
Read an unsigned Exp-Golomb code in the range 0 to 8190.
Definition golomb.h:53
static int get_se_golomb_long(GetBitContext *gb)
Definition golomb.h:294
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
#define MAX_SPATIAL_SEGMENTATION
Definition hevc.c:32
#define FLAG_IS_NALFF
Definition hevc.c:45
static int hvcc_parse_vps_extension(GetBitContext *gb, HVCCNALUnit *nal, HEVCDecoderConfigurationRecord *hvcc, uint8_t vps_max_layers_minus1, uint8_t vps_base_layer_internal_flag)
Definition hevc.c:393
static int hvcc_parse_nal_unit(const uint8_t *buf, uint32_t len, int type, HEVCDecoderConfigurationRecord *hvcc, int flags)
Definition hevc.c:1273
static void hvcc_close(HEVCDecoderConfigurationRecord *hvcc)
Definition hevc.c:955
static void skip_sub_layer_ordering_info(GetBitContext *gb)
Definition hevc.c:386
static void nal_unit_parse_header(GetBitContext *gb, uint8_t *nal_type, uint8_t *nuh_layer_id)
Definition hevc.c:826
int ff_isom_write_lhvc(AVIOContext *pb, const uint8_t *data, int size, int ps_array_completeness, void *logctx)
Writes L-HEVC extradata (parameter sets with nuh_layer_id > 0, as a LHEVCDecoderConfigurationRecord) ...
Definition hevc.c:1408
int ff_hevc_annexb2mp4_buf(const uint8_t *buf_in, uint8_t **buf_out, int *size, int filter_ps, int *ps_count)
Writes Annex B formatted HEVC NAL units to a data buffer.
Definition hevc.c:1252
int ff_hevc_annexb2mp4(AVIOContext *pb, const uint8_t *buf_in, int size, int filter_ps, int *ps_count)
Writes Annex B formatted HEVC NAL units to the provided AVIOContext.
Definition hevc.c:1204
static void hvcc_update_ptl(HEVCDecoderConfigurationRecord *hvcc, HVCCProfileTierLevel *ptl)
Definition hevc.c:98
static int parse_rps(GetBitContext *gb, unsigned int rps_idx, unsigned int num_rps, unsigned int num_delta_pocs[HEVC_MAX_SHORT_TERM_REF_PIC_SETS])
Definition hevc.c:561
static void hvcc_parse_ptl(GetBitContext *gb, HEVCDecoderConfigurationRecord *hvcc, int profile_present_flag, unsigned int max_sub_layers_minus1)
Definition hevc.c:152
static void skip_sub_layer_hrd_parameters(GetBitContext *gb, unsigned int cpb_cnt_minus1, uint8_t sub_pic_hrd_params_present_flag)
Definition hevc.c:205
static int hvcc_add_nal_unit(const uint8_t *nal_buf, uint32_t nal_size, HEVCDecoderConfigurationRecord *hvcc, int flags, unsigned array_idx)
Definition hevc.c:862
static int skip_hrd_parameters(GetBitContext *gb, uint8_t cprms_present_flag, unsigned int max_sub_layers_minus1)
Definition hevc.c:224
static int hvcc_parse_pps(GetBitContext *gb, HVCCNALUnit *nal, HEVCDecoderConfigurationRecord *hvcc)
Definition hevc.c:769
static int hvcc_parse_vps(GetBitContext *gb, HVCCNALUnit *nal, HEVCDecoderConfigurationRecord *hvcc)
Definition hevc.c:460
int ff_isom_write_hvcc(AVIOContext *pb, const uint8_t *data, int size, int ps_array_completeness, void *logctx)
Writes HEVC extradata (parameter sets and declarative SEI NAL units with nuh_layer_id == 0,...
Definition hevc.c:1401
static int write_configuration_record(void *logctx, AVIOContext *pb, const uint8_t *data, int size, int flags)
Definition hevc.c:1293
static void hvcc_parse_vui(GetBitContext *gb, HEVCDecoderConfigurationRecord *hvcc, unsigned int max_sub_layers_minus1)
Definition hevc.c:308
static void skip_scaling_list_data(GetBitContext *gb)
Definition hevc.c:542
static void skip_timing_info(GetBitContext *gb)
Definition hevc.c:299
static int hvcc_array_add_nal_unit(const uint8_t *nal_buf, uint32_t nal_size, HVCCNALUnitArray *array)
Definition hevc.c:840
static int hvcc_write(void *logctx, AVIOContext *pb, HEVCDecoderConfigurationRecord *hvcc, int flags)
Definition hevc.c:964
@ SEI_SUFFIX_INDEX
Definition hevc.c:39
@ NB_ARRAYS
Definition hevc.c:40
@ SEI_PREFIX_INDEX
Definition hevc.c:38
@ VPS_INDEX
Definition hevc.c:35
#define FLAG_IS_LHVC
Definition hevc.c:46
static void hvcc_init(HEVCDecoderConfigurationRecord *hvcc)
Definition hevc.c:935
#define FLAG_ARRAY_COMPLETENESS
Definition hevc.c:44
static int hvcc_parse_sps(GetBitContext *gb, HVCCNALUnit *nal, HEVCDecoderConfigurationRecord *hvcc)
Definition hevc.c:628
cl_device_type type
#define AV_RB32(p)
#define AV_RB24(x)
@ HEVC_NAL_SEI_SUFFIX
Definition hevc.h:69
@ HEVC_NAL_SEI_PREFIX
Definition hevc.h:68
@ HEVC_NAL_PPS
Definition hevc.h:63
@ HEVC_NAL_VPS
Definition hevc.h:61
@ HEVC_NAL_SPS
Definition hevc.h:62
@ HEVC_MAX_SPS_COUNT
Definition hevc.h:115
@ HEVC_MAX_PPS_COUNT
Definition hevc.h:117
@ HEVC_MAX_SUB_LAYERS
Definition hevc.h:105
@ HEVC_MAX_VPS_COUNT
Definition hevc.h:113
@ HEVC_MAX_SHORT_TERM_REF_PIC_SETS
Definition hevc.h:125
internal header for HEVC (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
#define FF_ARRAY_ELEMS(a)
Bytestream IO Context.
Definition avio.h:160
HVCCNALUnitArray arrays[NB_ARRAYS]
Definition hevc.c:84
uint16_t min_spatial_segmentation_idc
Definition hevc.c:73
uint64_t general_constraint_indicator_flags
Definition hevc.c:71
uint32_t general_profile_compatibility_flags
Definition hevc.c:70
uint8_t NAL_unit_type
Definition hevc.c:60
HVCCNALUnit * nal
Definition hevc.c:62
uint8_t array_completeness
Definition hevc.c:59
uint16_t numNalus
Definition hevc.c:61
uint8_t vps_max_sub_layers_minus1
Definition hevc.c:55
uint8_t nuh_layer_id
Definition hevc.c:49
const uint8_t * nalUnit
Definition hevc.c:52
uint16_t nalUnitLength
Definition hevc.c:51
uint8_t parameter_set_id
Definition hevc.c:50
uint8_t profile_space
Definition hevc.c:90
uint8_t tier_flag
Definition hevc.c:91
uint8_t level_idc
Definition hevc.c:95
uint64_t constraint_indicator_flags
Definition hevc.c:94
uint32_t profile_compatibility_flags
Definition hevc.c:93
uint8_t profile_idc
Definition hevc.c:92
#define av_free(p)
#define av_freep(p)
#define av_log(a,...)
static int array[MAX_W *MAX_W]
int size
int len