FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/hevc.c
Date: 2024-04-25 05:10:44
Exec Total Coverage
Lines: 314 471 66.7%
Functions: 16 21 76.2%
Branches: 98 226 43.4%

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