FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/hevc.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 442 574 77.0%
Functions: 22 24 91.7%
Branches: 171 302 56.6%

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