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