FFmpeg coverage


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