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