| @@ -0,0 +1,3116 @@ |
| 1 | +/* |
| 2 | + * This file is part of FFmpeg. |
| 3 | + * |
| 4 | + * FFmpeg is free software; you can redistribute it and/or |
| 5 | + * modify it under the terms of the GNU Lesser General Public |
| 6 | + * License as published by the Free Software Foundation; either |
| 7 | + * version 2.1 of the License, or (at your option) any later version. |
| 8 | + * |
| 9 | + * FFmpeg is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | + * Lesser General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU Lesser General Public |
| 15 | + * License along with FFmpeg; if not, write to the Free Software |
| 16 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | + */ |
| 18 | + |
| 19 | +static int FUNC(rbsp_trailing_bits) (CodedBitstreamContext *ctx, |
| 20 | + RWContext *rw) |
| 21 | +{ |
| 22 | + int err; |
| 23 | + |
| 24 | + fixed(1, rbsp_stop_one_bit, 1); |
| 25 | + while (byte_alignment(rw) != 0) |
| 26 | + fixed(1, rbsp_alignment_zero_bit, 0); |
| 27 | + return 0; |
| 28 | +} |
| 29 | + |
| 30 | +static int FUNC(nal_unit_header) (CodedBitstreamContext *ctx, RWContext *rw, |
| 31 | + H266RawNALUnitHeader *current, |
| 32 | + int expected_nal_unit_type) |
| 33 | +{ |
| 34 | + int err; |
| 35 | + |
| 36 | + fixed(1, forbidden_zero_bit, 0); |
| 37 | + flag(nuh_reserved_zero_bit); |
| 38 | + |
| 39 | + u(6, nuh_layer_id, 0, 55); |
| 40 | + |
| 41 | + if (expected_nal_unit_type >= 0) |
| 42 | + u(5, nal_unit_type, expected_nal_unit_type, expected_nal_unit_type); |
| 43 | + else |
| 44 | + ub(5, nal_unit_type); |
| 45 | + |
| 46 | + u(3, nuh_temporal_id_plus1, 1, 7); |
| 47 | + return 0; |
| 48 | +} |
| 49 | + |
| 50 | +static int FUNC(byte_alignment) (CodedBitstreamContext *ctx, RWContext *rw) |
| 51 | +{ |
| 52 | + int err; |
| 53 | + |
| 54 | + fixed(1, byte_alignment_bit_equal_to_one, 1); |
| 55 | + while (byte_alignment(rw) != 0) |
| 56 | + fixed(1, byte_alignment_bit_equal_to_zero, 0); |
| 57 | + return 0; |
| 58 | +} |
| 59 | + |
| 60 | +static int FUNC(general_constraints_info) (CodedBitstreamContext *ctx, |
| 61 | + RWContext *rw, |
| 62 | + H266GeneralConstraintsInfo *current) |
| 63 | +{ |
| 64 | + int err, i; |
| 65 | + |
| 66 | + flag(gci_present_flag); |
| 67 | + if (current->gci_present_flag) { |
| 68 | + /* general */ |
| 69 | + flag(gci_intra_only_constraint_flag); |
| 70 | + flag(gci_all_layers_independent_constraint_flag); |
| 71 | + flag(gci_one_au_only_constraint_flag); |
| 72 | + |
| 73 | + /* picture format */ |
| 74 | + u(4, gci_sixteen_minus_max_bitdepth_constraint_idc, 0, 8); |
| 75 | + ub(2, gci_three_minus_max_chroma_format_constraint_idc); |
| 76 | + |
| 77 | + /* NAL unit type related */ |
| 78 | + flag(gci_no_mixed_nalu_types_in_pic_constraint_flag); |
| 79 | + flag(gci_no_trail_constraint_flag); |
| 80 | + flag(gci_no_stsa_constraint_flag); |
| 81 | + flag(gci_no_rasl_constraint_flag); |
| 82 | + flag(gci_no_radl_constraint_flag); |
| 83 | + flag(gci_no_idr_constraint_flag); |
| 84 | + flag(gci_no_cra_constraint_flag); |
| 85 | + flag(gci_no_gdr_constraint_flag); |
| 86 | + flag(gci_no_aps_constraint_flag); |
| 87 | + flag(gci_no_idr_rpl_constraint_flag); |
| 88 | + |
| 89 | + /* tile, slice, subpicture partitioning */ |
| 90 | + flag(gci_one_tile_per_pic_constraint_flag); |
| 91 | + flag(gci_pic_header_in_slice_header_constraint_flag); |
| 92 | + flag(gci_one_slice_per_pic_constraint_flag); |
| 93 | + flag(gci_no_rectangular_slice_constraint_flag); |
| 94 | + flag(gci_one_slice_per_subpic_constraint_flag); |
| 95 | + flag(gci_no_subpic_info_constraint_flag); |
| 96 | + |
| 97 | + /* CTU and block partitioning */ |
| 98 | + ub(2, gci_three_minus_max_log2_ctu_size_constraint_idc); |
| 99 | + flag(gci_no_partition_constraints_override_constraint_flag); |
| 100 | + flag(gci_no_mtt_constraint_flag); |
| 101 | + flag(gci_no_qtbtt_dual_tree_intra_constraint_flag); |
| 102 | + |
| 103 | + /* intra */ |
| 104 | + flag(gci_no_palette_constraint_flag); |
| 105 | + flag(gci_no_ibc_constraint_flag); |
| 106 | + flag(gci_no_isp_constraint_flag); |
| 107 | + flag(gci_no_mrl_constraint_flag); |
| 108 | + flag(gci_no_mip_constraint_flag); |
| 109 | + flag(gci_no_cclm_constraint_flag); |
| 110 | + |
| 111 | + /* inter */ |
| 112 | + flag(gci_no_ref_pic_resampling_constraint_flag); |
| 113 | + flag(gci_no_res_change_in_clvs_constraint_flag); |
| 114 | + flag(gci_no_weighted_prediction_constraint_flag); |
| 115 | + flag(gci_no_ref_wraparound_constraint_flag); |
| 116 | + flag(gci_no_temporal_mvp_constraint_flag); |
| 117 | + flag(gci_no_sbtmvp_constraint_flag); |
| 118 | + flag(gci_no_amvr_constraint_flag); |
| 119 | + flag(gci_no_bdof_constraint_flag); |
| 120 | + flag(gci_no_smvd_constraint_flag); |
| 121 | + flag(gci_no_dmvr_constraint_flag); |
| 122 | + flag(gci_no_mmvd_constraint_flag); |
| 123 | + flag(gci_no_affine_motion_constraint_flag); |
| 124 | + flag(gci_no_prof_constraint_flag); |
| 125 | + flag(gci_no_bcw_constraint_flag); |
| 126 | + flag(gci_no_ciip_constraint_flag); |
| 127 | + flag(gci_no_gpm_constraint_flag); |
| 128 | + |
| 129 | + /* transform, quantization, residual */ |
| 130 | + flag(gci_no_luma_transform_size_64_constraint_flag); |
| 131 | + flag(gci_no_transform_skip_constraint_flag); |
| 132 | + flag(gci_no_bdpcm_constraint_flag); |
| 133 | + flag(gci_no_mts_constraint_flag); |
| 134 | + flag(gci_no_lfnst_constraint_flag); |
| 135 | + flag(gci_no_joint_cbcr_constraint_flag); |
| 136 | + flag(gci_no_sbt_constraint_flag); |
| 137 | + flag(gci_no_act_constraint_flag); |
| 138 | + flag(gci_no_explicit_scaling_list_constraint_flag); |
| 139 | + flag(gci_no_dep_quant_constraint_flag); |
| 140 | + flag(gci_no_sign_data_hiding_constraint_flag); |
| 141 | + flag(gci_no_cu_qp_delta_constraint_flag); |
| 142 | + flag(gci_no_chroma_qp_offset_constraint_flag); |
| 143 | + |
| 144 | + /* loop filter */ |
| 145 | + flag(gci_no_sao_constraint_flag); |
| 146 | + flag(gci_no_alf_constraint_flag); |
| 147 | + flag(gci_no_ccalf_constraint_flag); |
| 148 | + flag(gci_no_lmcs_constraint_flag); |
| 149 | + flag(gci_no_ladf_constraint_flag); |
| 150 | + flag(gci_no_virtual_boundaries_constraint_flag); |
| 151 | + ub(8, gci_num_reserved_bits); |
| 152 | + for (i = 0; i < current->gci_num_reserved_bits; i++) { |
| 153 | + flags(gci_reserved_zero_bit[i], 1, i); |
| 154 | + } |
| 155 | + } |
| 156 | + while (byte_alignment(rw) != 0) |
| 157 | + fixed(1, gci_alignment_zero_bit, 0); |
| 158 | + return 0; |
| 159 | +} |
| 160 | + |
| 161 | +static int FUNC(profile_tier_level) (CodedBitstreamContext *ctx, |
| 162 | + RWContext *rw, |
| 163 | + H266RawProfileTierLevel *current, |
| 164 | + int profile_tier_present_flag, |
| 165 | + int max_num_sub_layers_minus1) |
| 166 | +{ |
| 167 | + int err, i; |
| 168 | + |
| 169 | + if (profile_tier_present_flag) { |
| 170 | + ub(7, general_profile_idc); |
| 171 | + flag(general_tier_flag); |
| 172 | + } |
| 173 | + ub(8, general_level_idc); |
| 174 | + flag(ptl_frame_only_constraint_flag); |
| 175 | + flag(ptl_multilayer_enabled_flag); |
| 176 | + if (profile_tier_present_flag) { |
| 177 | + CHECK(FUNC(general_constraints_info) (ctx, rw, |
| 178 | + ¤t-> |
| 179 | + general_constraints_info)); |
| 180 | + } |
| 181 | + for (i = max_num_sub_layers_minus1 - 1; i >= 0; i--) |
| 182 | + flags(ptl_sublayer_level_present_flag[i], 1, i); |
| 183 | + while (byte_alignment(rw) != 0) |
| 184 | + flag(ptl_reserved_zero_bit); |
| 185 | + for (i = max_num_sub_layers_minus1 - 1; i >= 0; i--) |
| 186 | + if (current->ptl_sublayer_level_present_flag[i]) |
| 187 | + ubs(8, sublayer_level_idc[i], 1, i); |
| 188 | + if (profile_tier_present_flag) { |
| 189 | + ub(8, ptl_num_sub_profiles); |
| 190 | + for (i = 0; i < current->ptl_num_sub_profiles; i++) |
| 191 | + ubs(32, general_sub_profile_idc[i], 1, i); |
| 192 | + } |
| 193 | + return 0; |
| 194 | +} |
| 195 | + |
| 196 | +static int FUNC(vui_parameters_default) (CodedBitstreamContext *ctx, |
| 197 | + RWContext *rw, H266RawVUI *current) |
| 198 | +{ |
| 199 | + //defined in D.8 |
| 200 | + infer(vui_progressive_source_flag, 0); |
| 201 | + infer(vui_interlaced_source_flag, 0); |
| 202 | + |
| 203 | + infer(vui_non_packed_constraint_flag, 0); |
| 204 | + infer(vui_non_projected_constraint_flag, 0); |
| 205 | + |
| 206 | + infer(vui_aspect_ratio_constant_flag, 0); |
| 207 | + infer(vui_aspect_ratio_idc, 0); |
| 208 | + |
| 209 | + infer(vui_overscan_info_present_flag, 0); |
| 210 | + |
| 211 | + infer(vui_colour_primaries, 2); |
| 212 | + infer(vui_transfer_characteristics, 2); |
| 213 | + infer(vui_matrix_coeffs, 2); |
| 214 | + infer(vui_full_range_flag, 0); |
| 215 | + |
| 216 | + infer(vui_chroma_sample_loc_type_frame, 6); |
| 217 | + infer(vui_chroma_sample_loc_type_top_field, 6); |
| 218 | + infer(vui_chroma_sample_loc_type_bottom_field, 6); |
| 219 | + return 0; |
| 220 | +} |
| 221 | + |
| 222 | +static int FUNC(vui_parameters) (CodedBitstreamContext *ctx, RWContext *rw, |
| 223 | + H266RawVUI *current, |
| 224 | + uint8_t chroma_format_idc) |
| 225 | +{ |
| 226 | + int err; |
| 227 | + |
| 228 | + flag(vui_progressive_source_flag); |
| 229 | + flag(vui_interlaced_source_flag); |
| 230 | + flag(vui_non_packed_constraint_flag); |
| 231 | + flag(vui_non_projected_constraint_flag); |
| 232 | + flag(vui_aspect_ratio_info_present_flag); |
| 233 | + if (current->vui_aspect_ratio_info_present_flag) { |
| 234 | + flag(vui_aspect_ratio_constant_flag); |
| 235 | + ub(8, vui_aspect_ratio_idc); |
| 236 | + if (current->vui_aspect_ratio_idc == 255) { |
| 237 | + ub(16, vui_sar_width); |
| 238 | + ub(16, vui_sar_height); |
| 239 | + } |
| 240 | + } else { |
| 241 | + infer(vui_aspect_ratio_constant_flag, 0); |
| 242 | + infer(vui_aspect_ratio_idc, 0); |
| 243 | + } |
| 244 | + flag(vui_overscan_info_present_flag); |
| 245 | + if (current->vui_overscan_info_present_flag) |
| 246 | + flag(vui_overscan_appropriate_flag); |
| 247 | + flag(vui_colour_description_present_flag); |
| 248 | + if (current->vui_colour_description_present_flag) { |
| 249 | + ub(8, vui_colour_primaries); |
| 250 | + av_log(ctx->log_ctx, AV_LOG_DEBUG, "vui_colour_primaries == %d \n", |
| 251 | + current->vui_colour_primaries); |
| 252 | + ub(8, vui_transfer_characteristics); |
| 253 | + av_log(ctx->log_ctx, AV_LOG_DEBUG, |
| 254 | + "vui_transfer_characteristics == %d \n", |
| 255 | + current->vui_transfer_characteristics); |
| 256 | + ub(8, vui_matrix_coeffs); |
| 257 | + av_log(ctx->log_ctx, AV_LOG_DEBUG, "vui_matrix_coeffs == %d \n", |
| 258 | + current->vui_matrix_coeffs); |
| 259 | + flag(vui_full_range_flag); |
| 260 | + } else { |
| 261 | + infer(vui_colour_primaries, 2); |
| 262 | + infer(vui_transfer_characteristics, 2); |
| 263 | + infer(vui_matrix_coeffs, 2); |
| 264 | + infer(vui_full_range_flag, 0); |
| 265 | + } |
| 266 | + flag(vui_chroma_loc_info_present_flag); |
| 267 | + if (chroma_format_idc != 1 && current->vui_chroma_loc_info_present_flag) { |
| 268 | + av_log(ctx->log_ctx, AV_LOG_ERROR, "chroma_format_idc == %d," |
| 269 | + "vui_chroma_loc_info_present_flag can't not be true", |
| 270 | + chroma_format_idc); |
| 271 | + return AVERROR_INVALIDDATA; |
| 272 | + } |
| 273 | + if (current->vui_chroma_loc_info_present_flag) { |
| 274 | + if (current->vui_progressive_source_flag && |
| 275 | + !current->vui_interlaced_source_flag) { |
| 276 | + ue(vui_chroma_sample_loc_type_frame, 0, 6); |
| 277 | + } else { |
| 278 | + ue(vui_chroma_sample_loc_type_top_field, 0, 6); |
| 279 | + ue(vui_chroma_sample_loc_type_bottom_field, 0, 6); |
| 280 | + } |
| 281 | + } else { |
| 282 | + if (chroma_format_idc == 1) { |
| 283 | + infer(vui_chroma_sample_loc_type_frame, 6); |
| 284 | + infer(vui_chroma_sample_loc_type_top_field, |
| 285 | + current->vui_chroma_sample_loc_type_frame); |
| 286 | + infer(vui_chroma_sample_loc_type_bottom_field, |
| 287 | + current->vui_chroma_sample_loc_type_frame); |
| 288 | + } |
| 289 | + } |
| 290 | + return 0; |
| 291 | +} |
| 292 | + |
| 293 | +static int FUNC(payload_extension) (CodedBitstreamContext *ctx, RWContext *rw, |
| 294 | + H266RawExtensionData *current, |
| 295 | + uint32_t payload_size, int cur_pos) |
| 296 | +{ |
| 297 | + int err; |
| 298 | + size_t byte_length, k; |
| 299 | + |
| 300 | +#ifdef READ |
| 301 | + GetBitContext tmp; |
| 302 | + int bits_left, payload_zero_bits; |
| 303 | + |
| 304 | + if (!cbs_h265_payload_extension_present(rw, payload_size, cur_pos)) |
| 305 | + return 0; |
| 306 | + |
| 307 | + bits_left = 8 * payload_size - cur_pos; |
| 308 | + tmp = *rw; |
| 309 | + if (bits_left > 8) |
| 310 | + skip_bits_long(&tmp, bits_left - 8); |
| 311 | + payload_zero_bits = get_bits(&tmp, FFMIN(bits_left, 8)); |
| 312 | + if (!payload_zero_bits) |
| 313 | + return AVERROR_INVALIDDATA; |
| 314 | + payload_zero_bits = ff_ctz(payload_zero_bits); |
| 315 | + current->bit_length = bits_left - payload_zero_bits - 1; |
| 316 | + allocate(current->data, (current->bit_length + 7) / 8); |
| 317 | +#endif |
| 318 | + |
| 319 | + byte_length = (current->bit_length + 7) / 8; |
| 320 | + for (k = 0; k < byte_length; k++) { |
| 321 | + int length = FFMIN(current->bit_length - k * 8, 8); |
| 322 | + xu(length, reserved_payload_extension_data, current->data[k], |
| 323 | + 0, MAX_UINT_BITS(length), 0); |
| 324 | + } |
| 325 | + |
| 326 | + return 0; |
| 327 | +} |
| 328 | + |
| 329 | +static int FUNC(vui_payload) (CodedBitstreamContext *ctx, RWContext *rw, |
| 330 | + H266RawVUI *current, uint16_t vui_payload_size, |
| 331 | + uint8_t chroma_format_idc) |
| 332 | +{ |
| 333 | + int err; |
| 334 | + int start_position, current_position; |
| 335 | + |
| 336 | + start_position = bit_position(rw); |
| 337 | + CHECK(FUNC(vui_parameters) (ctx, rw, current, chroma_format_idc)); |
| 338 | + current_position = bit_position(rw) - start_position; |
| 339 | + |
| 340 | + if (current_position < 8 * vui_payload_size) { |
| 341 | + CHECK(FUNC(payload_extension) (ctx, rw, ¤t->extension_data, |
| 342 | + vui_payload_size, current_position)); |
| 343 | + fixed(1, vui_payload_bit_equal_to_one, 1); |
| 344 | + while (byte_alignment(rw) != 0) |
| 345 | + fixed(1, vui_payload_bit_equal_to_zero, 0); |
| 346 | + } |
| 347 | + return 0; |
| 348 | +} |
| 349 | + |
| 350 | +static int FUNC(extension_data) (CodedBitstreamContext *ctx, RWContext *rw, |
| 351 | + H266RawExtensionData *current) |
| 352 | +{ |
| 353 | + int err; |
| 354 | + size_t k; |
| 355 | +#ifdef READ |
| 356 | + GetBitContext start; |
| 357 | + uint8_t bit; |
| 358 | + start = *rw; |
| 359 | + for (k = 0; cbs_h2645_read_more_rbsp_data(rw); k++) |
| 360 | + skip_bits(rw, 1); |
| 361 | + current->bit_length = k; |
| 362 | + if (k > 0) { |
| 363 | + *rw = start; |
| 364 | + allocate(current->data, (current->bit_length + 7) / 8); |
| 365 | + for (k = 0; k < current->bit_length; k++) { |
| 366 | + xu(1, extension_data, bit, 0, 1, 0); |
| 367 | + current->data[k / 8] |= bit << (7 - k % 8); |
| 368 | + } |
| 369 | + } |
| 370 | +#else |
| 371 | + for (k = 0; k < current->bit_length; k++) |
| 372 | + xu(1, extension_data, current->data[k / 8] >> (7 - k % 8) & 1, 0, 1, 0); |
| 373 | +#endif |
| 374 | + return 0; |
| 375 | +} |
| 376 | + |
| 377 | +static int FUNC(dpb_parameters) (CodedBitstreamContext *ctx, RWContext *rw, |
| 378 | + H266DpbParameters *current, |
| 379 | + uint8_t max_sublayers_minus1, |
| 380 | + uint8_t sublayer_info_flag) |
| 381 | +{ |
| 382 | + int err, i; |
| 383 | + for (i = (sublayer_info_flag ? 0 : max_sublayers_minus1); |
| 384 | + i <= max_sublayers_minus1; i++) { |
| 385 | + ues(dpb_max_dec_pic_buffering_minus1[i], 0, VVC_MAX_DPB_SIZE - 1, 1, i); |
| 386 | + ues(dpb_max_num_reorder_pics[i], |
| 387 | + 0, current->dpb_max_dec_pic_buffering_minus1[i], 1, i); |
| 388 | + ues(dpb_max_latency_increase_plus1[i], 0, UINT32_MAX - 1, 1, i); |
| 389 | + } |
| 390 | + return 0; |
| 391 | +} |
| 392 | + |
| 393 | +static int FUNC(ref_pic_list_struct) (CodedBitstreamContext *ctx, |
| 394 | + RWContext *rw, |
| 395 | + H266RefPicListStruct *current, |
| 396 | + uint8_t list_idx, uint8_t rpls_idx, |
| 397 | + const H266RawSPS *sps) |
| 398 | +{ |
| 399 | + CodedBitstreamH266Context *h266 = ctx->priv_data; |
| 400 | + int err, i, j, general_layer_idx = -1, num_direct_ref_layers = 0; |
| 401 | + const H266RawVPS *vps = h266->vps[sps->sps_video_parameter_set_id]; |
| 402 | + |
| 403 | + if (!vps) { |
| 404 | + av_log(ctx->log_ctx, AV_LOG_ERROR, |
| 405 | + "VPS id %d not available.\n", sps->sps_video_parameter_set_id); |
| 406 | + return AVERROR_INVALIDDATA; |
| 407 | + } |
| 408 | + //7.4.3.3 (29) |
| 409 | + for (i = 0; i <= vps->vps_max_layers_minus1; i++) { |
| 410 | + if (sps->nal_unit_header.nuh_layer_id == vps->vps_layer_id[i]) { |
| 411 | + general_layer_idx = i; |
| 412 | + break; |
| 413 | + } |
| 414 | + } |
| 415 | + if (general_layer_idx < 0) { |
| 416 | + av_log(ctx->log_ctx, AV_LOG_ERROR, "vps_layer_id %d not available.\n", |
| 417 | + sps->nal_unit_header.nuh_layer_id); |
| 418 | + return AVERROR_INVALIDDATA; |
| 419 | + } |
| 420 | + //7.4.3.3 (28) |
| 421 | + for (j = 0; j <= vps->vps_max_layers_minus1; j++) { |
| 422 | + if (vps->vps_direct_ref_layer_flag[general_layer_idx][j]) |
| 423 | + num_direct_ref_layers++; |
| 424 | + } |
| 425 | + |
| 426 | + ue(num_ref_entries, 0, VVC_MAX_REF_ENTRIES); |
| 427 | + if (sps->sps_long_term_ref_pics_flag && |
| 428 | + rpls_idx < sps->sps_num_ref_pic_lists[list_idx] && |
| 429 | + current->num_ref_entries > 0) |
| 430 | + flag(ltrp_in_header_flag); |
| 431 | + if (sps->sps_long_term_ref_pics_flag && |
| 432 | + rpls_idx == sps->sps_num_ref_pic_lists[list_idx]) |
| 433 | + infer(ltrp_in_header_flag, 1); |
| 434 | + for (i = 0, j = 0; i < current->num_ref_entries; i++) { |
| 435 | + if (sps->sps_inter_layer_prediction_enabled_flag) |
| 436 | + flags(inter_layer_ref_pic_flag[i], 1, i); |
| 437 | + else |
| 438 | + infer(inter_layer_ref_pic_flag[i], 0); |
| 439 | + |
| 440 | + if (!current->inter_layer_ref_pic_flag[i]) { |
| 441 | + if (sps->sps_long_term_ref_pics_flag) |
| 442 | + flags(st_ref_pic_flag[i], 1, i); |
| 443 | + else |
| 444 | + infer(st_ref_pic_flag[i], 1); |
| 445 | + if (current->st_ref_pic_flag[i]) { |
| 446 | + int abs_delta_poc_st; |
| 447 | + ues(abs_delta_poc_st[i], 0, MAX_UINT_BITS(15), 1, i); |
| 448 | + if ((sps->sps_weighted_pred_flag || |
| 449 | + sps->sps_weighted_bipred_flag) && i != 0) |
| 450 | + abs_delta_poc_st = current->abs_delta_poc_st[i]; |
| 451 | + else |
| 452 | + abs_delta_poc_st = current->abs_delta_poc_st[i] + 1; |
| 453 | + if (abs_delta_poc_st > 0) |
| 454 | + flags(strp_entry_sign_flag[i], 1, i); |
| 455 | + } else { |
| 456 | + if (!current->ltrp_in_header_flag) { |
| 457 | + uint8_t bits = sps->sps_log2_max_pic_order_cnt_lsb_minus4 + 4; |
| 458 | + ubs(bits, rpls_poc_lsb_lt[j], 1, j); |
| 459 | + j++; |
| 460 | + } |
| 461 | + } |
| 462 | + } else { |
| 463 | + if (num_direct_ref_layers == 0) { |
| 464 | + av_log(ctx->log_ctx, AV_LOG_ERROR, |
| 465 | + "num_direct_ref_layers needs > 0.\n"); |
| 466 | + return AVERROR_INVALIDDATA; |
| 467 | + } |
| 468 | + ues(ilrp_idx[i], 0, num_direct_ref_layers - 1, 1, i); |
| 469 | + } |
| 470 | + } |
| 471 | + return 0; |
| 472 | +} |
| 473 | + |
| 474 | +static int FUNC(ref_pic_lists) (CodedBitstreamContext *ctx, RWContext *rw, |
| 475 | + const H266RawSPS *sps, const H266RawPPS *pps, |
| 476 | + H266RefPicLists *current) { |
| 477 | + const H266RefPicListStruct * ref_list; |
| 478 | + int err, i, j, num_ltrp_entries; |
| 479 | + for (i = 0; i < 2; i++) { |
| 480 | + if (sps->sps_num_ref_pic_lists[i] > 0 && |
| 481 | + (i == 0 || (i == 1 && pps->pps_rpl1_idx_present_flag))) { |
| 482 | + flags(rpl_sps_flag[i], 1, i); |
| 483 | + } else { |
| 484 | + if (sps->sps_num_ref_pic_lists[i] == 0) { |
| 485 | + infer(rpl_sps_flag[i], 0); |
| 486 | + } else { |
| 487 | + if (!pps->pps_rpl1_idx_present_flag && i == 1) |
| 488 | + infer(rpl_sps_flag[1], current->rpl_sps_flag[0]); |
| 489 | + } |
| 490 | + } |
| 491 | + if (current->rpl_sps_flag[i]) { |
| 492 | + if (sps->sps_num_ref_pic_lists[i] > 1 && |
| 493 | + (i == 0 || (i == 1 && pps->pps_rpl1_idx_present_flag))) { |
| 494 | + uint8_t bits = av_ceil_log2(sps->sps_num_ref_pic_lists[i]); |
| 495 | + us(bits, rpl_idx[i], 0, sps->sps_num_ref_pic_lists[i] - 1, 1, i); |
| 496 | + } else if (sps->sps_num_ref_pic_lists[i] == 1) { |
| 497 | + infer(rpl_idx[i], 0); |
| 498 | + } else if (i == 1 && !pps->pps_rpl1_idx_present_flag) { |
| 499 | + infer(rpl_idx[1], current->rpl_idx[0]); |
| 500 | + } else { |
| 501 | + //how to handle this? or never happpend? |
| 502 | + av_log(ctx->log_ctx, AV_LOG_ERROR, |
| 503 | + "can't infer the rpl_idx[i]\n"); |
| 504 | + return AVERROR_PATCHWELCOME; |
| 505 | + } |
| 506 | + memcpy(¤t->rpl_ref_list[i], |
| 507 | + &sps->sps_ref_pic_list_struct[i][current->rpl_idx[i]], |
| 508 | + sizeof(current->rpl_ref_list[i])); |
| 509 | + } else { |
| 510 | + CHECK(FUNC(ref_pic_list_struct) (ctx, rw, ¤t->rpl_ref_list[i], |
| 511 | + i, sps->sps_num_ref_pic_lists[i], |
| 512 | + sps)); |
| 513 | + } |
| 514 | + ref_list = ¤t->rpl_ref_list[i]; |
| 515 | + |
| 516 | + num_ltrp_entries = 0; |
| 517 | + for (int i = 0; i < ref_list->num_ref_entries; i++) { |
| 518 | + if (!ref_list->inter_layer_ref_pic_flag[i]) { |
| 519 | + if (!ref_list->st_ref_pic_flag[i]) { |
| 520 | + num_ltrp_entries++; |
| 521 | + } |
| 522 | + } |
| 523 | + } |
| 524 | + |
| 525 | + for (j = 0; j < num_ltrp_entries; j++) { |
| 526 | + if (ref_list->ltrp_in_header_flag) { |
| 527 | + ubs(sps->sps_log2_max_pic_order_cnt_lsb_minus4 + 4, |
| 528 | + poc_lsb_lt[i][j], 2, i, j); |
| 529 | + } |
| 530 | + flags(delta_poc_msb_cycle_present_flag[i][j], 2, i, j); |
| 531 | + if (current->delta_poc_msb_cycle_present_flag[i][j]) { |
| 532 | + uint32_t max = |
| 533 | + 1 << (32 - sps->sps_log2_max_pic_order_cnt_lsb_minus4 - 4); |
| 534 | + ues(delta_poc_msb_cycle_lt[i][j], 0, max, 2, i, j); |
| 535 | + } |
| 536 | + } |
| 537 | + } |
| 538 | + return 0; |
| 539 | +} |
| 540 | + |
| 541 | +static int FUNC(general_timing_hrd_parameters)(CodedBitstreamContext *ctx, |
| 542 | + RWContext *rw, |
| 543 | + H266RawGeneralTimingHrdParameters *current) |
| 544 | +{ |
| 545 | + int err; |
| 546 | + ub(32, num_units_in_tick); |
| 547 | + u(32, time_scale, 1, MAX_UINT_BITS(32)); |
| 548 | + flag(general_nal_hrd_params_present_flag); |
| 549 | + flag(general_vcl_hrd_params_present_flag); |
| 550 | + |
| 551 | + if (current->general_nal_hrd_params_present_flag || |
| 552 | + current->general_vcl_hrd_params_present_flag) { |
| 553 | + flag(general_same_pic_timing_in_all_ols_flag); |
| 554 | + flag(general_du_hrd_params_present_flag); |
| 555 | + if (current->general_du_hrd_params_present_flag) |
| 556 | + ub(8, tick_divisor_minus2); |
| 557 | + ub(4, bit_rate_scale); |
| 558 | + ub(4, cpb_size_scale); |
| 559 | + if (current->general_du_hrd_params_present_flag) |
| 560 | + ub(4, cpb_size_du_scale); |
| 561 | + ue(hrd_cpb_cnt_minus1, 0, 31); |
| 562 | + } else { |
| 563 | + //infer general_same_pic_timing_in_all_ols_flag? |
| 564 | + infer(general_du_hrd_params_present_flag, 0); |
| 565 | + } |
| 566 | + return 0; |
| 567 | +} |
| 568 | + |
| 569 | +static int FUNC(sublayer_hrd_parameters) (CodedBitstreamContext *ctx, |
| 570 | + RWContext *rw, |
| 571 | + H266RawSubLayerHRDParameters *current, |
| 572 | + int sublayer_id, |
| 573 | + const H266RawGeneralTimingHrdParameters *general) |
| 574 | +{ |
| 575 | + int err, i; |
| 576 | + for (i = 0; i <= general->hrd_cpb_cnt_minus1; i++) { |
| 577 | + ues(bit_rate_value_minus1[sublayer_id][i], 0, UINT32_MAX - 1, 2, |
| 578 | + sublayer_id, i); |
| 579 | + ues(cpb_size_value_minus1[sublayer_id][i], 0, UINT32_MAX - 1, 2, |
| 580 | + sublayer_id, i); |
| 581 | + if (general->general_du_hrd_params_present_flag) { |
| 582 | + ues(cpb_size_du_value_minus1[sublayer_id][i], |
| 583 | + 0, UINT32_MAX - 1, 2, sublayer_id, i); |
| 584 | + ues(bit_rate_du_value_minus1[sublayer_id][i], |
| 585 | + 0, UINT32_MAX - 1, 2, sublayer_id, i); |
| 586 | + } |
| 587 | + flags(cbr_flag[sublayer_id][i], 2, sublayer_id, i); |
| 588 | + } |
| 589 | + return 0; |
| 590 | +} |
| 591 | + |
| 592 | +static int FUNC(ols_timing_hrd_parameters) (CodedBitstreamContext *ctx, |
| 593 | + RWContext *rw, H266RawOlsTimingHrdParameters *current, |
| 594 | + uint8_t first_sublayer, uint8_t max_sublayers_minus1, |
| 595 | + const H266RawGeneralTimingHrdParameters *general) |
| 596 | +{ |
| 597 | + int err, i; |
| 598 | + for (i = first_sublayer; i <= max_sublayers_minus1; i++) { |
| 599 | + flags(fixed_pic_rate_general_flag[i], 1, i); |
| 600 | + if (!current->fixed_pic_rate_general_flag[i]) |
| 601 | + flags(fixed_pic_rate_within_cvs_flag[i], 1, i); |
| 602 | + else |
| 603 | + infer(fixed_pic_rate_within_cvs_flag[i], 1); |
| 604 | + if (current->fixed_pic_rate_within_cvs_flag[i]) { |
| 605 | + ues(elemental_duration_in_tc_minus1[i], 0, 2047, 1, i); |
| 606 | + infer(low_delay_hrd_flag[i], 0); |
| 607 | + } else if ((general->general_nal_hrd_params_present_flag || |
| 608 | + general->general_vcl_hrd_params_present_flag) && |
| 609 | + general->hrd_cpb_cnt_minus1 == 0) { |
| 610 | + flags(low_delay_hrd_flag[i], 1, i); |
| 611 | + } else { |
| 612 | + infer(low_delay_hrd_flag[i], 0); |
| 613 | + } |
| 614 | + if (general->general_nal_hrd_params_present_flag) |
| 615 | + CHECK(FUNC(sublayer_hrd_parameters) (ctx, rw, |
| 616 | + ¤t->nal_sub_layer_hrd_parameters, |
| 617 | + i, general)); |
| 618 | + if (general->general_vcl_hrd_params_present_flag) |
| 619 | + CHECK(FUNC(sublayer_hrd_parameters) (ctx, rw, |
| 620 | + ¤t->nal_sub_layer_hrd_parameters, |
| 621 | + i, general)); |
| 622 | + } |
| 623 | + return 0; |
| 624 | +} |
| 625 | + |
| 626 | +static int FUNC(vps) (CodedBitstreamContext *ctx, RWContext *rw, |
| 627 | + H266RawVPS *current) |
| 628 | +{ |
| 629 | + int err, i, j, k; |
| 630 | + uint16_t total_num_olss = 0; |
| 631 | + uint8_t ols_mode_idc = 0; |
| 632 | + uint16_t num_multi_layer_olss = 0; |
| 633 | + uint8_t layer_included_in_ols_flag[VVC_MAX_TOTAL_NUM_OLSS][VVC_MAX_LAYERS]; |
| 634 | + uint8_t num_ref_layers[VVC_MAX_LAYERS]; |
| 635 | + uint8_t reference_layer_idx[VVC_MAX_LAYERS][VVC_MAX_LAYERS]; |
| 636 | + |
| 637 | + HEADER("Video Parameter Set"); |
| 638 | + |
| 639 | + CHECK(FUNC(nal_unit_header) (ctx, rw, |
| 640 | + ¤t->nal_unit_header, VVC_VPS_NUT)); |
| 641 | + |
| 642 | + ub(4, vps_video_parameter_set_id); |
| 643 | + if (current->vps_video_parameter_set_id == 0) { |
| 644 | + av_log(ctx->log_ctx, AV_LOG_ERROR, |
| 645 | + "vps_video_parameter_set_id should > 0.\n"); |
| 646 | + return AVERROR_INVALIDDATA; |
| 647 | + } |
| 648 | + ub(6, vps_max_layers_minus1); |
| 649 | + u(3, vps_max_sublayers_minus1, 0, 6); |
| 650 | + if (current->vps_max_layers_minus1 > 0 |
| 651 | + && current->vps_max_sublayers_minus1 > 0) |
| 652 | + flag(vps_default_ptl_dpb_hrd_max_tid_flag); |
| 653 | + else |
| 654 | + infer(vps_default_ptl_dpb_hrd_max_tid_flag, 1); |
| 655 | + |
| 656 | + if (current->vps_max_layers_minus1 > 0) |
| 657 | + flag(vps_all_independent_layers_flag); |
| 658 | + else |
| 659 | + infer(vps_all_independent_layers_flag, 1); |
| 660 | + |
| 661 | + for (i = 0; i <= current->vps_max_layers_minus1; i++) { |
| 662 | + ubs(6, vps_layer_id[i], 1, i); |
| 663 | + if (i > 0 && current->vps_layer_id[i] <= current->vps_layer_id[i - 1]) { |
| 664 | + av_log(ctx->log_ctx, AV_LOG_ERROR, |
| 665 | + "vps_layer_id[%d](%d) should > vps_layer_id[%d](%d).\n", |
| 666 | + i, current->vps_layer_id[i], i - 1, |
| 667 | + current->vps_layer_id[i - 1]); |
| 668 | + return AVERROR_INVALIDDATA; |
| 669 | + } |
| 670 | + if (i > 0 && !current->vps_all_independent_layers_flag) { |
| 671 | + flags(vps_independent_layer_flag[i], 1, i); |
| 672 | + if (!current->vps_independent_layer_flag[i]) { |
| 673 | + flags(vps_max_tid_ref_present_flag[i], 1, i); |
| 674 | + for (j = 0; j < i; j++) { |
| 675 | + flags(vps_direct_ref_layer_flag[i][j], 2, i, j); |
| 676 | + if (current->vps_max_tid_ref_present_flag[i] && |
| 677 | + current->vps_direct_ref_layer_flag[i][j]) { |
| 678 | + ubs(3, vps_max_tid_il_ref_pics_plus1[i][j], 2, i, j); |
| 679 | + } else { |
| 680 | + infer(vps_max_tid_il_ref_pics_plus1[i][j], |
| 681 | + current->vps_max_sublayers_minus1 + 1); |
| 682 | + } |
| 683 | + } |
| 684 | + } else { |
| 685 | + for (j = 0; j < i; j++) { |
| 686 | + infer(vps_direct_ref_layer_flag[i][j], 0); |
| 687 | + } |
| 688 | + } |
| 689 | + } else { |
| 690 | + infer(vps_independent_layer_flag[i], 1); |
| 691 | + for (j = 0; j < i; j++) { |
| 692 | + infer(vps_direct_ref_layer_flag[i][j], 0); |
| 693 | + } |
| 694 | + } |
| 695 | + } |
| 696 | + |
| 697 | + if (current->vps_max_layers_minus1 > 0) { |
| 698 | + if (current->vps_all_independent_layers_flag) |
| 699 | + flag(vps_each_layer_is_an_ols_flag); |
| 700 | + else |
| 701 | + infer(vps_each_layer_is_an_ols_flag, 0); |
| 702 | + if (!current->vps_each_layer_is_an_ols_flag) { |
| 703 | + if (!current->vps_all_independent_layers_flag) |
| 704 | + ub(2, vps_ols_mode_idc); |
| 705 | + else |
| 706 | + infer(vps_ols_mode_idc, 2); |
| 707 | + if (current->vps_ols_mode_idc == 2) { |
| 708 | + ub(8, vps_num_output_layer_sets_minus2); |
| 709 | + for (i = 1; i <= current->vps_num_output_layer_sets_minus2 + 1; |
| 710 | + i++) |
| 711 | + for (j = 0; j <= current->vps_max_layers_minus1; j++) |
| 712 | + flags(vps_ols_output_layer_flag[i][j], 2, i, j); |
| 713 | + } |
| 714 | + ols_mode_idc = current->vps_ols_mode_idc; |
| 715 | + } else { |
| 716 | + ols_mode_idc = 4; |
| 717 | + } |
| 718 | + if (ols_mode_idc == 4 || ols_mode_idc == 0 || ols_mode_idc == 1) |
| 719 | + total_num_olss = current->vps_max_layers_minus1 + 1; |
| 720 | + else if (ols_mode_idc = 2) |
| 721 | + total_num_olss = current->vps_num_output_layer_sets_minus2 + 2; |
| 722 | + else |
| 723 | + av_log(ctx->log_ctx, AV_LOG_ERROR, |
| 724 | + "ols_mode_idc == 3, patch welcome"); |
| 725 | + u(8, vps_num_ptls_minus1, 0, total_num_olss - 1); |
| 726 | + } else { |
| 727 | + infer(vps_each_layer_is_an_ols_flag, 1); |
| 728 | + infer(vps_num_ptls_minus1, 0); |
| 729 | + } |
| 730 | + { |
| 731 | + //calc NumMultiLayerOlss |
| 732 | + int m; |
| 733 | + uint8_t dependency_flag[VVC_MAX_LAYERS][VVC_MAX_LAYERS]; |
| 734 | + uint16_t num_output_layers_in_ols[VVC_MAX_TOTAL_NUM_OLSS]; |
| 735 | + uint8_t num_sub_layers_in_layer_in_ols[VVC_MAX_TOTAL_NUM_OLSS][VVC_MAX_TOTAL_NUM_OLSS]; |
| 736 | + uint8_t output_layer_idx[VVC_MAX_TOTAL_NUM_OLSS][VVC_MAX_LAYERS]; |
| 737 | + |
| 738 | + //7.4.3.3 vps_direct_ref_layer_flag section |
| 739 | + for (i = 0; i <= current->vps_max_layers_minus1; i++) { |
| 740 | + for (j = 0; j <= current->vps_max_layers_minus1; j++) { |
| 741 | + dependency_flag[i][j] = current->vps_direct_ref_layer_flag[i][j]; |
| 742 | + for (k = 0; k < i; k++) { |
| 743 | + if (current->vps_direct_ref_layer_flag[i][k] && |
| 744 | + dependency_flag[k][j]) |
| 745 | + dependency_flag[i][j] = 1; |
| 746 | + } |
| 747 | + } |
| 748 | + } |
| 749 | + for (i = 0; i <= current->vps_max_layers_minus1; i++) { |
| 750 | + int r; |
| 751 | + for (j = 0, r = 0; j <= current->vps_max_layers_minus1; j++) { |
| 752 | + if (dependency_flag[i][j]) |
| 753 | + reference_layer_idx[i][r++] = j; |
| 754 | + } |
| 755 | + num_ref_layers[i] = r; |
| 756 | + } |
| 757 | + |
| 758 | + //7.4.3.3 vps_ols_output_layer_flag section |
| 759 | + num_output_layers_in_ols[0] = 1; |
| 760 | + num_sub_layers_in_layer_in_ols[0][0] = |
| 761 | + current->vps_ptl_max_tid[current->vps_ols_ptl_idx[0]] + 1; |
| 762 | + for (i = 1; i < total_num_olss; i++) { |
| 763 | + if (ols_mode_idc == 4 || ols_mode_idc == 0) { |
| 764 | + num_output_layers_in_ols[i] = 1; |
| 765 | + if (current->vps_each_layer_is_an_ols_flag) { |
| 766 | + num_sub_layers_in_layer_in_ols[i][0] = |
| 767 | + current->vps_ptl_max_tid[current->vps_ols_ptl_idx[i]] + 1; |
| 768 | + } else { |
| 769 | + num_sub_layers_in_layer_in_ols[i][i] = |
| 770 | + current->vps_ptl_max_tid[current->vps_ols_ptl_idx[i]] + 1; |
| 771 | + for (k = i - 1; k >= 0; k--) { |
| 772 | + num_sub_layers_in_layer_in_ols[i][k] = 0; |
| 773 | + for (m = k + 1; m <= i; m++) { |
| 774 | + uint8_t max_sublayer_needed = |
| 775 | + FFMIN(num_sub_layers_in_layer_in_ols[i][m], |
| 776 | + current->vps_max_tid_il_ref_pics_plus1[m][k]); |
| 777 | + if (current->vps_direct_ref_layer_flag[m][k] && |
| 778 | + num_sub_layers_in_layer_in_ols[i][k] < max_sublayer_needed) |
| 779 | + num_sub_layers_in_layer_in_ols[i][k] = max_sublayer_needed; |
| 780 | + } |
| 781 | + } |
| 782 | + } |
| 783 | + } else if (current->vps_ols_mode_idc == 1) { |
| 784 | + num_output_layers_in_ols[i] = i + 1; |
| 785 | + for (j = 0; j < num_output_layers_in_ols[i]; j++) { |
| 786 | + num_sub_layers_in_layer_in_ols[i][j] = |
| 787 | + current->vps_ptl_max_tid[current->vps_ols_ptl_idx[i]] + 1; |
| 788 | + } |
| 789 | + } else if (current->vps_ols_mode_idc == 2) { |
| 790 | + uint8_t highest_included_layer = 0; |
| 791 | + for (j = 0; j <= current->vps_max_layers_minus1; j++) { |
| 792 | + layer_included_in_ols_flag[i][j] = 0; |
| 793 | + num_sub_layers_in_layer_in_ols[i][j] = 0; |
| 794 | + } |
| 795 | + for (k = 0, j = 0; k <= current->vps_max_layers_minus1; k++) { |
| 796 | + if (current->vps_ols_output_layer_flag[i][k]) { |
| 797 | + layer_included_in_ols_flag[i][k] = 1; |
| 798 | + highest_included_layer = k; |
| 799 | + output_layer_idx[i][j] = k; |
| 800 | + num_sub_layers_in_layer_in_ols[i][k] = |
| 801 | + current->vps_ptl_max_tid[current-> |
| 802 | + vps_ols_ptl_idx[i]] + 1; |
| 803 | + j++; |
| 804 | + } |
| 805 | + } |
| 806 | + num_output_layers_in_ols[i] = j; |
| 807 | + for (j = 0; j < num_output_layers_in_ols[i]; j++) { |
| 808 | + int idx = output_layer_idx[i][j]; |
| 809 | + for (k = 0; k < num_ref_layers[idx]; k++) { |
| 810 | + if (!layer_included_in_ols_flag[i][reference_layer_idx[idx][k]]) |
| 811 | + layer_included_in_ols_flag[i][reference_layer_idx[idx][k]] = 1; |
| 812 | + } |
| 813 | + } |
| 814 | + for (k = highest_included_layer - 1; k >= 0; k--) { |
| 815 | + if (layer_included_in_ols_flag[i][k] && |
| 816 | + !current->vps_ols_output_layer_flag[i][k]) { |
| 817 | + for (m = k + 1; m <= highest_included_layer; m++) { |
| 818 | + uint8_t max_sublayer_needed = |
| 819 | + FFMIN(num_sub_layers_in_layer_in_ols[i][m], |
| 820 | + current->vps_max_tid_il_ref_pics_plus1[m][k]); |
| 821 | + if (current->vps_direct_ref_layer_flag[m][k] && |
| 822 | + layer_included_in_ols_flag[i][m] && |
| 823 | + num_sub_layers_in_layer_in_ols[i][k] < |
| 824 | + max_sublayer_needed) |
| 825 | + num_sub_layers_in_layer_in_ols[i][k] = |
| 826 | + max_sublayer_needed; |
| 827 | + } |
| 828 | + } |
| 829 | + } |
| 830 | + } |
| 831 | + } |
| 832 | + for (i = 1; i < total_num_olss; i++) { |
| 833 | + int num_layers_in_ols = 0; |
| 834 | + if (current->vps_each_layer_is_an_ols_flag) { |
| 835 | + num_layers_in_ols = 1; |
| 836 | + } else if (current->vps_ols_mode_idc == 0 || |
| 837 | + current->vps_ols_mode_idc == 1) { |
| 838 | + num_layers_in_ols = i + 1; |
| 839 | + } else if (current->vps_ols_mode_idc == 2) { |
| 840 | + for (k = 0, j = 0; k <= current->vps_max_layers_minus1; k++) { |
| 841 | + if (layer_included_in_ols_flag[i][k]) |
| 842 | + j++; |
| 843 | + num_layers_in_ols = j; |
| 844 | + } |
| 845 | + } |
| 846 | + if (num_layers_in_ols > 1) { |
| 847 | + num_multi_layer_olss++; |
| 848 | + } |
| 849 | + } |
| 850 | + } |
| 851 | + |
| 852 | + for (i = 0; i <= current->vps_num_ptls_minus1; i++) { |
| 853 | + if (i > 0) |
| 854 | + flags(vps_pt_present_flag[i], 1, i); |
| 855 | + else |
| 856 | + infer(vps_pt_present_flag[i], 1); |
| 857 | + |
| 858 | + if (!current->vps_default_ptl_dpb_hrd_max_tid_flag) |
| 859 | + us(3, vps_ptl_max_tid[i], 0, current->vps_max_sublayers_minus1, 1, i); |
| 860 | + else |
| 861 | + infer(vps_ptl_max_tid[i], current->vps_max_sublayers_minus1); |
| 862 | + } |
| 863 | + while (byte_alignment(rw) != 0) |
| 864 | + fixed(1, vps_ptl_alignment_zero_bit, 0); |
| 865 | + for (i = 0; i <= current->vps_num_ptls_minus1; i++) { |
| 866 | + CHECK(FUNC(profile_tier_level) (ctx, rw, |
| 867 | + current->vps_profile_tier_level + i, |
| 868 | + current->vps_pt_present_flag[i], |
| 869 | + current->vps_ptl_max_tid[i])); |
| 870 | + } |
| 871 | + for (i = 0; i < total_num_olss; i++) { |
| 872 | + if (current->vps_num_ptls_minus1 > 0 && |
| 873 | + current->vps_num_ptls_minus1 + 1 != total_num_olss) { |
| 874 | + us(8, vps_ols_ptl_idx[i], 0, current->vps_num_ptls_minus1, 1, i); |
| 875 | + } else if (current->vps_num_ptls_minus1 == 0) { |
| 876 | + infer(vps_ols_ptl_idx[i], 0); |
| 877 | + } else { |
| 878 | + infer(vps_ols_ptl_idx[i], i); |
| 879 | + } |
| 880 | + } |
| 881 | + |
| 882 | + if (!current->vps_each_layer_is_an_ols_flag) { |
| 883 | + uint16_t vps_num_dpb_params; |
| 884 | + ue(vps_num_dpb_params_minus1, 0, num_multi_layer_olss - 1); |
| 885 | + if (current->vps_each_layer_is_an_ols_flag) |
| 886 | + vps_num_dpb_params = 0; |
| 887 | + else |
| 888 | + vps_num_dpb_params = current->vps_num_dpb_params_minus1 + 1; |
| 889 | + |
| 890 | + if (current->vps_max_sublayers_minus1 > 0) |
| 891 | + flag(vps_sublayer_dpb_params_present_flag); |
| 892 | + else |
| 893 | + infer(vps_sublayer_dpb_params_present_flag, 0); |
| 894 | + |
| 895 | + for (i = 0; i < vps_num_dpb_params; i++) { |
| 896 | + if (!current->vps_default_ptl_dpb_hrd_max_tid_flag) |
| 897 | + us(3, vps_dpb_max_tid[i], 0, current->vps_max_sublayers_minus1, |
| 898 | + 1, i); |
| 899 | + else |
| 900 | + infer(vps_dpb_max_tid[i], current->vps_max_sublayers_minus1); |
| 901 | + CHECK(FUNC(dpb_parameters) (ctx, rw, current->vps_dpb_params + i, |
| 902 | + current->vps_dpb_max_tid[i], |
| 903 | + current-> |
| 904 | + vps_sublayer_dpb_params_present_flag)); |
| 905 | + } |
| 906 | + for (i = 0; i < num_multi_layer_olss; i++) { |
| 907 | + ues(vps_ols_dpb_pic_width[i], 0, UINT16_MAX, 1, i); |
| 908 | + ues(vps_ols_dpb_pic_height[i], 0, UINT16_MAX, 1, i); |
| 909 | + ubs(2, vps_ols_dpb_chroma_format[i], 1, i); |
| 910 | + ues(vps_ols_dpb_bitdepth_minus8[i], 0, 2, 1, i); |
| 911 | + if (vps_num_dpb_params > 1 |
| 912 | + && vps_num_dpb_params != num_multi_layer_olss) |
| 913 | + ues(vps_ols_dpb_params_idx[i], 0, vps_num_dpb_params - 1, 1, i); |
| 914 | + else if (vps_num_dpb_params == 1) |
| 915 | + infer(vps_ols_dpb_params_idx[i], 0); |
| 916 | + else |
| 917 | + infer(vps_ols_dpb_params_idx[i], i); |
| 918 | + } |
| 919 | + flag(vps_timing_hrd_params_present_flag); |
| 920 | + if (current->vps_timing_hrd_params_present_flag) { |
| 921 | + CHECK(FUNC(general_timing_hrd_parameters) (ctx, rw, |
| 922 | + ¤t-> |
| 923 | + vps_general_timing_hrd_parameters)); |
| 924 | + if (current->vps_max_sublayers_minus1 > 0) |
| 925 | + flag(vps_sublayer_cpb_params_present_flag); |
| 926 | + else |
| 927 | + infer(vps_sublayer_cpb_params_present_flag, 0); |
| 928 | + ue(vps_num_ols_timing_hrd_params_minus1, 0, |
| 929 | + num_multi_layer_olss - 1); |
| 930 | + for (i = 0; i <= current->vps_num_ols_timing_hrd_params_minus1; i++) { |
| 931 | + uint8_t first_sublayer; |
| 932 | + if (!current->vps_default_ptl_dpb_hrd_max_tid_flag) |
| 933 | + us(3, vps_hrd_max_tid[i], 0, |
| 934 | + current->vps_max_sublayers_minus1, 1, i); |
| 935 | + else |
| 936 | + infer(vps_hrd_max_tid[i], |
| 937 | + current->vps_max_sublayers_minus1); |
| 938 | + first_sublayer = current->vps_sublayer_cpb_params_present_flag ? |
| 939 | + 0 : current->vps_hrd_max_tid[i]; |
| 940 | + CHECK(FUNC(ols_timing_hrd_parameters) |
| 941 | + (ctx, rw, ¤t->vps_ols_timing_hrd_parameters, |
| 942 | + first_sublayer, current->vps_max_sublayers_minus1, |
| 943 | + ¤t->vps_general_timing_hrd_parameters)); |
| 944 | + |
| 945 | + } |
| 946 | + if (current->vps_num_ols_timing_hrd_params_minus1 > 0 && |
| 947 | + current->vps_num_ols_timing_hrd_params_minus1 + 1 != |
| 948 | + num_multi_layer_olss) { |
| 949 | + for (i = 0; i < num_multi_layer_olss; i++) { |
| 950 | + ues(vps_ols_timing_hrd_idx[i], 0, |
| 951 | + current->vps_num_ols_timing_hrd_params_minus1, 1, i); |
| 952 | + } |
| 953 | + } else if (current->vps_num_ols_timing_hrd_params_minus1 == 0) { |
| 954 | + for (i = 0; i < num_multi_layer_olss; i++) |
| 955 | + infer(vps_ols_timing_hrd_idx[i], 0); |
| 956 | + } else { |
| 957 | + for (i = 0; i < num_multi_layer_olss; i++) |
| 958 | + infer(vps_ols_timing_hrd_idx[i], i); |
| 959 | + } |
| 960 | + } |
| 961 | + } |
| 962 | + |
| 963 | + flag(vps_extension_flag); |
| 964 | + if (current->vps_extension_flag) |
| 965 | + CHECK(FUNC(extension_data) (ctx, rw, ¤t->extension_data)); |
| 966 | + CHECK(FUNC(rbsp_trailing_bits) (ctx, rw)); |
| 967 | + |
| 968 | + return 0; |
| 969 | +} |
| 970 | + |
| 971 | + |
| 972 | +static int FUNC(sps)(CodedBitstreamContext *ctx, RWContext *rw, |
| 973 | + H266RawSPS *current) |
| 974 | +{ |
| 975 | + CodedBitstreamH266Context *h266 = ctx->priv_data; |
| 976 | + int err, i, j; |
| 977 | + unsigned int ctb_log2_size_y, min_cb_log2_size_y, |
| 978 | + min_qt_log2_size_intra_y, min_qt_log2_size_inter_y, |
| 979 | + ctb_size_y, max_num_merge_cand, tmp_width_val, tmp_height_val; |
| 980 | + uint8_t qp_bd_offset; |
| 981 | + |
| 982 | + static const uint8_t h266_sub_width_c[] = { |
| 983 | + 1, 2, 2, 1 |
| 984 | + }; |
| 985 | + static const uint8_t h266_sub_height_c[] = { |
| 986 | + 1, 2, 1, 1 |
| 987 | + }; |
| 988 | + |
| 989 | + HEADER("Sequence Parameter Set"); |
| 990 | + |
| 991 | + CHECK(FUNC(nal_unit_header) (ctx, rw, |
| 992 | + ¤t->nal_unit_header, VVC_SPS_NUT)); |
| 993 | + |
| 994 | + ub(4, sps_seq_parameter_set_id); |
| 995 | + ub(4, sps_video_parameter_set_id); |
| 996 | + if (current->sps_video_parameter_set_id == 0 && !h266->vps_ref[0]) { |
| 997 | + H266RawVPS *vps; |
| 998 | + AVBufferRef *ref = av_buffer_allocz(sizeof(H266RawVPS)); |
| 999 | + if (!ref) { |
| 1000 | + return AVERROR(ENOMEM); |
| 1001 | + } |
| 1002 | + vps = (H266RawVPS *) ref->data; |
| 1003 | + vps->vps_max_layers_minus1 = 0; |
| 1004 | + vps->vps_independent_layer_flag[0] = 1; |
| 1005 | + vps->vps_layer_id[0] = current->nal_unit_header.nuh_layer_id; |
| 1006 | + h266->vps_ref[0] = ref; |
| 1007 | + h266->vps[0] = vps; |
| 1008 | + } |
| 1009 | + |
| 1010 | + u(3, sps_max_sublayers_minus1, 0, VVC_MAX_SUBLAYERS - 1); |
| 1011 | + u(2, sps_chroma_format_idc, 0, 3); |
| 1012 | + u(2, sps_log2_ctu_size_minus5, 0, 3); |
| 1013 | + ctb_log2_size_y = current->sps_log2_ctu_size_minus5 + 5; |
| 1014 | + ctb_size_y = 1 << ctb_log2_size_y; |
| 1015 | + |
| 1016 | + flag(sps_ptl_dpb_hrd_params_present_flag); |
| 1017 | + if (current->sps_ptl_dpb_hrd_params_present_flag) { |
| 1018 | + CHECK(FUNC(profile_tier_level) (ctx, rw, ¤t->profile_tier_level, |
| 1019 | + 1, current->sps_max_sublayers_minus1)); |
| 1020 | + } |
| 1021 | + flag(sps_gdr_enabled_flag); |
| 1022 | + flag(sps_ref_pic_resampling_enabled_flag); |
| 1023 | + if (current->sps_ref_pic_resampling_enabled_flag) |
| 1024 | + flag(sps_res_change_in_clvs_allowed_flag); |
| 1025 | + else |
| 1026 | + infer(sps_res_change_in_clvs_allowed_flag, 0); |
| 1027 | + |
| 1028 | + ue(sps_pic_width_max_in_luma_samples, 1, VVC_MAX_WIDTH); |
| 1029 | + ue(sps_pic_height_max_in_luma_samples, 1, VVC_MAX_HEIGHT); |
| 1030 | + |
| 1031 | + flag(sps_conformance_window_flag); |
| 1032 | + if (current->sps_conformance_window_flag) { |
| 1033 | + uint8_t sub_width_c = h266_sub_width_c[current->sps_chroma_format_idc]; |
| 1034 | + uint8_t sub_height_c = h266_sub_height_c[current->sps_chroma_format_idc]; |
| 1035 | + uint16_t width = current->sps_pic_width_max_in_luma_samples / sub_width_c; |
| 1036 | + uint16_t height = current->sps_pic_height_max_in_luma_samples / sub_height_c; |
| 1037 | + ue(sps_conf_win_left_offset, 0, width); |
| 1038 | + ue(sps_conf_win_right_offset, 0, width - current->sps_conf_win_left_offset); |
| 1039 | + ue(sps_conf_win_top_offset, 0, height); |
| 1040 | + ue(sps_conf_win_bottom_offset, 0, height - current->sps_conf_win_top_offset); |
| 1041 | + } else { |
| 1042 | + infer(sps_conf_win_left_offset, 0); |
| 1043 | + infer(sps_conf_win_right_offset, 0); |
| 1044 | + infer(sps_conf_win_top_offset, 0); |
| 1045 | + infer(sps_conf_win_bottom_offset, 0); |
| 1046 | + } |
| 1047 | + |
| 1048 | + tmp_width_val = AV_CEIL_RSHIFT(current->sps_pic_width_max_in_luma_samples, |
| 1049 | + ctb_log2_size_y); |
| 1050 | + tmp_height_val = AV_CEIL_RSHIFT(current->sps_pic_height_max_in_luma_samples, |
| 1051 | + ctb_log2_size_y); |
| 1052 | + |
| 1053 | + flag(sps_subpic_info_present_flag); |
| 1054 | + if (current->sps_subpic_info_present_flag) { |
| 1055 | + ue(sps_num_subpics_minus1, 1, VVC_MAX_SLICES - 1); |
| 1056 | + if (current->sps_num_subpics_minus1 > 0) { |
| 1057 | + flag(sps_independent_subpics_flag); |
| 1058 | + flag(sps_subpic_same_size_flag); |
| 1059 | + } |
| 1060 | + |
| 1061 | + if (current->sps_num_subpics_minus1 > 0) { |
| 1062 | + int wlen = av_ceil_log2(tmp_width_val); |
| 1063 | + int hlen = av_ceil_log2(tmp_height_val); |
| 1064 | + if (current->sps_pic_width_max_in_luma_samples > ctb_size_y) |
| 1065 | + ubs(wlen, sps_subpic_width_minus1[0], 1, 0); |
| 1066 | + else |
| 1067 | + infer(sps_subpic_width_minus1[i], tmp_width_val - 1); |
| 1068 | + if (current->sps_pic_height_max_in_luma_samples > ctb_size_y) |
| 1069 | + ubs(hlen, sps_subpic_height_minus1[0], 1, 0); |
| 1070 | + else |
| 1071 | + infer(sps_subpic_height_minus1[0], tmp_height_val); |
| 1072 | + if (!current->sps_independent_subpics_flag) { |
| 1073 | + flags(sps_subpic_treated_as_pic_flag[0], 1, 0); |
| 1074 | + flags(sps_loop_filter_across_subpic_enabled_flag[0], 1, 0); |
| 1075 | + } else { |
| 1076 | + infer(sps_subpic_treated_as_pic_flag[0], 1); |
| 1077 | + infer(sps_loop_filter_across_subpic_enabled_flag[0], 1); |
| 1078 | + } |
| 1079 | + for (i = 1; i <= current->sps_num_subpics_minus1; i++) { |
| 1080 | + if (!current->sps_subpic_same_size_flag) { |
| 1081 | + if (current->sps_pic_width_max_in_luma_samples > ctb_size_y) |
| 1082 | + ubs(wlen, sps_subpic_ctu_top_left_x[i], 1, i); |
| 1083 | + else |
| 1084 | + infer(sps_subpic_ctu_top_left_x[i], 0); |
| 1085 | + if (current->sps_pic_height_max_in_luma_samples > |
| 1086 | + ctb_size_y) |
| 1087 | + ubs(hlen, sps_subpic_ctu_top_left_y[i], 1, i); |
| 1088 | + else |
| 1089 | + infer(sps_subpic_ctu_top_left_y[i], 0); |
| 1090 | + if (i < current->sps_num_subpics_minus1 && |
| 1091 | + current->sps_pic_width_max_in_luma_samples > |
| 1092 | + ctb_size_y) { |
| 1093 | + ubs(wlen, sps_subpic_width_minus1[i], 1, i); |
| 1094 | + } else { |
| 1095 | + infer(sps_subpic_width_minus1[i], |
| 1096 | + tmp_width_val - |
| 1097 | + current->sps_subpic_ctu_top_left_x[i] - 1); |
| 1098 | + } |
| 1099 | + if (i < current->sps_num_subpics_minus1 && |
| 1100 | + current->sps_pic_height_max_in_luma_samples > |
| 1101 | + ctb_size_y) { |
| 1102 | + ubs(hlen, sps_subpic_height_minus1[i], 1, i); |
| 1103 | + } else { |
| 1104 | + infer(sps_subpic_height_minus1[i], |
| 1105 | + tmp_height_val - |
| 1106 | + current->sps_subpic_ctu_top_left_y[i] - 1); |
| 1107 | + } |
| 1108 | + } else { |
| 1109 | + int num_subpic_cols = tmp_width_val / |
| 1110 | + (current->sps_subpic_width_minus1[0] + 1); |
| 1111 | + infer(sps_subpic_ctu_top_left_x[i], |
| 1112 | + (i % num_subpic_cols) * |
| 1113 | + (current->sps_subpic_width_minus1[0] + 1)); |
| 1114 | + infer(sps_subpic_ctu_top_left_y[i], |
| 1115 | + (i / num_subpic_cols) * |
| 1116 | + (current->sps_subpic_height_minus1[0] + 1)); |
| 1117 | + infer(sps_subpic_width_minus1[i], |
| 1118 | + current->sps_subpic_width_minus1[0]); |
| 1119 | + infer(sps_subpic_height_minus1[i], |
| 1120 | + current->sps_subpic_height_minus1[0]); |
| 1121 | + } |
| 1122 | + if (!current->sps_independent_subpics_flag) { |
| 1123 | + flags(sps_subpic_treated_as_pic_flag[i], 1, i); |
| 1124 | + flags(sps_loop_filter_across_subpic_enabled_flag[i], 1, i); |
| 1125 | + } else { |
| 1126 | + infer(sps_subpic_treated_as_pic_flag[i], 1); |
| 1127 | + infer(sps_loop_filter_across_subpic_enabled_flag[i], 0); |
| 1128 | + } |
| 1129 | + } |
| 1130 | + ue(sps_subpic_id_len_minus1, 0, 15); |
| 1131 | + if ((1 << (current->sps_subpic_id_len_minus1 + 1)) < |
| 1132 | + current->sps_num_subpics_minus1 + 1) { |
| 1133 | + av_log(ctx->log_ctx, AV_LOG_ERROR, |
| 1134 | + "sps_subpic_id_len_minus1(%d) is too small\n", |
| 1135 | + current->sps_subpic_id_len_minus1); |
| 1136 | + return AVERROR_INVALIDDATA; |
| 1137 | + } |
| 1138 | + flag(sps_subpic_id_mapping_explicitly_signalled_flag); |
| 1139 | + if (current->sps_subpic_id_mapping_explicitly_signalled_flag) { |
| 1140 | + flag(sps_subpic_id_mapping_present_flag); |
| 1141 | + if (current->sps_subpic_id_mapping_present_flag) { |
| 1142 | + for (i = 0; i <= current->sps_num_subpics_minus1; i++) { |
| 1143 | + ubs(current->sps_subpic_id_len_minus1 + 1, |
| 1144 | + sps_subpic_id[i], 1, i); |
| 1145 | + } |
| 1146 | + } |
| 1147 | + } |
| 1148 | + } else { |
| 1149 | + infer(sps_subpic_ctu_top_left_x[0], 0); |
| 1150 | + infer(sps_subpic_ctu_top_left_y[0], 0); |
| 1151 | + infer(sps_subpic_width_minus1[0], tmp_width_val - 1); |
| 1152 | + infer(sps_subpic_height_minus1[0], tmp_height_val - 1); |
| 1153 | + } |
| 1154 | + } else { |
| 1155 | + infer(sps_num_subpics_minus1, 0); |
| 1156 | + infer(sps_independent_subpics_flag, 1); |
| 1157 | + infer(sps_subpic_same_size_flag, 0); |
| 1158 | + infer(sps_subpic_id_mapping_explicitly_signalled_flag, 0); |
| 1159 | + infer(sps_subpic_ctu_top_left_x[0], 0); |
| 1160 | + infer(sps_subpic_ctu_top_left_y[0], 0); |
| 1161 | + infer(sps_subpic_width_minus1[0], tmp_width_val - 1); |
| 1162 | + infer(sps_subpic_height_minus1[0], tmp_height_val - 1); |
| 1163 | + } |
| 1164 | + |
| 1165 | + |
| 1166 | + ue(sps_bitdepth_minus8, 0, 2); |
| 1167 | + qp_bd_offset = 6 * current->sps_bitdepth_minus8; |
| 1168 | + |
| 1169 | + flag(sps_entropy_coding_sync_enabled_flag); |
| 1170 | + flag(sps_entry_point_offsets_present_flag); |
| 1171 | + |
| 1172 | + u(4, sps_log2_max_pic_order_cnt_lsb_minus4, 0, 12); |
| 1173 | + flag(sps_poc_msb_cycle_flag); |
| 1174 | + if (current->sps_poc_msb_cycle_flag) |
| 1175 | + ue(sps_poc_msb_cycle_len_minus1, |
| 1176 | + 0, 32 - current->sps_log2_max_pic_order_cnt_lsb_minus4 - 5); |
| 1177 | + |
| 1178 | + u(2, sps_num_extra_ph_bytes, 0, 2); |
| 1179 | + for (i = 0; i < (current->sps_num_extra_ph_bytes * 8); i++) { |
| 1180 | + flags(sps_extra_ph_bit_present_flag[i], 1, i); |
| 1181 | + } |
| 1182 | + |
| 1183 | + u(2, sps_num_extra_sh_bytes, 0, 2); |
| 1184 | + for (i = 0; i < (current->sps_num_extra_sh_bytes * 8); i++) { |
| 1185 | + flags(sps_extra_sh_bit_present_flag[i], 1, i); |
| 1186 | + } |
| 1187 | + |
| 1188 | + if (current->sps_ptl_dpb_hrd_params_present_flag) { |
| 1189 | + if (current->sps_max_sublayers_minus1 > 0) |
| 1190 | + flag(sps_sublayer_dpb_params_flag); |
| 1191 | + else |
| 1192 | + infer(sps_sublayer_dpb_params_flag, 0); |
| 1193 | + CHECK(FUNC(dpb_parameters) (ctx, rw, ¤t->sps_dpb_params, |
| 1194 | + current->sps_max_sublayers_minus1, |
| 1195 | + current->sps_sublayer_dpb_params_flag)); |
| 1196 | + } |
| 1197 | + |
| 1198 | + ue(sps_log2_min_luma_coding_block_size_minus2, |
| 1199 | + 0, FFMIN(4, current->sps_log2_ctu_size_minus5 + 3)); |
| 1200 | + min_cb_log2_size_y = |
| 1201 | + current->sps_log2_min_luma_coding_block_size_minus2 + 2; |
| 1202 | + |
| 1203 | + flag(sps_partition_constraints_override_enabled_flag); |
| 1204 | + |
| 1205 | + ue(sps_log2_diff_min_qt_min_cb_intra_slice_luma, |
| 1206 | + 0, FFMIN(6, ctb_log2_size_y) - min_cb_log2_size_y); |
| 1207 | + min_qt_log2_size_intra_y = |
| 1208 | + current->sps_log2_diff_min_qt_min_cb_intra_slice_luma + |
| 1209 | + min_cb_log2_size_y; |
| 1210 | + |
| 1211 | + ue(sps_max_mtt_hierarchy_depth_intra_slice_luma, |
| 1212 | + 0, 2 * (ctb_log2_size_y - min_cb_log2_size_y)); |
| 1213 | + |
| 1214 | + if (current->sps_max_mtt_hierarchy_depth_intra_slice_luma != 0) { |
| 1215 | + ue(sps_log2_diff_max_bt_min_qt_intra_slice_luma, |
| 1216 | + 0, ctb_log2_size_y - min_qt_log2_size_intra_y); |
| 1217 | + ue(sps_log2_diff_max_tt_min_qt_intra_slice_luma, |
| 1218 | + 0, FFMIN(6, ctb_log2_size_y) - min_qt_log2_size_intra_y); |
| 1219 | + } else { |
| 1220 | + infer(sps_log2_diff_max_bt_min_qt_intra_slice_luma, 0); |
| 1221 | + infer(sps_log2_diff_max_tt_min_qt_intra_slice_luma, 0); |
| 1222 | + } |
| 1223 | + |
| 1224 | + if (current->sps_chroma_format_idc != 0) { |
| 1225 | + flag(sps_qtbtt_dual_tree_intra_flag); |
| 1226 | + } else { |
| 1227 | + infer(sps_qtbtt_dual_tree_intra_flag, 0); |
| 1228 | + } |
| 1229 | + |
| 1230 | + if (current->sps_qtbtt_dual_tree_intra_flag) { |
| 1231 | + ue(sps_log2_diff_min_qt_min_cb_intra_slice_chroma, |
| 1232 | + 0, FFMIN(6, ctb_log2_size_y) - min_cb_log2_size_y); |
| 1233 | + ue(sps_max_mtt_hierarchy_depth_intra_slice_chroma, |
| 1234 | + 0, 2 * (ctb_log2_size_y - min_cb_log2_size_y)); |
| 1235 | + if (current->sps_max_mtt_hierarchy_depth_intra_slice_chroma != 0) { |
| 1236 | + unsigned int min_qt_log2_size_intra_c = |
| 1237 | + current->sps_log2_diff_min_qt_min_cb_intra_slice_chroma + |
| 1238 | + min_cb_log2_size_y; |
| 1239 | + ue(sps_log2_diff_max_bt_min_qt_intra_slice_chroma, |
| 1240 | + 0, FFMIN(6, ctb_log2_size_y) - min_qt_log2_size_intra_c); |
| 1241 | + ue(sps_log2_diff_max_tt_min_qt_intra_slice_chroma, |
| 1242 | + 0, FFMIN(6, ctb_log2_size_y) - min_qt_log2_size_intra_c); |
| 1243 | + } |
| 1244 | + } else { |
| 1245 | + infer(sps_log2_diff_min_qt_min_cb_intra_slice_chroma, 0); |
| 1246 | + infer(sps_max_mtt_hierarchy_depth_intra_slice_chroma, 0); |
| 1247 | + } |
| 1248 | + if (current->sps_max_mtt_hierarchy_depth_intra_slice_chroma == 0) { |
| 1249 | + infer(sps_log2_diff_max_bt_min_qt_intra_slice_chroma, 0); |
| 1250 | + infer(sps_log2_diff_max_tt_min_qt_intra_slice_chroma, 0); |
| 1251 | + } |
| 1252 | + |
| 1253 | + ue(sps_log2_diff_min_qt_min_cb_inter_slice, |
| 1254 | + 0, FFMIN(6, ctb_log2_size_y) - min_cb_log2_size_y); |
| 1255 | + min_qt_log2_size_inter_y = |
| 1256 | + current->sps_log2_diff_min_qt_min_cb_inter_slice + min_cb_log2_size_y; |
| 1257 | + |
| 1258 | + ue(sps_max_mtt_hierarchy_depth_inter_slice, |
| 1259 | + 0, 2 * (ctb_log2_size_y - min_cb_log2_size_y)); |
| 1260 | + if (current->sps_max_mtt_hierarchy_depth_inter_slice != 0) { |
| 1261 | + ue(sps_log2_diff_max_bt_min_qt_inter_slice, |
| 1262 | + 0, ctb_log2_size_y - min_qt_log2_size_inter_y); |
| 1263 | + ue(sps_log2_diff_max_tt_min_qt_inter_slice, |
| 1264 | + 0, FFMIN(6, ctb_log2_size_y) - min_qt_log2_size_inter_y); |
| 1265 | + } else { |
| 1266 | + infer(sps_log2_diff_max_bt_min_qt_inter_slice, 0); |
| 1267 | + infer(sps_log2_diff_max_tt_min_qt_inter_slice, 0); |
| 1268 | + } |
| 1269 | + |
| 1270 | + if (ctb_size_y > 32) |
| 1271 | + flag(sps_max_luma_transform_size_64_flag); |
| 1272 | + else |
| 1273 | + infer(sps_max_luma_transform_size_64_flag, 0); |
| 1274 | + |
| 1275 | + flag(sps_transform_skip_enabled_flag); |
| 1276 | + if (current->sps_transform_skip_enabled_flag) { |
| 1277 | + ue(sps_log2_transform_skip_max_size_minus2, 0, 3); |
| 1278 | + flag(sps_bdpcm_enabled_flag); |
| 1279 | + } |
| 1280 | + |
| 1281 | + flag(sps_mts_enabled_flag); |
| 1282 | + if (current->sps_mts_enabled_flag) { |
| 1283 | + flag(sps_explicit_mts_intra_enabled_flag); |
| 1284 | + flag(sps_explicit_mts_inter_enabled_flag); |
| 1285 | + } else { |
| 1286 | + infer(sps_explicit_mts_intra_enabled_flag, 0); |
| 1287 | + infer(sps_explicit_mts_inter_enabled_flag, 0); |
| 1288 | + } |
| 1289 | + |
| 1290 | + flag(sps_lfnst_enabled_flag); |
| 1291 | + |
| 1292 | + if (current->sps_chroma_format_idc != 0) { |
| 1293 | + uint8_t num_qp_tables; |
| 1294 | + flag(sps_joint_cbcr_enabled_flag); |
| 1295 | + flag(sps_same_qp_table_for_chroma_flag); |
| 1296 | + num_qp_tables = current->sps_same_qp_table_for_chroma_flag ? |
| 1297 | + 1 : (current->sps_joint_cbcr_enabled_flag ? 3 : 2); |
| 1298 | + for (i = 0; i < num_qp_tables; i++) { |
| 1299 | + ses(sps_qp_table_start_minus26[i], -26 - qp_bd_offset, 36, 1, i); |
| 1300 | + ues(sps_num_points_in_qp_table_minus1[i], |
| 1301 | + 0, 36 - current->sps_qp_table_start_minus26[i], 1, i); |
| 1302 | + for (j = 0; j <= current->sps_num_points_in_qp_table_minus1[i]; j++) { |
| 1303 | + uint8_t max = MAX_UINT_BITS(8); |
| 1304 | + ues(sps_delta_qp_in_val_minus1[i][j], 0, max, 2, i, j); |
| 1305 | + ues(sps_delta_qp_diff_val[i][j], 0, max, 2, i, j); |
| 1306 | + } |
| 1307 | + } |
| 1308 | + } else { |
| 1309 | + infer(sps_joint_cbcr_enabled_flag, 0); |
| 1310 | + infer(sps_same_qp_table_for_chroma_flag, 0); |
| 1311 | + } |
| 1312 | + |
| 1313 | + flag(sps_sao_enabled_flag); |
| 1314 | + flag(sps_alf_enabled_flag); |
| 1315 | + if (current->sps_alf_enabled_flag && current->sps_chroma_format_idc) |
| 1316 | + flag(sps_ccalf_enabled_flag); |
| 1317 | + else |
| 1318 | + infer(sps_ccalf_enabled_flag, 0); |
| 1319 | + flag(sps_lmcs_enabled_flag); |
| 1320 | + flag(sps_weighted_pred_flag); |
| 1321 | + flag(sps_weighted_bipred_flag); |
| 1322 | + flag(sps_long_term_ref_pics_flag); |
| 1323 | + if (current->sps_video_parameter_set_id > 0) |
| 1324 | + flag(sps_inter_layer_prediction_enabled_flag); |
| 1325 | + else |
| 1326 | + infer(sps_inter_layer_prediction_enabled_flag, 0); |
| 1327 | + flag(sps_idr_rpl_present_flag); |
| 1328 | + flag(sps_rpl1_same_as_rpl0_flag); |
| 1329 | + |
| 1330 | + for (i = 0; i < (current->sps_rpl1_same_as_rpl0_flag ? 1 : 2); i++) { |
| 1331 | + ues(sps_num_ref_pic_lists[i], 0, VVC_MAX_REF_PIC_LISTS, 1, i); |
| 1332 | + for (j = 0; j < current->sps_num_ref_pic_lists[i]; j++) |
| 1333 | + CHECK(FUNC(ref_pic_list_struct) (ctx, rw, |
| 1334 | + ¤t-> |
| 1335 | + sps_ref_pic_list_struct[i][j], i, |
| 1336 | + j, current)); |
| 1337 | + } |
| 1338 | + |
| 1339 | + if (current->sps_rpl1_same_as_rpl0_flag) { |
| 1340 | + current->sps_num_ref_pic_lists[1] = current->sps_num_ref_pic_lists[0]; |
| 1341 | + for (j = 0; j < current->sps_num_ref_pic_lists[0]; j++) |
| 1342 | + memcpy(¤t->sps_ref_pic_list_struct[1][j], |
| 1343 | + ¤t->sps_ref_pic_list_struct[0][j], |
| 1344 | + sizeof(current->sps_ref_pic_list_struct[0][j])); |
| 1345 | + } |
| 1346 | + |
| 1347 | + flag(sps_ref_wraparound_enabled_flag); |
| 1348 | + |
| 1349 | + flag(sps_temporal_mvp_enabled_flag); |
| 1350 | + if (current->sps_temporal_mvp_enabled_flag) |
| 1351 | + flag(sps_sbtmvp_enabled_flag); |
| 1352 | + else |
| 1353 | + infer(sps_sbtmvp_enabled_flag, 0); |
| 1354 | + |
| 1355 | + flag(sps_amvr_enabled_flag); |
| 1356 | + flag(sps_bdof_enabled_flag); |
| 1357 | + if (current->sps_bdof_enabled_flag) |
| 1358 | + flag(sps_bdof_control_present_in_ph_flag); |
| 1359 | + else |
| 1360 | + infer(sps_bdof_control_present_in_ph_flag, 0); |
| 1361 | + |
| 1362 | + flag(sps_smvd_enabled_flag); |
| 1363 | + flag(sps_dmvr_enabled_flag); |
| 1364 | + if (current->sps_dmvr_enabled_flag) |
| 1365 | + flag(sps_dmvr_control_present_in_ph_flag); |
| 1366 | + else |
| 1367 | + infer(sps_dmvr_control_present_in_ph_flag, 0); |
| 1368 | + |
| 1369 | + flag(sps_mmvd_enabled_flag); |
| 1370 | + if (current->sps_mmvd_enabled_flag) |
| 1371 | + flag(sps_mmvd_fullpel_only_enabled_flag); |
| 1372 | + else |
| 1373 | + infer(sps_mmvd_fullpel_only_enabled_flag, 0); |
| 1374 | + |
| 1375 | + ue(sps_six_minus_max_num_merge_cand, 0, 5); |
| 1376 | + max_num_merge_cand = 6 - current->sps_six_minus_max_num_merge_cand; |
| 1377 | + |
| 1378 | + flag(sps_sbt_enabled_flag); |
| 1379 | + |
| 1380 | + flag(sps_affine_enabled_flag); |
| 1381 | + if (current->sps_affine_enabled_flag) { |
| 1382 | + ue(sps_five_minus_max_num_subblock_merge_cand, |
| 1383 | + 0, 5 - current->sps_sbtmvp_enabled_flag); |
| 1384 | + flag(sps_6param_affine_enabled_flag); |
| 1385 | + if (current->sps_amvr_enabled_flag) |
| 1386 | + flag(sps_affine_amvr_enabled_flag); |
| 1387 | + else |
| 1388 | + infer(sps_affine_amvr_enabled_flag, 0); |
| 1389 | + flag(sps_affine_prof_enabled_flag); |
| 1390 | + if (current->sps_affine_prof_enabled_flag) |
| 1391 | + flag(sps_prof_control_present_in_ph_flag); |
| 1392 | + else |
| 1393 | + infer(sps_prof_control_present_in_ph_flag, 0); |
| 1394 | + } else { |
| 1395 | + infer(sps_6param_affine_enabled_flag, 0); |
| 1396 | + infer(sps_affine_amvr_enabled_flag, 0); |
| 1397 | + infer(sps_affine_prof_enabled_flag, 0); |
| 1398 | + infer(sps_prof_control_present_in_ph_flag, 0); |
| 1399 | + } |
| 1400 | + |
| 1401 | + flag(sps_bcw_enabled_flag); |
| 1402 | + flag(sps_ciip_enabled_flag); |
| 1403 | + |
| 1404 | + if (max_num_merge_cand >= 2) { |
| 1405 | + flag(sps_gpm_enabled_flag); |
| 1406 | + if (current->sps_gpm_enabled_flag && max_num_merge_cand >= 3) |
| 1407 | + ue(sps_max_num_merge_cand_minus_max_num_gpm_cand, |
| 1408 | + 0, max_num_merge_cand - 2); |
| 1409 | + } else { |
| 1410 | + infer(sps_gpm_enabled_flag, 0); |
| 1411 | + } |
| 1412 | + |
| 1413 | + ue(sps_log2_parallel_merge_level_minus2, 0, ctb_log2_size_y - 2); |
| 1414 | + |
| 1415 | + flag(sps_isp_enabled_flag); |
| 1416 | + flag(sps_mrl_enabled_flag); |
| 1417 | + flag(sps_mip_enabled_flag); |
| 1418 | + |
| 1419 | + if (current->sps_chroma_format_idc != 0) |
| 1420 | + flag(sps_cclm_enabled_flag); |
| 1421 | + else |
| 1422 | + infer(sps_cclm_enabled_flag, 0); |
| 1423 | + if (current->sps_chroma_format_idc == 1) { |
| 1424 | + flag(sps_chroma_horizontal_collocated_flag); |
| 1425 | + flag(sps_chroma_vertical_collocated_flag); |
| 1426 | + } else { |
| 1427 | + infer(sps_chroma_horizontal_collocated_flag, 1); |
| 1428 | + infer(sps_chroma_vertical_collocated_flag, 1); |
| 1429 | + } |
| 1430 | + |
| 1431 | + flag(sps_palette_enabled_flag); |
| 1432 | + if (current->sps_chroma_format_idc == 3 && |
| 1433 | + !current->sps_max_luma_transform_size_64_flag) |
| 1434 | + flag(sps_act_enabled_flag); |
| 1435 | + else |
| 1436 | + infer(sps_act_enabled_flag, 0); |
| 1437 | + if (current->sps_transform_skip_enabled_flag || |
| 1438 | + current->sps_palette_enabled_flag) |
| 1439 | + ue(sps_min_qp_prime_ts, 0, 8); |
| 1440 | + |
| 1441 | + flag(sps_ibc_enabled_flag); |
| 1442 | + if (current->sps_ibc_enabled_flag) |
| 1443 | + ue(sps_six_minus_max_num_ibc_merge_cand, 0, 5); |
| 1444 | + |
| 1445 | + flag(sps_ladf_enabled_flag); |
| 1446 | + if (current->sps_ladf_enabled_flag) { |
| 1447 | + ub(2, sps_num_ladf_intervals_minus2); |
| 1448 | + se(sps_ladf_lowest_interval_qp_offset, -63, 63); |
| 1449 | + for (i = 0; i < current->sps_num_ladf_intervals_minus2 + 1; i++) { |
| 1450 | + ses(sps_ladf_qp_offset[i], -63, 63, 1, i); |
| 1451 | + ues(sps_ladf_delta_threshold_minus1[i], |
| 1452 | + 0, (2 << (8 + current->sps_bitdepth_minus8)) - 3, 1, i); |
| 1453 | + } |
| 1454 | + } |
| 1455 | + |
| 1456 | + flag(sps_explicit_scaling_list_enabled_flag); |
| 1457 | + if (current->sps_lfnst_enabled_flag && |
| 1458 | + current->sps_explicit_scaling_list_enabled_flag) |
| 1459 | + flag(sps_scaling_matrix_for_lfnst_disabled_flag); |
| 1460 | + |
| 1461 | + if (current->sps_act_enabled_flag && |
| 1462 | + current->sps_explicit_scaling_list_enabled_flag) |
| 1463 | + flag(sps_scaling_matrix_for_alternative_colour_space_disabled_flag); |
| 1464 | + else |
| 1465 | + infer(sps_scaling_matrix_for_alternative_colour_space_disabled_flag, 0); |
| 1466 | + if (current->sps_scaling_matrix_for_alternative_colour_space_disabled_flag) |
| 1467 | + flag(sps_scaling_matrix_designated_colour_space_flag); |
| 1468 | + |
| 1469 | + flag(sps_dep_quant_enabled_flag); |
| 1470 | + flag(sps_sign_data_hiding_enabled_flag); |
| 1471 | + |
| 1472 | + flag(sps_virtual_boundaries_enabled_flag); |
| 1473 | + if (current->sps_virtual_boundaries_enabled_flag) { |
| 1474 | + flag(sps_virtual_boundaries_present_flag); |
| 1475 | + if (current->sps_virtual_boundaries_present_flag) { |
| 1476 | + ue(sps_num_ver_virtual_boundaries, |
| 1477 | + 0, current->sps_pic_width_max_in_luma_samples <= 8 ? 0 : 3); |
| 1478 | + for (i = 0; i < current->sps_num_ver_virtual_boundaries; i++) |
| 1479 | + ues(sps_virtual_boundary_pos_x_minus1[i], |
| 1480 | + 0, (current->sps_pic_width_max_in_luma_samples + 7) / 8 - 2, |
| 1481 | + 1, i); |
| 1482 | + ue(sps_num_hor_virtual_boundaries, |
| 1483 | + 0, current->sps_pic_height_max_in_luma_samples <= 8 ? 0 : 3); |
| 1484 | + for (i = 0; i < current->sps_num_hor_virtual_boundaries; i++) |
| 1485 | + ues(sps_virtual_boundary_pos_y_minus1[i], |
| 1486 | + 0, (current->sps_pic_height_max_in_luma_samples + 7) / |
| 1487 | + 8 - 2, 1, i); |
| 1488 | + } |
| 1489 | + } else { |
| 1490 | + infer(sps_virtual_boundaries_present_flag, 0); |
| 1491 | + infer(sps_num_ver_virtual_boundaries, 0); |
| 1492 | + infer(sps_num_hor_virtual_boundaries, 0); |
| 1493 | + } |
| 1494 | + |
| 1495 | + if (current->sps_ptl_dpb_hrd_params_present_flag) { |
| 1496 | + flag(sps_timing_hrd_params_present_flag); |
| 1497 | + if (current->sps_timing_hrd_params_present_flag) { |
| 1498 | + uint8_t first_sublayer; |
| 1499 | + CHECK(FUNC(general_timing_hrd_parameters) (ctx, rw, |
| 1500 | + ¤t->sps_general_timing_hrd_parameters)); |
| 1501 | + if (current->sps_max_sublayers_minus1 > 0) |
| 1502 | + flag(sps_sublayer_cpb_params_present_flag); |
| 1503 | + else |
| 1504 | + infer(sps_sublayer_cpb_params_present_flag, 0); |
| 1505 | + first_sublayer = current->sps_sublayer_cpb_params_present_flag ? |
| 1506 | + 0 : current->sps_max_sublayers_minus1; |
| 1507 | + CHECK(FUNC(ols_timing_hrd_parameters) (ctx, rw, |
| 1508 | + ¤t->sps_ols_timing_hrd_parameters, first_sublayer, |
| 1509 | + current->sps_max_sublayers_minus1, |
| 1510 | + ¤t->sps_general_timing_hrd_parameters)); |
| 1511 | + } |
| 1512 | + } |
| 1513 | + |
| 1514 | + flag(sps_field_seq_flag); |
| 1515 | + flag(sps_vui_parameters_present_flag); |
| 1516 | + if (current->sps_vui_parameters_present_flag) { |
| 1517 | + ue(sps_vui_payload_size_minus1, 0, 1023); |
| 1518 | + while (byte_alignment(rw) != 0) |
| 1519 | + fixed(1, sps_vui_alignment_zero_bit, 0); |
| 1520 | + CHECK(FUNC(vui_payload) (ctx, rw, ¤t->vui, |
| 1521 | + current->sps_vui_payload_size_minus1 + 1, |
| 1522 | + current->sps_chroma_format_idc)); |
| 1523 | + } else { |
| 1524 | + CHECK(FUNC(vui_parameters_default) (ctx, rw, ¤t->vui)); |
| 1525 | + } |
| 1526 | + flag(sps_extension_flag); |
| 1527 | + if (current->sps_extension_flag) |
| 1528 | + CHECK(FUNC(extension_data) (ctx, rw, ¤t->extension_data)); |
| 1529 | + |
| 1530 | + CHECK(FUNC(rbsp_trailing_bits) (ctx, rw)); |
| 1531 | + |
| 1532 | + return 0; |
| 1533 | +} |
| 1534 | + |
| 1535 | +static int FUNC(pps) (CodedBitstreamContext *ctx, RWContext *rw, |
| 1536 | + H266RawPPS *current) |
| 1537 | +{ |
| 1538 | + CodedBitstreamH266Context *h266 = ctx->priv_data; |
| 1539 | + const H266RawSPS *sps; |
| 1540 | + int err, i; |
| 1541 | + unsigned int min_cb_size_y, divisor, ctb_size_y, |
| 1542 | + pic_width_in_ctbs_y, pic_height_in_ctbs_y; |
| 1543 | + uint8_t sub_width_c, sub_height_c, qp_bd_offset; |
| 1544 | + |
| 1545 | + static const uint8_t h266_sub_width_c[] = { |
| 1546 | + 1, 2, 2, 1 |
| 1547 | + }; |
| 1548 | + static const uint8_t h266_sub_height_c[] = { |
| 1549 | + 1, 2, 1, 1 |
| 1550 | + }; |
| 1551 | + |
| 1552 | + HEADER("Picture Parameter Set"); |
| 1553 | + |
| 1554 | + CHECK(FUNC(nal_unit_header) (ctx, rw, |
| 1555 | + ¤t->nal_unit_header, VVC_PPS_NUT)); |
| 1556 | + |
| 1557 | + ub(6, pps_pic_parameter_set_id); |
| 1558 | + ub(4, pps_seq_parameter_set_id); |
| 1559 | + sps = h266->sps[current->pps_seq_parameter_set_id]; |
| 1560 | + if (!sps) { |
| 1561 | + av_log(ctx->log_ctx, AV_LOG_ERROR, "SPS id %d not available.\n", |
| 1562 | + current->pps_seq_parameter_set_id); |
| 1563 | + return AVERROR_INVALIDDATA; |
| 1564 | + } |
| 1565 | + |
| 1566 | + flag(pps_mixed_nalu_types_in_pic_flag); |
| 1567 | + ue(pps_pic_width_in_luma_samples, |
| 1568 | + 1, sps->sps_pic_width_max_in_luma_samples); |
| 1569 | + ue(pps_pic_height_in_luma_samples, |
| 1570 | + 1, sps->sps_pic_height_max_in_luma_samples); |
| 1571 | + |
| 1572 | + min_cb_size_y = 1 << (sps->sps_log2_min_luma_coding_block_size_minus2 + 2); |
| 1573 | + divisor = FFMAX(min_cb_size_y, 8); |
| 1574 | + if (current->pps_pic_width_in_luma_samples % divisor || |
| 1575 | + current->pps_pic_height_in_luma_samples % divisor) { |
| 1576 | + av_log(ctx->log_ctx, AV_LOG_ERROR, |
| 1577 | + "Invalid dimensions: %ux%u not divisible " |
| 1578 | + "by %u, MinCbSizeY = %u.\n", |
| 1579 | + current->pps_pic_width_in_luma_samples, |
| 1580 | + current->pps_pic_height_in_luma_samples, divisor, min_cb_size_y); |
| 1581 | + return AVERROR_INVALIDDATA; |
| 1582 | + } |
| 1583 | + if (!sps->sps_res_change_in_clvs_allowed_flag && |
| 1584 | + (current->pps_pic_width_in_luma_samples != |
| 1585 | + sps->sps_pic_width_max_in_luma_samples || |
| 1586 | + current->pps_pic_height_in_luma_samples != |
| 1587 | + sps->sps_pic_height_max_in_luma_samples)) { |
| 1588 | + av_log(ctx->log_ctx, AV_LOG_ERROR, |
| 1589 | + "Resoltuion change is not allowed, " |
| 1590 | + "in max resolution (%ux%u) mismatched with pps(%ux%u).\n", |
| 1591 | + sps->sps_pic_width_max_in_luma_samples, |
| 1592 | + sps->sps_pic_height_max_in_luma_samples, |
| 1593 | + current->pps_pic_width_in_luma_samples, |
| 1594 | + current->pps_pic_height_in_luma_samples); |
| 1595 | + return AVERROR_INVALIDDATA; |
| 1596 | + } |
| 1597 | + |
| 1598 | + ctb_size_y = 1 << (sps->sps_log2_ctu_size_minus5 + 5); |
| 1599 | + if (sps->sps_ref_wraparound_enabled_flag) { |
| 1600 | + if ((ctb_size_y / min_cb_size_y + 1) > |
| 1601 | + (current->pps_pic_width_in_luma_samples / min_cb_size_y - 1)) { |
| 1602 | + av_log(ctx->log_ctx, AV_LOG_ERROR, |
| 1603 | + "Invalid width(%u), ctb_size_y = %u, min_cb_size_y = %u.\n", |
| 1604 | + current->pps_pic_width_in_luma_samples, |
| 1605 | + ctb_size_y, min_cb_size_y); |
| 1606 | + return AVERROR_INVALIDDATA; |
| 1607 | + } |
| 1608 | + } |
| 1609 | + |
| 1610 | + flag(pps_conformance_window_flag); |
| 1611 | + if (current->pps_pic_width_in_luma_samples == |
| 1612 | + sps->sps_pic_width_max_in_luma_samples && |
| 1613 | + current->pps_pic_height_in_luma_samples == |
| 1614 | + sps->sps_pic_height_max_in_luma_samples && |
| 1615 | + current->pps_conformance_window_flag) { |
| 1616 | + av_log(ctx->log_ctx, AV_LOG_ERROR, |
| 1617 | + "Conformance window flag should not true.\n"); |
| 1618 | + return AVERROR_INVALIDDATA; |
| 1619 | + } |
| 1620 | + |
| 1621 | + sub_width_c = h266_sub_width_c[sps->sps_chroma_format_idc]; |
| 1622 | + sub_height_c = h266_sub_height_c[sps->sps_chroma_format_idc]; |
| 1623 | + if (current->pps_conformance_window_flag) { |
| 1624 | + ue(pps_conf_win_left_offset, 0, current->pps_pic_width_in_luma_samples); |
| 1625 | + ue(pps_conf_win_right_offset, |
| 1626 | + 0, current->pps_pic_width_in_luma_samples); |
| 1627 | + ue(pps_conf_win_top_offset, 0, current->pps_pic_height_in_luma_samples); |
| 1628 | + ue(pps_conf_win_bottom_offset, |
| 1629 | + 0, current->pps_pic_height_in_luma_samples); |
| 1630 | + if (sub_width_c * |
| 1631 | + (current->pps_conf_win_left_offset + |
| 1632 | + current->pps_conf_win_right_offset) >= |
| 1633 | + current->pps_pic_width_in_luma_samples || |
| 1634 | + sub_height_c * |
| 1635 | + (current->pps_conf_win_top_offset + |
| 1636 | + current->pps_conf_win_bottom_offset) >= |
| 1637 | + current->pps_pic_height_in_luma_samples) { |
| 1638 | + av_log(ctx->log_ctx, AV_LOG_ERROR, |
| 1639 | + "Invalid pps conformance window: (%u, %u, %u, %u), " |
| 1640 | + "resolution is %ux%u, sub wxh is %ux%u.\n", |
| 1641 | + current->pps_conf_win_left_offset, |
| 1642 | + current->pps_conf_win_right_offset, |
| 1643 | + current->pps_conf_win_top_offset, |
| 1644 | + current->pps_conf_win_bottom_offset, |
| 1645 | + current->pps_pic_width_in_luma_samples, |
| 1646 | + current->pps_pic_height_in_luma_samples, |
| 1647 | + sub_width_c, sub_height_c); |
| 1648 | + return AVERROR_INVALIDDATA; |
| 1649 | + } |
| 1650 | + } else { |
| 1651 | + if (current->pps_pic_width_in_luma_samples == |
| 1652 | + sps->sps_pic_width_max_in_luma_samples && |
| 1653 | + current->pps_pic_height_in_luma_samples == |
| 1654 | + sps->sps_pic_height_max_in_luma_samples) { |
| 1655 | + infer(pps_conf_win_left_offset, sps->sps_conf_win_left_offset); |
| 1656 | + infer(pps_conf_win_right_offset, sps->sps_conf_win_right_offset); |
| 1657 | + infer(pps_conf_win_top_offset, sps->sps_conf_win_top_offset); |
| 1658 | + infer(pps_conf_win_bottom_offset, sps->sps_conf_win_bottom_offset); |
| 1659 | + } else { |
| 1660 | + infer(pps_conf_win_left_offset, 0); |
| 1661 | + infer(pps_conf_win_right_offset, 0); |
| 1662 | + infer(pps_conf_win_top_offset, 0); |
| 1663 | + infer(pps_conf_win_bottom_offset, 0); |
| 1664 | + } |
| 1665 | + |
| 1666 | + } |
| 1667 | + |
| 1668 | + flag(pps_scaling_window_explicit_signalling_flag); |
| 1669 | + if (!sps->sps_ref_pic_resampling_enabled_flag && |
| 1670 | + current->pps_scaling_window_explicit_signalling_flag) { |
| 1671 | + av_log(ctx->log_ctx, AV_LOG_ERROR, |
| 1672 | + "Invalid data: sps_ref_pic_resampling_enabled_flag is false, " |
| 1673 | + "but pps_scaling_window_explicit_signalling_flag is true.\n"); |
| 1674 | + return AVERROR_INVALIDDATA; |
| 1675 | + } |
| 1676 | + if (current->pps_scaling_window_explicit_signalling_flag) { |
| 1677 | + se(pps_scaling_win_left_offset, |
| 1678 | + -current->pps_pic_width_in_luma_samples * 15 / sub_width_c, |
| 1679 | + current->pps_pic_width_in_luma_samples / sub_width_c); |
| 1680 | + se(pps_scaling_win_right_offset, |
| 1681 | + -current->pps_pic_width_in_luma_samples * 15 / sub_width_c, |
| 1682 | + current->pps_pic_width_in_luma_samples / sub_width_c); |
| 1683 | + se(pps_scaling_win_top_offset, |
| 1684 | + -current->pps_pic_height_in_luma_samples * 15 / sub_height_c, |
| 1685 | + current->pps_pic_height_in_luma_samples / sub_height_c); |
| 1686 | + se(pps_scaling_win_bottom_offset, |
| 1687 | + -current->pps_pic_height_in_luma_samples * 15 / sub_height_c, |
| 1688 | + current->pps_pic_height_in_luma_samples / sub_height_c); |
| 1689 | + } else { |
| 1690 | + infer(pps_scaling_win_left_offset, current->pps_conf_win_left_offset); |
| 1691 | + infer(pps_scaling_win_right_offset, current->pps_conf_win_right_offset); |
| 1692 | + infer(pps_scaling_win_top_offset, current->pps_conf_win_top_offset); |
| 1693 | + infer(pps_scaling_win_bottom_offset, current->pps_conf_win_bottom_offset); |
| 1694 | + } |
| 1695 | + |
| 1696 | + flag(pps_output_flag_present_flag); |
| 1697 | + flag(pps_no_pic_partition_flag); |
| 1698 | + flag(pps_subpic_id_mapping_present_flag); |
| 1699 | + |
| 1700 | + if (current->pps_subpic_id_mapping_present_flag) { |
| 1701 | + if (!current->pps_no_pic_partition_flag) { |
| 1702 | + ue(pps_num_subpics_minus1, |
| 1703 | + sps->sps_num_subpics_minus1, sps->sps_num_subpics_minus1); |
| 1704 | + } else { |
| 1705 | + infer(pps_num_subpics_minus1, 0); |
| 1706 | + } |
| 1707 | + ue(pps_subpic_id_len_minus1, sps->sps_subpic_id_len_minus1, |
| 1708 | + sps->sps_subpic_id_len_minus1); |
| 1709 | + for (i = 0; i <= current->pps_num_subpics_minus1; i++) { |
| 1710 | + ubs(sps->sps_subpic_id_len_minus1 + 1, pps_subpic_id[i], 1, i); |
| 1711 | + } |
| 1712 | + } |
| 1713 | + |
| 1714 | + pic_width_in_ctbs_y = AV_CEIL_RSHIFT |
| 1715 | + (current->pps_pic_width_in_luma_samples, (sps->sps_log2_ctu_size_minus5 + 5)); |
| 1716 | + pic_height_in_ctbs_y = AV_CEIL_RSHIFT( |
| 1717 | + current->pps_pic_height_in_luma_samples,(sps->sps_log2_ctu_size_minus5 + 5)); |
| 1718 | + if (!current->pps_no_pic_partition_flag) { |
| 1719 | + unsigned int exp_tile_width = 0, exp_tile_height = 0; |
| 1720 | + unsigned int unified_size, remaining_size; |
| 1721 | + |
| 1722 | + u(2, pps_log2_ctu_size_minus5, |
| 1723 | + sps->sps_log2_ctu_size_minus5, sps->sps_log2_ctu_size_minus5); |
| 1724 | + ue(pps_num_exp_tile_columns_minus1, |
| 1725 | + 0, FFMIN(pic_width_in_ctbs_y - 1, VVC_MAX_TILE_COLUMNS - 1)); |
| 1726 | + ue(pps_num_exp_tile_rows_minus1, |
| 1727 | + 0, FFMIN(pic_height_in_ctbs_y - 1, VVC_MAX_TILE_ROWS - 1)); |
| 1728 | + |
| 1729 | + for (i = 0; i <= current->pps_num_exp_tile_columns_minus1; i++) { |
| 1730 | + ues(pps_tile_column_width_minus1[i], |
| 1731 | + 0, pic_width_in_ctbs_y - exp_tile_width - 1, 1, i); |
| 1732 | + exp_tile_width += current->pps_tile_column_width_minus1[i] + 1; |
| 1733 | + } |
| 1734 | + for (i = 0; i <= current->pps_num_exp_tile_rows_minus1; i++) { |
| 1735 | + ues(pps_tile_row_height_minus1[i], |
| 1736 | + 0, pic_height_in_ctbs_y - 1, 1, i); |
| 1737 | + exp_tile_height += current->pps_tile_row_height_minus1[i] + 1; |
| 1738 | + } |
| 1739 | + |
| 1740 | + remaining_size = pic_width_in_ctbs_y; |
| 1741 | + for (i = 0; i <= current->pps_num_exp_tile_columns_minus1; i++) { |
| 1742 | + if (current->pps_tile_column_width_minus1[i] >= remaining_size) { |
| 1743 | + av_log(ctx->log_ctx, AV_LOG_ERROR, |
| 1744 | + "Tile column width(%d) exceeds picture width\n",i); |
| 1745 | + return AVERROR_INVALIDDATA; |
| 1746 | + } |
| 1747 | + remaining_size -= (current->pps_tile_column_width_minus1[i] + 1); |
| 1748 | + } |
| 1749 | + unified_size = (i == 0 ? pic_width_in_ctbs_y : |
| 1750 | + (current->pps_tile_column_width_minus1[i - 1] + 1)); |
| 1751 | + while (remaining_size > 0) { |
| 1752 | + if (current->num_tile_columns > VVC_MAX_TILE_COLUMNS) { |
| 1753 | + av_log(ctx->log_ctx, AV_LOG_ERROR, |
| 1754 | + "NumTileColumns(%d) > than VVC_MAX_TILE_COLUMNS(%d)\n", |
| 1755 | + current->num_tile_columns, VVC_MAX_TILE_COLUMNS); |
| 1756 | + return AVERROR_INVALIDDATA; |
| 1757 | + } |
| 1758 | + unified_size = FFMIN(remaining_size, unified_size); |
| 1759 | + current->pps_tile_column_width_minus1[i] = unified_size - 1; |
| 1760 | + remaining_size -= unified_size; |
| 1761 | + i++; |
| 1762 | + } |
| 1763 | + current->num_tile_columns = i; |
| 1764 | + if (current->num_tile_columns > VVC_MAX_TILE_COLUMNS) { |
| 1765 | + av_log(ctx->log_ctx, AV_LOG_ERROR, |
| 1766 | + "NumTileColumns(%d) > than VVC_MAX_TILE_COLUMNS(%d)\n", |
| 1767 | + current->num_tile_columns, VVC_MAX_TILE_COLUMNS); |
| 1768 | + return AVERROR_INVALIDDATA; |
| 1769 | + } |
| 1770 | + |
| 1771 | + remaining_size = pic_height_in_ctbs_y; |
| 1772 | + for (i = 0; i <= current->pps_num_exp_tile_rows_minus1; i++) { |
| 1773 | + if (current->pps_tile_row_height_minus1[i] >= remaining_size) { |
| 1774 | + av_log(ctx->log_ctx, AV_LOG_ERROR, |
| 1775 | + "Tile row height(%d) exceeds picture height\n",i); |
| 1776 | + return AVERROR_INVALIDDATA; |
| 1777 | + } |
| 1778 | + remaining_size -= (current->pps_tile_row_height_minus1[i] + 1); |
| 1779 | + } |
| 1780 | + unified_size = (i == 0 ? pic_height_in_ctbs_y : |
| 1781 | + (current->pps_tile_row_height_minus1[i - 1] + 1)); |
| 1782 | + |
| 1783 | + while (remaining_size > 0) { |
| 1784 | + unified_size = FFMIN(remaining_size, unified_size); |
| 1785 | + current->pps_tile_row_height_minus1[i] = unified_size - 1; |
| 1786 | + remaining_size -= unified_size; |
| 1787 | + i++; |
| 1788 | + } |
| 1789 | + current->num_tile_rows=i; |
| 1790 | + if (current->num_tile_rows > VVC_MAX_TILE_ROWS) { |
| 1791 | + av_log(ctx->log_ctx, AV_LOG_ERROR, |
| 1792 | + "NumTileRows(%d) > than VVC_MAX_TILE_ROWS(%d)\n", |
| 1793 | + current->num_tile_rows, VVC_MAX_TILE_ROWS); |
| 1794 | + return AVERROR_INVALIDDATA; |
| 1795 | + } |
| 1796 | + |
| 1797 | + current->num_tiles_in_pic = current->num_tile_columns * |
| 1798 | + current->num_tile_rows; |
| 1799 | + if (current->num_tiles_in_pic > VVC_MAX_TILES_PER_AU) { |
| 1800 | + av_log(ctx->log_ctx, AV_LOG_ERROR, |
| 1801 | + "NumTilesInPic(%d) > than VVC_MAX_TILES_PER_AU(%d)\n", |
| 1802 | + current->num_tiles_in_pic, VVC_MAX_TILES_PER_AU); |
| 1803 | + return AVERROR_INVALIDDATA; |
| 1804 | + } |
| 1805 | + |
| 1806 | + if (current->num_tiles_in_pic > 1) { |
| 1807 | + flag(pps_loop_filter_across_tiles_enabled_flag); |
| 1808 | + flag(pps_rect_slice_flag); |
| 1809 | + } else { |
| 1810 | + infer(pps_loop_filter_across_tiles_enabled_flag, 0); |
| 1811 | + infer(pps_rect_slice_flag, 1); |
| 1812 | + } |
| 1813 | + if (current->pps_rect_slice_flag) |
| 1814 | + flag(pps_single_slice_per_subpic_flag); |
| 1815 | + else |
| 1816 | + infer(pps_single_slice_per_subpic_flag, 1); |
| 1817 | + if (current->pps_rect_slice_flag && |
| 1818 | + !current->pps_single_slice_per_subpic_flag) { |
| 1819 | + int j; |
| 1820 | + uint16_t tile_idx = 0, tile_x, tile_y, ctu_x, ctu_y; |
| 1821 | + uint16_t slice_top_left_ctu_x[VVC_MAX_SLICES]; |
| 1822 | + uint16_t slice_top_left_ctu_y[VVC_MAX_SLICES]; |
| 1823 | + ue(pps_num_slices_in_pic_minus1, 0, VVC_MAX_SLICES - 1); |
| 1824 | + if (current->pps_num_slices_in_pic_minus1 > 1) |
| 1825 | + flag(pps_tile_idx_delta_present_flag); |
| 1826 | + else |
| 1827 | + infer(pps_tile_idx_delta_present_flag, 0); |
| 1828 | + for (i = 0; i < current->pps_num_slices_in_pic_minus1; i++) { |
| 1829 | + tile_x = tile_idx % current->num_tile_columns; |
| 1830 | + tile_y = tile_idx / current->num_tile_columns; |
| 1831 | + if (tile_x != current->num_tile_columns - 1) { |
| 1832 | + ues(pps_slice_width_in_tiles_minus1[i], |
| 1833 | + 0, current->num_tile_columns - 1, 1, i); |
| 1834 | + } else { |
| 1835 | + infer(pps_slice_width_in_tiles_minus1[i], 0); |
| 1836 | + } |
| 1837 | + if (tile_y != current->num_tile_rows - 1 && |
| 1838 | + (current->pps_tile_idx_delta_present_flag || tile_x == 0)) { |
| 1839 | + ues(pps_slice_height_in_tiles_minus1[i], |
| 1840 | + 0, current->num_tile_rows - 1, 1, i); |
| 1841 | + } else { |
| 1842 | + if (tile_y == current->num_tile_rows - 1) |
| 1843 | + infer(pps_slice_height_in_tiles_minus1[i], 0); |
| 1844 | + else |
| 1845 | + infer(pps_slice_height_in_tiles_minus1[i], |
| 1846 | + current->pps_slice_height_in_tiles_minus1[i - 1]); |
| 1847 | + } |
| 1848 | + |
| 1849 | + ctu_x = ctu_y = 0; |
| 1850 | + for (j = 0; j < tile_x; j++) { |
| 1851 | + ctu_x += current->pps_tile_column_width_minus1[j] + 1; |
| 1852 | + } |
| 1853 | + for (j = 0; j < tile_y; j++) { |
| 1854 | + ctu_y += current->pps_tile_row_height_minus1[j] + 1; |
| 1855 | + } |
| 1856 | + if (current->pps_slice_width_in_tiles_minus1[i] == 0 && |
| 1857 | + current->pps_slice_height_in_tiles_minus1[i] == 0 && |
| 1858 | + current->pps_tile_row_height_minus1[tile_y] > 0) { |
| 1859 | + int num_slices_in_tile, |
| 1860 | + uniform_slice_height, remaining_height_in_ctbs_y; |
| 1861 | + remaining_height_in_ctbs_y = |
| 1862 | + current->pps_tile_row_height_minus1[tile_y] + 1; |
| 1863 | + ues(pps_num_exp_slices_in_tile[i], |
| 1864 | + 0, current->pps_tile_row_height_minus1[tile_y], 1, i); |
| 1865 | + if (current->pps_num_exp_slices_in_tile[i] == 0) { |
| 1866 | + num_slices_in_tile = 1; |
| 1867 | + slice_top_left_ctu_x[i] = ctu_x; |
| 1868 | + slice_top_left_ctu_y[i] = ctu_y; |
| 1869 | + } else { |
| 1870 | + uint16_t slice_height_in_ctus; |
| 1871 | + for (j = 0; j < current->pps_num_exp_slices_in_tile[i]; |
| 1872 | + j++) { |
| 1873 | + ues(pps_exp_slice_height_in_ctus_minus1[i][j], 0, |
| 1874 | + current->pps_tile_row_height_minus1[tile_y], 2, |
| 1875 | + i, j); |
| 1876 | + slice_height_in_ctus = |
| 1877 | + current-> |
| 1878 | + pps_exp_slice_height_in_ctus_minus1[i][j] + 1; |
| 1879 | + |
| 1880 | + current->slice_height_in_ctus[i + j] = |
| 1881 | + slice_height_in_ctus; |
| 1882 | + slice_top_left_ctu_x[i + j] = ctu_x; |
| 1883 | + slice_top_left_ctu_y[i + j] = ctu_y; |
| 1884 | + ctu_y += slice_height_in_ctus; |
| 1885 | + |
| 1886 | + remaining_height_in_ctbs_y -= slice_height_in_ctus; |
| 1887 | + } |
| 1888 | + uniform_slice_height = 1 + |
| 1889 | + (j == 0 ? current->pps_tile_row_height_minus1[tile_y] : |
| 1890 | + current->pps_exp_slice_height_in_ctus_minus1[i][j-1]); |
| 1891 | + while (remaining_height_in_ctbs_y > uniform_slice_height) { |
| 1892 | + current->slice_height_in_ctus[i + j] = |
| 1893 | + uniform_slice_height; |
| 1894 | + slice_top_left_ctu_x[i + j] = ctu_x; |
| 1895 | + slice_top_left_ctu_y[i + j] = ctu_y; |
| 1896 | + ctu_y += uniform_slice_height; |
| 1897 | + |
| 1898 | + remaining_height_in_ctbs_y -= uniform_slice_height; |
| 1899 | + j++; |
| 1900 | + } |
| 1901 | + if (remaining_height_in_ctbs_y > 0) { |
| 1902 | + current->slice_height_in_ctus[i + j] = |
| 1903 | + remaining_height_in_ctbs_y; |
| 1904 | + slice_top_left_ctu_x[i + j] = ctu_x; |
| 1905 | + slice_top_left_ctu_y[i + j] = ctu_y; |
| 1906 | + j++; |
| 1907 | + } |
| 1908 | + num_slices_in_tile = j; |
| 1909 | + } |
| 1910 | + i += num_slices_in_tile - 1; |
| 1911 | + } else { |
| 1912 | + uint16_t height = 0; |
| 1913 | + infer(pps_num_exp_slices_in_tile[i], 0); |
| 1914 | + for (j = 0; |
| 1915 | + j <= current->pps_slice_height_in_tiles_minus1[i]; |
| 1916 | + j++) { |
| 1917 | + height += |
| 1918 | + current->pps_tile_row_height_minus1[tile_y + j] + 1; |
| 1919 | + } |
| 1920 | + current->slice_height_in_ctus[i] = height; |
| 1921 | + |
| 1922 | + slice_top_left_ctu_x[i] = ctu_x; |
| 1923 | + slice_top_left_ctu_y[i] = ctu_y; |
| 1924 | + } |
| 1925 | + if (i < current->pps_num_slices_in_pic_minus1) { |
| 1926 | + if (current->pps_tile_idx_delta_present_flag) { |
| 1927 | + ses(pps_tile_idx_delta_val[i], |
| 1928 | + -current->num_tiles_in_pic + 1, |
| 1929 | + current->num_tiles_in_pic - 1, 1, i); |
| 1930 | + if (current->pps_tile_idx_delta_val[i] == 0) { |
| 1931 | + av_log(ctx->log_ctx, AV_LOG_ERROR, |
| 1932 | + "pps_tile_idx_delta_val[i] shall not be equal to 0.\n"); |
| 1933 | + } |
| 1934 | + tile_idx += current->pps_tile_idx_delta_val[i]; |
| 1935 | + } else { |
| 1936 | + infer(pps_tile_idx_delta_val[i], 0); |
| 1937 | + tile_idx += |
| 1938 | + current->pps_slice_width_in_tiles_minus1[i] + 1; |
| 1939 | + if (tile_idx % current->num_tile_columns == 0) { |
| 1940 | + tile_idx += |
| 1941 | + current->pps_slice_height_in_tiles_minus1[i] * |
| 1942 | + current->num_tile_columns; |
| 1943 | + } |
| 1944 | + } |
| 1945 | + } |
| 1946 | + } |
| 1947 | + if (i == current->pps_num_slices_in_pic_minus1) { |
| 1948 | + uint16_t height = 0; |
| 1949 | + |
| 1950 | + tile_x = tile_idx % current->num_tile_columns; |
| 1951 | + tile_y = tile_idx / current->num_tile_columns; |
| 1952 | + |
| 1953 | + ctu_x = 0, ctu_y = 0; |
| 1954 | + for (j = 0; j < tile_x; j++) { |
| 1955 | + ctu_x += current->pps_tile_column_width_minus1[j] + 1; |
| 1956 | + } |
| 1957 | + for (j = 0; j < tile_y; j++) { |
| 1958 | + ctu_y += current->pps_tile_row_height_minus1[j] + 1; |
| 1959 | + } |
| 1960 | + slice_top_left_ctu_x[i] = ctu_x; |
| 1961 | + slice_top_left_ctu_y[i] = ctu_y; |
| 1962 | + |
| 1963 | + current->pps_slice_width_in_tiles_minus1[i] = |
| 1964 | + current->num_tile_columns - tile_x - 1; |
| 1965 | + current->pps_slice_height_in_tiles_minus1[i] = |
| 1966 | + current->num_tile_rows - tile_y - 1; |
| 1967 | + |
| 1968 | + for (j = 0; j <= current->pps_slice_height_in_tiles_minus1[i]; |
| 1969 | + j++) { |
| 1970 | + height += |
| 1971 | + current->pps_tile_row_height_minus1[tile_y + j] + 1; |
| 1972 | + } |
| 1973 | + current->slice_height_in_ctus[i] = height; |
| 1974 | + |
| 1975 | + infer(pps_num_exp_slices_in_tile[i], 0); |
| 1976 | + } |
| 1977 | + //now, we got all slice information, let's resolve NumSlicesInSubpic |
| 1978 | + for (i = 0; i <= sps->sps_num_subpics_minus1; i++) { |
| 1979 | + current->num_slices_in_subpic[i] = 0; |
| 1980 | + for (j = 0; j <= current->pps_num_slices_in_pic_minus1; j++) { |
| 1981 | + uint16_t pos_x = 0, pos_y = 0; |
| 1982 | + pos_x = slice_top_left_ctu_x[j]; |
| 1983 | + pos_y = slice_top_left_ctu_y[j]; |
| 1984 | + if ((pos_x >= sps->sps_subpic_ctu_top_left_x[i]) && |
| 1985 | + (pos_x < |
| 1986 | + sps->sps_subpic_ctu_top_left_x[i] + |
| 1987 | + sps->sps_subpic_width_minus1[i] + 1) && |
| 1988 | + (pos_y >= sps->sps_subpic_ctu_top_left_y[i]) && |
| 1989 | + (pos_y < sps->sps_subpic_ctu_top_left_y[i] + |
| 1990 | + sps->sps_subpic_height_minus1[i] + 1)) { |
| 1991 | + current->num_slices_in_subpic[i]++; |
| 1992 | + } |
| 1993 | + } |
| 1994 | + } |
| 1995 | + } else { |
| 1996 | + if (current->pps_no_pic_partition_flag) |
| 1997 | + infer(pps_num_slices_in_pic_minus1, 0); |
| 1998 | + else if (current->pps_single_slice_per_subpic_flag) |
| 1999 | + infer(pps_num_slices_in_pic_minus1, |
| 2000 | + sps->sps_num_subpics_minus1); |
| 2001 | + // else? |
| 2002 | + } |
| 2003 | + if (!current->pps_rect_slice_flag || |
| 2004 | + current->pps_single_slice_per_subpic_flag || |
| 2005 | + current->pps_num_slices_in_pic_minus1 > 0) |
| 2006 | + flag(pps_loop_filter_across_slices_enabled_flag); |
| 2007 | + else |
| 2008 | + infer(pps_loop_filter_across_slices_enabled_flag, 0); |
| 2009 | + } else { |
| 2010 | + infer(pps_num_exp_tile_columns_minus1, 0); |
| 2011 | + infer(pps_tile_column_width_minus1[0], pic_width_in_ctbs_y - 1); |
| 2012 | + infer(pps_num_exp_tile_rows_minus1, 0); |
| 2013 | + infer(pps_tile_row_height_minus1[0], pic_height_in_ctbs_y - 1); |
| 2014 | + infer(num_tile_columns, 1); |
| 2015 | + infer(num_tile_rows, 1); |
| 2016 | + infer(num_tiles_in_pic, 1); |
| 2017 | + } |
| 2018 | + |
| 2019 | + flag(pps_cabac_init_present_flag); |
| 2020 | + for (i = 0; i < 2; i++) |
| 2021 | + ues(pps_num_ref_idx_default_active_minus1[i], 0, 14, 1, i); |
| 2022 | + flag(pps_rpl1_idx_present_flag); |
| 2023 | + flag(pps_weighted_pred_flag); |
| 2024 | + flag(pps_weighted_bipred_flag); |
| 2025 | + flag(pps_ref_wraparound_enabled_flag); |
| 2026 | + if (current->pps_ref_wraparound_enabled_flag) { |
| 2027 | + ue(pps_pic_width_minus_wraparound_offset, |
| 2028 | + 0, (current->pps_pic_width_in_luma_samples / min_cb_size_y) |
| 2029 | + - (ctb_size_y / min_cb_size_y) - 2); |
| 2030 | + } |
| 2031 | + |
| 2032 | + qp_bd_offset = 6 * sps->sps_bitdepth_minus8; |
| 2033 | + se(pps_init_qp_minus26, -(26 + qp_bd_offset), 37); |
| 2034 | + flag(pps_cu_qp_delta_enabled_flag); |
| 2035 | + flag(pps_chroma_tool_offsets_present_flag); |
| 2036 | + if (current->pps_chroma_tool_offsets_present_flag) { |
| 2037 | + se(pps_cb_qp_offset, -12, 12); |
| 2038 | + se(pps_cr_qp_offset, -12, 12); |
| 2039 | + flag(pps_joint_cbcr_qp_offset_present_flag); |
| 2040 | + if (current->pps_joint_cbcr_qp_offset_present_flag) |
| 2041 | + se(pps_joint_cbcr_qp_offset_value, -12, 12); |
| 2042 | + else |
| 2043 | + infer(pps_joint_cbcr_qp_offset_value, 0); |
| 2044 | + flag(pps_slice_chroma_qp_offsets_present_flag); |
| 2045 | + flag(pps_cu_chroma_qp_offset_list_enabled_flag); |
| 2046 | + if (current->pps_cu_chroma_qp_offset_list_enabled_flag) { |
| 2047 | + ue(pps_chroma_qp_offset_list_len_minus1, 0, 5); |
| 2048 | + for (i = 0; i <= current->pps_chroma_qp_offset_list_len_minus1; i++) { |
| 2049 | + ses(pps_cb_qp_offset_list[i], -12, 12, 1, i); |
| 2050 | + ses(pps_cr_qp_offset_list[i], -12, 12, 1, i); |
| 2051 | + if (current->pps_joint_cbcr_qp_offset_present_flag) |
| 2052 | + ses(pps_joint_cbcr_qp_offset_list[i], -12, 12, 1, i); |
| 2053 | + else |
| 2054 | + infer(pps_joint_cbcr_qp_offset_list[i], 0); |
| 2055 | + } |
| 2056 | + } |
| 2057 | + } else { |
| 2058 | + infer(pps_cb_qp_offset, 0); |
| 2059 | + infer(pps_cr_qp_offset, 0); |
| 2060 | + infer(pps_joint_cbcr_qp_offset_present_flag, 0); |
| 2061 | + infer(pps_joint_cbcr_qp_offset_value, 0); |
| 2062 | + infer(pps_slice_chroma_qp_offsets_present_flag, 0); |
| 2063 | + infer(pps_cu_chroma_qp_offset_list_enabled_flag, 0); |
| 2064 | + } |
| 2065 | + flag(pps_deblocking_filter_control_present_flag); |
| 2066 | + if (current->pps_deblocking_filter_control_present_flag) { |
| 2067 | + flag(pps_deblocking_filter_override_enabled_flag); |
| 2068 | + flag(pps_deblocking_filter_disabled_flag); |
| 2069 | + if (!current->pps_no_pic_partition_flag && |
| 2070 | + current->pps_deblocking_filter_override_enabled_flag) |
| 2071 | + flag(pps_dbf_info_in_ph_flag); |
| 2072 | + else |
| 2073 | + infer(pps_dbf_info_in_ph_flag, 0); |
| 2074 | + if (!current->pps_deblocking_filter_disabled_flag) { |
| 2075 | + se(pps_luma_beta_offset_div2, -12, 12); |
| 2076 | + se(pps_luma_tc_offset_div2, -12, 12); |
| 2077 | + if (current->pps_chroma_tool_offsets_present_flag) { |
| 2078 | + se(pps_cb_beta_offset_div2, -12, 12); |
| 2079 | + se(pps_cb_tc_offset_div2, -12, 12); |
| 2080 | + se(pps_cr_beta_offset_div2, -12, 12); |
| 2081 | + se(pps_cr_tc_offset_div2, -12, 12); |
| 2082 | + } else { |
| 2083 | + infer(pps_cb_beta_offset_div2, |
| 2084 | + current->pps_luma_beta_offset_div2); |
| 2085 | + infer(pps_cb_tc_offset_div2, current->pps_luma_tc_offset_div2); |
| 2086 | + infer(pps_cr_beta_offset_div2, |
| 2087 | + current->pps_luma_beta_offset_div2); |
| 2088 | + infer(pps_cr_tc_offset_div2, current->pps_luma_tc_offset_div2); |
| 2089 | + } |
| 2090 | + } else { |
| 2091 | + infer(pps_luma_beta_offset_div2, 0); |
| 2092 | + infer(pps_luma_tc_offset_div2, 0); |
| 2093 | + infer(pps_cb_beta_offset_div2, 0); |
| 2094 | + infer(pps_cb_tc_offset_div2, 0); |
| 2095 | + infer(pps_cr_beta_offset_div2, 0); |
| 2096 | + infer(pps_cr_tc_offset_div2, 0); |
| 2097 | + } |
| 2098 | + } else { |
| 2099 | + infer(pps_deblocking_filter_override_enabled_flag, 0); |
| 2100 | + infer(pps_deblocking_filter_disabled_flag, 0); |
| 2101 | + infer(pps_dbf_info_in_ph_flag, 0); |
| 2102 | + infer(pps_luma_beta_offset_div2, 0); |
| 2103 | + infer(pps_luma_tc_offset_div2, 0); |
| 2104 | + infer(pps_cb_beta_offset_div2, 0); |
| 2105 | + infer(pps_cb_tc_offset_div2, 0); |
| 2106 | + infer(pps_cr_beta_offset_div2, 0); |
| 2107 | + infer(pps_cr_tc_offset_div2, 0); |
| 2108 | + } |
| 2109 | + |
| 2110 | + if (!current->pps_no_pic_partition_flag) { |
| 2111 | + flag(pps_rpl_info_in_ph_flag); |
| 2112 | + flag(pps_sao_info_in_ph_flag); |
| 2113 | + flag(pps_alf_info_in_ph_flag); |
| 2114 | + if ((current->pps_weighted_pred_flag || |
| 2115 | + current->pps_weighted_bipred_flag) && |
| 2116 | + current->pps_rpl_info_in_ph_flag) |
| 2117 | + flag(pps_wp_info_in_ph_flag); |
| 2118 | + flag(pps_qp_delta_info_in_ph_flag); |
| 2119 | + } |
| 2120 | + flag(pps_picture_header_extension_present_flag); |
| 2121 | + flag(pps_slice_header_extension_present_flag); |
| 2122 | + |
| 2123 | + flag(pps_extension_flag); |
| 2124 | + if (current->pps_extension_flag) |
| 2125 | + CHECK(FUNC(extension_data) (ctx, rw, ¤t->extension_data)); |
| 2126 | + |
| 2127 | + CHECK(FUNC(rbsp_trailing_bits) (ctx, rw)); |
| 2128 | + return 0; |
| 2129 | +} |
| 2130 | + |
| 2131 | +static int FUNC(aud) (CodedBitstreamContext *ctx, RWContext *rw, |
| 2132 | + H266RawAUD *current) |
| 2133 | +{ |
| 2134 | + int err; |
| 2135 | + |
| 2136 | + HEADER("Access Unit Delimiter"); |
| 2137 | + |
| 2138 | + CHECK(FUNC(nal_unit_header) (ctx, rw, |
| 2139 | + ¤t->nal_unit_header, VVC_AUD_NUT)); |
| 2140 | + |
| 2141 | + flag(aud_irap_or_gdr_flag); |
| 2142 | + u(3, aud_pic_type, 0, 2); |
| 2143 | + |
| 2144 | + CHECK(FUNC(rbsp_trailing_bits) (ctx, rw)); |
| 2145 | + return 0; |
| 2146 | +} |
| 2147 | + |
| 2148 | +static int FUNC(pred_weight_table) (CodedBitstreamContext *ctx, RWContext *rw, |
| 2149 | + const H266RawSPS *sps, |
| 2150 | + const H266RawPPS *pps, |
| 2151 | + const H266RefPicLists *ref_lists, |
| 2152 | + uint8_t num_ref_idx_active[2], |
| 2153 | + H266RawPredWeightTable *current) |
| 2154 | +{ |
| 2155 | + int err, i, j; |
| 2156 | + uint8_t num_weights_l0, num_weights_l1; |
| 2157 | + ue(luma_log2_weight_denom, 0, 7); |
| 2158 | + if (sps->sps_chroma_format_idc != 0) { |
| 2159 | + se(delta_chroma_log2_weight_denom, |
| 2160 | + -current->luma_log2_weight_denom, |
| 2161 | + 7 - current->luma_log2_weight_denom); |
| 2162 | + } else { |
| 2163 | + infer(delta_chroma_log2_weight_denom, 0); |
| 2164 | + } |
| 2165 | + if (pps->pps_wp_info_in_ph_flag) |
| 2166 | + ue(num_l0_weights, 0, |
| 2167 | + FFMIN(15, ref_lists->rpl_ref_list[0].num_ref_entries)); |
| 2168 | + else |
| 2169 | + infer(num_l0_weights, 0); |
| 2170 | + num_weights_l0 = pps->pps_wp_info_in_ph_flag ? |
| 2171 | + current->num_l0_weights : num_ref_idx_active[0]; |
| 2172 | + for (i = 0; i < num_weights_l0; i++) { |
| 2173 | + flags(luma_weight_l0_flag[i], 1, i); |
| 2174 | + } |
| 2175 | + if (sps->sps_chroma_format_idc != 0) { |
| 2176 | + for (i = 0; i < num_weights_l0; i++) |
| 2177 | + flags(chroma_weight_l0_flag[i], 1, i); |
| 2178 | + } |
| 2179 | + for (i = 0; i < num_weights_l0; i++) { |
| 2180 | + if (current->luma_weight_l0_flag[i]) { |
| 2181 | + ses(delta_luma_weight_l0[i], -128, 127, 1, i); |
| 2182 | + ses(luma_offset_l0[i], -128, 127, 1, i); |
| 2183 | + } else { |
| 2184 | + infer(delta_luma_weight_l0[i], 0); |
| 2185 | + infer(luma_offset_l0[i], 0); |
| 2186 | + } |
| 2187 | + if (current->chroma_weight_l0_flag[i]) { |
| 2188 | + for (j = 0; j < 2; j++) { |
| 2189 | + ses(delta_chroma_weight_l0[i][j], -128, 127, 2, i, j); |
| 2190 | + ses(delta_chroma_offset_l0[i][j], -4 * 128, 4 * 127, 2, i, j); |
| 2191 | + } |
| 2192 | + } |
| 2193 | + } |
| 2194 | + |
| 2195 | + if (pps->pps_weighted_bipred_flag && pps->pps_wp_info_in_ph_flag && |
| 2196 | + ref_lists->rpl_ref_list[1].num_ref_entries > 0) { |
| 2197 | + ue(num_l1_weights, 0, |
| 2198 | + FFMIN(15, ref_lists->rpl_ref_list[1].num_ref_entries)); |
| 2199 | + } |
| 2200 | + if (!pps->pps_weighted_bipred_flag || |
| 2201 | + (pps->pps_wp_info_in_ph_flag && |
| 2202 | + ref_lists->rpl_ref_list[1].num_ref_entries == 0)) { |
| 2203 | + num_weights_l1 = 0; |
| 2204 | + } else if (pps->pps_wp_info_in_ph_flag) { |
| 2205 | + num_weights_l1 = current->num_l1_weights; |
| 2206 | + } else { |
| 2207 | + num_weights_l1 = num_ref_idx_active[1]; |
| 2208 | + } |
| 2209 | + |
| 2210 | + for (i = 0; i < num_weights_l1; i++) |
| 2211 | + flags(luma_weight_l1_flag[i], 1, i); |
| 2212 | + if (sps->sps_chroma_format_idc != 0) { |
| 2213 | + for (i = 0; i < num_weights_l1; i++) |
| 2214 | + flags(chroma_weight_l1_flag[i], 1, i); |
| 2215 | + } |
| 2216 | + for (i = 0; i < num_weights_l1; i++) { |
| 2217 | + if (current->luma_weight_l1_flag[i]) { |
| 2218 | + ses(delta_luma_weight_l1[i], -128, 127, 1, i); |
| 2219 | + ses(luma_offset_l1[i], -128, 127, 1, i); |
| 2220 | + } else { |
| 2221 | + infer(delta_luma_weight_l1[i], 0); |
| 2222 | + infer(luma_offset_l1[i], 0); |
| 2223 | + } |
| 2224 | + if (current->chroma_weight_l1_flag[i]) { |
| 2225 | + for (j = 0; j < 2; j++) { |
| 2226 | + ses(delta_chroma_weight_l1[i][j], -128, 127, 2, i, j); |
| 2227 | + ses(delta_chroma_offset_l1[i][j], -4 * 128, 4 * 127, 2, i, j); |
| 2228 | + } |
| 2229 | + } |
| 2230 | + } |
| 2231 | + return 0; |
| 2232 | +} |
| 2233 | + |
| 2234 | +static int FUNC(picture_header) (CodedBitstreamContext *ctx, RWContext *rw, |
| 2235 | + H266RawPH *current){ |
| 2236 | + CodedBitstreamH266Context *h266 = ctx->priv_data; |
| 2237 | + const H266RawVPS *vps; |
| 2238 | + const H266RawSPS *sps; |
| 2239 | + const H266RawPPS *pps; |
| 2240 | + int err, i; |
| 2241 | + unsigned int ctb_log2_size_y, min_cb_log2_size_y, |
| 2242 | + min_qt_log2_size_intra_y, min_qt_log2_size_inter_y; |
| 2243 | + uint8_t qp_bd_offset; |
| 2244 | + |
| 2245 | + flag(ph_gdr_or_irap_pic_flag); |
| 2246 | + flag(ph_non_ref_pic_flag); |
| 2247 | + if (current->ph_gdr_or_irap_pic_flag) |
| 2248 | + flag(ph_gdr_pic_flag); |
| 2249 | + else |
| 2250 | + infer(ph_gdr_pic_flag, 0); |
| 2251 | + flag(ph_inter_slice_allowed_flag); |
| 2252 | + if (current->ph_inter_slice_allowed_flag) |
| 2253 | + flag(ph_intra_slice_allowed_flag); |
| 2254 | + else |
| 2255 | + infer(ph_intra_slice_allowed_flag, 1); |
| 2256 | + ue(ph_pic_parameter_set_id, 0, VVC_MAX_PPS_COUNT - 1); |
| 2257 | + pps = h266->pps[current->ph_pic_parameter_set_id]; |
| 2258 | + if (!pps) { |
| 2259 | + av_log(ctx->log_ctx, AV_LOG_ERROR, "PPS id %d not available.\n", |
| 2260 | + current->ph_pic_parameter_set_id); |
| 2261 | + return AVERROR_INVALIDDATA; |
| 2262 | + } |
| 2263 | + sps = h266->sps[pps->pps_seq_parameter_set_id]; |
| 2264 | + if (!sps) { |
| 2265 | + av_log(ctx->log_ctx, AV_LOG_ERROR, "SPS id %d not available.\n", |
| 2266 | + pps->pps_seq_parameter_set_id); |
| 2267 | + return AVERROR_INVALIDDATA; |
| 2268 | + } |
| 2269 | + vps = h266->vps[sps->sps_video_parameter_set_id]; |
| 2270 | + if (!vps) { |
| 2271 | + av_log(ctx->log_ctx, AV_LOG_ERROR, "VPS id %d not available.\n", |
| 2272 | + sps->sps_video_parameter_set_id); |
| 2273 | + return AVERROR_INVALIDDATA; |
| 2274 | + } |
| 2275 | + |
| 2276 | + ub(sps->sps_log2_max_pic_order_cnt_lsb_minus4 + 4, ph_pic_order_cnt_lsb); |
| 2277 | + if (current->ph_gdr_pic_flag) |
| 2278 | + ue(ph_recovery_poc_cnt, 0, |
| 2279 | + 1 << (sps->sps_log2_max_pic_order_cnt_lsb_minus4 + 4)); |
| 2280 | + |
| 2281 | + for (i = 0; i < sps->sps_num_extra_ph_bytes * 8; i++) { |
| 2282 | + if (sps->sps_extra_ph_bit_present_flag[i]) |
| 2283 | + flags(ph_extra_bit[i], 1, i); |
| 2284 | + } |
| 2285 | + if (sps->sps_poc_msb_cycle_flag) { |
| 2286 | + flag(ph_poc_msb_cycle_present_flag); |
| 2287 | + if (current->ph_poc_msb_cycle_present_flag) |
| 2288 | + ub(sps->sps_poc_msb_cycle_len_minus1 + 1, ph_poc_msb_cycle_val); |
| 2289 | + } |
| 2290 | + if (sps->sps_alf_enabled_flag && pps->pps_alf_info_in_ph_flag) { |
| 2291 | + flag(ph_alf_enabled_flag); |
| 2292 | + if (current->ph_alf_enabled_flag) { |
| 2293 | + |
| 2294 | + ub(3, ph_num_alf_aps_ids_luma); |
| 2295 | + for (i = 0; i < current->ph_num_alf_aps_ids_luma; i++) |
| 2296 | + ubs(3, ph_alf_aps_id_luma[i], 1, i); |
| 2297 | + |
| 2298 | + if (sps->sps_chroma_format_idc != 0) { |
| 2299 | + flag(ph_alf_cb_enabled_flag); |
| 2300 | + flag(ph_alf_cr_enabled_flag); |
| 2301 | + } else { |
| 2302 | + infer(ph_alf_cb_enabled_flag, 0); |
| 2303 | + infer(ph_alf_cr_enabled_flag, 0); |
| 2304 | + } |
| 2305 | + |
| 2306 | + if (current->ph_alf_cb_enabled_flag |
| 2307 | + || current->ph_alf_cr_enabled_flag) { |
| 2308 | + ub(3, ph_alf_aps_id_chroma); |
| 2309 | + } |
| 2310 | + |
| 2311 | + if (sps->sps_ccalf_enabled_flag) { |
| 2312 | + flag(ph_alf_cc_cb_enabled_flag); |
| 2313 | + if (current->ph_alf_cc_cb_enabled_flag) |
| 2314 | + ub(3, ph_alf_cc_cb_aps_id); |
| 2315 | + flag(ph_alf_cc_cr_enabled_flag); |
| 2316 | + if (current->ph_alf_cc_cr_enabled_flag) |
| 2317 | + ub(3, ph_alf_cc_cr_aps_id); |
| 2318 | + } |
| 2319 | + } |
| 2320 | + } else { |
| 2321 | + infer(ph_alf_enabled_flag, 0); |
| 2322 | + } |
| 2323 | + if (sps->sps_lmcs_enabled_flag) { |
| 2324 | + flag(ph_lmcs_enabled_flag); |
| 2325 | + if (current->ph_lmcs_enabled_flag) { |
| 2326 | + ub(2, ph_lmcs_aps_id); |
| 2327 | + if (sps->sps_chroma_format_idc != 0) |
| 2328 | + flag(ph_chroma_residual_scale_flag); |
| 2329 | + else |
| 2330 | + infer(ph_chroma_residual_scale_flag, 0); |
| 2331 | + } |
| 2332 | + } else { |
| 2333 | + infer(ph_lmcs_enabled_flag, 0); |
| 2334 | + infer(ph_chroma_residual_scale_flag, 0); |
| 2335 | + } |
| 2336 | + |
| 2337 | + if (sps->sps_explicit_scaling_list_enabled_flag) { |
| 2338 | + flag(ph_explicit_scaling_list_enabled_flag); |
| 2339 | + if (current->ph_explicit_scaling_list_enabled_flag) { |
| 2340 | + //todo: check the ph_scaling_list_aps_id range, when aps ready |
| 2341 | + ub(3, ph_scaling_list_aps_id); |
| 2342 | + } |
| 2343 | + } else { |
| 2344 | + infer(ph_explicit_scaling_list_enabled_flag, 0); |
| 2345 | + } |
| 2346 | + if (sps->sps_virtual_boundaries_enabled_flag && |
| 2347 | + !sps->sps_virtual_boundaries_present_flag) { |
| 2348 | + flag(ph_virtual_boundaries_present_flag); |
| 2349 | + if (current->ph_virtual_boundaries_present_flag) { |
| 2350 | + ue(ph_num_ver_virtual_boundaries, |
| 2351 | + 0, pps->pps_pic_width_in_luma_samples <= 8 ? 0 : 3); |
| 2352 | + for (i = 0; i < current->ph_num_ver_virtual_boundaries; i++) { |
| 2353 | + ues(ph_virtual_boundary_pos_x_minus1[i], |
| 2354 | + 0, (pps->pps_pic_width_in_luma_samples + 7) / 8 - 2, 1, i); |
| 2355 | + } |
| 2356 | + ue(ph_num_hor_virtual_boundaries, |
| 2357 | + 0, pps->pps_pic_height_in_luma_samples <= 8 ? 0 : 3); |
| 2358 | + for (i = 0; i < current->ph_num_hor_virtual_boundaries; i++) { |
| 2359 | + ues(ph_virtual_boundary_pos_y_minus1[i], |
| 2360 | + 0, (pps->pps_pic_height_in_luma_samples + 7) / 8 - 2, 1, i); |
| 2361 | + } |
| 2362 | + } else { |
| 2363 | + infer(ph_num_ver_virtual_boundaries, 0); |
| 2364 | + infer(ph_num_hor_virtual_boundaries, 0); |
| 2365 | + } |
| 2366 | + } |
| 2367 | + if (pps->pps_output_flag_present_flag && !current->ph_non_ref_pic_flag) |
| 2368 | + flag(ph_pic_output_flag); |
| 2369 | + else |
| 2370 | + infer(ph_pic_output_flag, 1); |
| 2371 | + if (pps->pps_rpl_info_in_ph_flag) { |
| 2372 | + CHECK(FUNC(ref_pic_lists) |
| 2373 | + (ctx, rw, sps, pps, ¤t->ph_ref_pic_lists)); |
| 2374 | + } |
| 2375 | + if (sps->sps_partition_constraints_override_enabled_flag) |
| 2376 | + flag(ph_partition_constraints_override_flag); |
| 2377 | + else |
| 2378 | + infer(ph_partition_constraints_override_flag, 0); |
| 2379 | + |
| 2380 | + ctb_log2_size_y = sps->sps_log2_ctu_size_minus5 + 5; |
| 2381 | + min_cb_log2_size_y = sps->sps_log2_min_luma_coding_block_size_minus2 + 2; |
| 2382 | + if (current->ph_intra_slice_allowed_flag) { |
| 2383 | + if (current->ph_partition_constraints_override_flag) { |
| 2384 | + ue(ph_log2_diff_min_qt_min_cb_intra_slice_luma, |
| 2385 | + 0, FFMIN(6, ctb_log2_size_y) - min_cb_log2_size_y); |
| 2386 | + ue(ph_max_mtt_hierarchy_depth_intra_slice_luma, |
| 2387 | + 0, 2 * (ctb_log2_size_y - min_cb_log2_size_y)); |
| 2388 | + if (current->ph_max_mtt_hierarchy_depth_intra_slice_luma != 0) { |
| 2389 | + ue(ph_log2_diff_max_bt_min_qt_intra_slice_luma, |
| 2390 | + 0, ctb_log2_size_y - min_qt_log2_size_intra_y); |
| 2391 | + ue(ph_log2_diff_max_tt_min_qt_intra_slice_luma, |
| 2392 | + 0, FFMIN(6, ctb_log2_size_y) - min_qt_log2_size_intra_y); |
| 2393 | + } else { |
| 2394 | + infer(ph_log2_diff_max_bt_min_qt_intra_slice_luma, |
| 2395 | + sps->sps_log2_diff_max_bt_min_qt_intra_slice_luma); |
| 2396 | + infer(ph_log2_diff_max_tt_min_qt_intra_slice_luma, |
| 2397 | + sps->sps_log2_diff_max_tt_min_qt_intra_slice_luma); |
| 2398 | + } |
| 2399 | + if (sps->sps_qtbtt_dual_tree_intra_flag) { |
| 2400 | + ue(ph_log2_diff_min_qt_min_cb_intra_slice_chroma, |
| 2401 | + 0, FFMIN(6, ctb_log2_size_y) - min_cb_log2_size_y); |
| 2402 | + ue(ph_max_mtt_hierarchy_depth_intra_slice_chroma, |
| 2403 | + 0, 2 * (ctb_log2_size_y - min_cb_log2_size_y)); |
| 2404 | + if (sps->sps_max_mtt_hierarchy_depth_intra_slice_chroma != 0) { |
| 2405 | + unsigned int min_qt_log2_size_intra_c = |
| 2406 | + sps->sps_log2_diff_min_qt_min_cb_intra_slice_chroma + |
| 2407 | + min_cb_log2_size_y; |
| 2408 | + ue(ph_log2_diff_max_bt_min_qt_intra_slice_chroma, |
| 2409 | + 0, FFMIN(6, ctb_log2_size_y) - min_qt_log2_size_intra_c); |
| 2410 | + ue(ph_log2_diff_max_tt_min_qt_intra_slice_chroma, |
| 2411 | + 0, FFMIN(6, ctb_log2_size_y) - min_qt_log2_size_intra_c); |
| 2412 | + } else { |
| 2413 | + infer(ph_log2_diff_max_bt_min_qt_intra_slice_chroma, |
| 2414 | + sps->sps_log2_diff_max_bt_min_qt_intra_slice_chroma); |
| 2415 | + infer(ph_log2_diff_max_tt_min_qt_intra_slice_chroma, |
| 2416 | + sps->sps_log2_diff_max_tt_min_qt_intra_slice_chroma); |
| 2417 | + } |
| 2418 | + } |
| 2419 | + } else { |
| 2420 | + infer(ph_log2_diff_min_qt_min_cb_intra_slice_luma, |
| 2421 | + sps->sps_log2_diff_min_qt_min_cb_intra_slice_luma); |
| 2422 | + infer(ph_max_mtt_hierarchy_depth_intra_slice_luma, |
| 2423 | + sps->sps_max_mtt_hierarchy_depth_intra_slice_luma); |
| 2424 | + infer(ph_log2_diff_max_bt_min_qt_intra_slice_luma, |
| 2425 | + sps->sps_log2_diff_max_bt_min_qt_intra_slice_luma); |
| 2426 | + infer(ph_log2_diff_max_tt_min_qt_intra_slice_luma, |
| 2427 | + sps->sps_log2_diff_max_tt_min_qt_intra_slice_luma); |
| 2428 | + infer(ph_log2_diff_min_qt_min_cb_intra_slice_chroma, |
| 2429 | + sps->sps_log2_diff_min_qt_min_cb_intra_slice_chroma); |
| 2430 | + infer(ph_max_mtt_hierarchy_depth_intra_slice_chroma, |
| 2431 | + sps->sps_max_mtt_hierarchy_depth_intra_slice_chroma); |
| 2432 | + infer(ph_log2_diff_max_bt_min_qt_intra_slice_chroma, |
| 2433 | + sps->sps_log2_diff_max_bt_min_qt_intra_slice_chroma); |
| 2434 | + infer(ph_log2_diff_max_tt_min_qt_intra_slice_chroma, |
| 2435 | + sps->sps_log2_diff_max_tt_min_qt_intra_slice_chroma); |
| 2436 | + } |
| 2437 | + |
| 2438 | + min_qt_log2_size_intra_y = |
| 2439 | + current->ph_log2_diff_min_qt_min_cb_intra_slice_luma + |
| 2440 | + min_cb_log2_size_y; |
| 2441 | + if (pps->pps_cu_qp_delta_enabled_flag) |
| 2442 | + ue(ph_cu_qp_delta_subdiv_intra_slice, 0, |
| 2443 | + 2 * (ctb_log2_size_y - min_qt_log2_size_intra_y + |
| 2444 | + current->ph_max_mtt_hierarchy_depth_intra_slice_luma)); |
| 2445 | + else |
| 2446 | + infer(ph_cu_qp_delta_subdiv_intra_slice, 0); |
| 2447 | + |
| 2448 | + if (pps->pps_cu_chroma_qp_offset_list_enabled_flag) |
| 2449 | + ue(ph_cu_chroma_qp_offset_subdiv_intra_slice, 0, |
| 2450 | + 2 * (ctb_log2_size_y - min_qt_log2_size_intra_y + |
| 2451 | + current->ph_max_mtt_hierarchy_depth_intra_slice_luma)); |
| 2452 | + else |
| 2453 | + infer(ph_cu_chroma_qp_offset_subdiv_intra_slice, 0); |
| 2454 | + } |
| 2455 | + if (current->ph_inter_slice_allowed_flag) { |
| 2456 | + if (current->ph_partition_constraints_override_flag) { |
| 2457 | + ue(ph_log2_diff_min_qt_min_cb_inter_slice, |
| 2458 | + 0, FFMIN(6, ctb_log2_size_y) - min_cb_log2_size_y); |
| 2459 | + min_qt_log2_size_inter_y = |
| 2460 | + current->ph_log2_diff_min_qt_min_cb_inter_slice + |
| 2461 | + min_cb_log2_size_y; |
| 2462 | + ue(ph_max_mtt_hierarchy_depth_inter_slice, 0, |
| 2463 | + 2 * (ctb_log2_size_y - min_cb_log2_size_y)); |
| 2464 | + if (current->ph_max_mtt_hierarchy_depth_inter_slice != 0) { |
| 2465 | + ue(ph_log2_diff_max_bt_min_qt_inter_slice, |
| 2466 | + 0, ctb_log2_size_y - min_qt_log2_size_inter_y); |
| 2467 | + ue(ph_log2_diff_max_tt_min_qt_inter_slice, |
| 2468 | + 0, FFMIN(6, ctb_log2_size_y) - min_qt_log2_size_inter_y); |
| 2469 | + } |
| 2470 | + } else { |
| 2471 | + infer(ph_log2_diff_min_qt_min_cb_inter_slice, |
| 2472 | + sps->sps_log2_diff_min_qt_min_cb_inter_slice); |
| 2473 | + min_qt_log2_size_inter_y = |
| 2474 | + current->ph_log2_diff_min_qt_min_cb_inter_slice + |
| 2475 | + min_cb_log2_size_y; |
| 2476 | + infer(ph_max_mtt_hierarchy_depth_inter_slice, |
| 2477 | + sps->sps_max_mtt_hierarchy_depth_inter_slice); |
| 2478 | + infer(ph_log2_diff_max_bt_min_qt_inter_slice, |
| 2479 | + sps->sps_log2_diff_max_bt_min_qt_inter_slice); |
| 2480 | + infer(ph_log2_diff_max_tt_min_qt_inter_slice, |
| 2481 | + sps->sps_log2_diff_max_tt_min_qt_inter_slice); |
| 2482 | + } |
| 2483 | + |
| 2484 | + if (pps->pps_cu_qp_delta_enabled_flag) |
| 2485 | + ue(ph_cu_qp_delta_subdiv_inter_slice, 0, |
| 2486 | + 2 * (ctb_log2_size_y - min_qt_log2_size_inter_y + |
| 2487 | + current->ph_max_mtt_hierarchy_depth_inter_slice)); |
| 2488 | + else |
| 2489 | + infer(ph_cu_qp_delta_subdiv_inter_slice, 0); |
| 2490 | + |
| 2491 | + if (pps->pps_cu_chroma_qp_offset_list_enabled_flag) |
| 2492 | + ue(ph_cu_chroma_qp_offset_subdiv_inter_slice, 0, |
| 2493 | + 2 * (ctb_log2_size_y - min_qt_log2_size_inter_y + |
| 2494 | + current->ph_max_mtt_hierarchy_depth_inter_slice)); |
| 2495 | + else |
| 2496 | + infer(ph_cu_chroma_qp_offset_subdiv_inter_slice, 0); |
| 2497 | + if (sps->sps_temporal_mvp_enabled_flag) { |
| 2498 | + flag(ph_temporal_mvp_enabled_flag); |
| 2499 | + if (current->ph_temporal_mvp_enabled_flag && |
| 2500 | + pps->pps_rpl_info_in_ph_flag) { |
| 2501 | + if (current->ph_ref_pic_lists.rpl_ref_list[1].num_ref_entries > 0) |
| 2502 | + flag(ph_collocated_from_l0_flag); |
| 2503 | + else |
| 2504 | + infer(ph_collocated_from_l0_flag, 1); |
| 2505 | + if ((current->ph_collocated_from_l0_flag && |
| 2506 | + current->ph_ref_pic_lists.rpl_ref_list[0].num_ref_entries > 1) |
| 2507 | + || (!current->ph_collocated_from_l0_flag && |
| 2508 | + current->ph_ref_pic_lists.rpl_ref_list[1].num_ref_entries > 1)) { |
| 2509 | + unsigned int idx = |
| 2510 | + current->ph_collocated_from_l0_flag ? 0 : 1; |
| 2511 | + ue(ph_collocated_ref_idx, 0, |
| 2512 | + current->ph_ref_pic_lists.rpl_ref_list[idx]. |
| 2513 | + num_ref_entries - 1); |
| 2514 | + } else { |
| 2515 | + infer(ph_collocated_ref_idx, 0); |
| 2516 | + } |
| 2517 | + } |
| 2518 | + } |
| 2519 | + if (sps->sps_mmvd_fullpel_only_enabled_flag) |
| 2520 | + flag(ph_mmvd_fullpel_only_flag); |
| 2521 | + else |
| 2522 | + infer(ph_mmvd_fullpel_only_flag, 0); |
| 2523 | + if (!pps->pps_rpl_info_in_ph_flag || |
| 2524 | + current->ph_ref_pic_lists.rpl_ref_list[1].num_ref_entries > 0) { |
| 2525 | + flag(ph_mvd_l1_zero_flag); |
| 2526 | + if (sps->sps_bdof_control_present_in_ph_flag) { |
| 2527 | + flag(ph_bdof_disabled_flag); |
| 2528 | + } else { |
| 2529 | + if (!sps->sps_bdof_control_present_in_ph_flag) |
| 2530 | + infer(ph_bdof_disabled_flag, |
| 2531 | + 1 - sps->sps_bdof_enabled_flag); |
| 2532 | + else |
| 2533 | + infer(ph_bdof_disabled_flag, 1); |
| 2534 | + } |
| 2535 | + if (sps->sps_dmvr_control_present_in_ph_flag) { |
| 2536 | + flag(ph_dmvr_disabled_flag); |
| 2537 | + } else { |
| 2538 | + if (!sps->sps_dmvr_control_present_in_ph_flag) |
| 2539 | + infer(ph_dmvr_disabled_flag, |
| 2540 | + 1 - sps->sps_dmvr_enabled_flag); |
| 2541 | + else |
| 2542 | + infer(ph_dmvr_disabled_flag, 1); |
| 2543 | + } |
| 2544 | + } else { |
| 2545 | + infer(ph_mvd_l1_zero_flag, 1); |
| 2546 | + } |
| 2547 | + if (sps->sps_prof_control_present_in_ph_flag) |
| 2548 | + flag(ph_prof_disabled_flag); |
| 2549 | + else |
| 2550 | + infer(ph_prof_disabled_flag, !sps->sps_affine_prof_enabled_flag); |
| 2551 | + if ((pps->pps_weighted_pred_flag || |
| 2552 | + pps->pps_weighted_bipred_flag) && pps->pps_wp_info_in_ph_flag) { |
| 2553 | + |
| 2554 | + // if pps->pps_wp_info_in_ph_fla == 1 |
| 2555 | + // pred_weight_table will not use num_ref_idx_active |
| 2556 | + uint8_t num_ref_idx_active[2] = { 0, 0 }; |
| 2557 | + CHECK(FUNC(pred_weight_table) |
| 2558 | + (ctx, rw, sps, pps, ¤t->ph_ref_pic_lists, |
| 2559 | + num_ref_idx_active, ¤t->ph_pred_weight_table)); |
| 2560 | + } |
| 2561 | + } |
| 2562 | + |
| 2563 | + qp_bd_offset = 6 * sps->sps_bitdepth_minus8; |
| 2564 | + if (pps->pps_qp_delta_info_in_ph_flag) |
| 2565 | + se(ph_qp_delta, -qp_bd_offset - (26 + pps->pps_init_qp_minus26), |
| 2566 | + 63 - (26 + pps->pps_init_qp_minus26)); |
| 2567 | + |
| 2568 | + if (sps->sps_joint_cbcr_enabled_flag) |
| 2569 | + flag(ph_joint_cbcr_sign_flag); |
| 2570 | + else |
| 2571 | + infer(ph_joint_cbcr_sign_flag, 0); |
| 2572 | + if (sps->sps_sao_enabled_flag && pps->pps_sao_info_in_ph_flag) { |
| 2573 | + flag(ph_sao_luma_enabled_flag); |
| 2574 | + if (sps->sps_chroma_format_idc != 0) |
| 2575 | + flag(ph_sao_chroma_enabled_flag); |
| 2576 | + else |
| 2577 | + infer(ph_sao_chroma_enabled_flag, 0); |
| 2578 | + } else { |
| 2579 | + infer(ph_sao_luma_enabled_flag, 0); |
| 2580 | + infer(ph_sao_chroma_enabled_flag, 0); |
| 2581 | + } |
| 2582 | + |
| 2583 | + if (pps->pps_dbf_info_in_ph_flag) { |
| 2584 | + flag(ph_deblocking_params_present_flag); |
| 2585 | + if (current->ph_deblocking_params_present_flag) { |
| 2586 | + if (!pps->pps_deblocking_filter_disabled_flag) { |
| 2587 | + flag(ph_deblocking_filter_disabled_flag); |
| 2588 | + } else { |
| 2589 | + if (pps->pps_deblocking_filter_disabled_flag && |
| 2590 | + current->ph_deblocking_params_present_flag) { |
| 2591 | + infer(ph_deblocking_filter_disabled_flag, 0); |
| 2592 | + } else { |
| 2593 | + infer(ph_deblocking_filter_disabled_flag, |
| 2594 | + pps->pps_deblocking_filter_disabled_flag); |
| 2595 | + } |
| 2596 | + } |
| 2597 | + if (!current->ph_deblocking_filter_disabled_flag) { |
| 2598 | + se(ph_luma_beta_offset_div2, -12, 12); |
| 2599 | + se(ph_luma_tc_offset_div2, -12, 12); |
| 2600 | + if (pps->pps_chroma_tool_offsets_present_flag) { |
| 2601 | + se(ph_cb_beta_offset_div2, -12, 12); |
| 2602 | + se(ph_cb_tc_offset_div2, -12, 12); |
| 2603 | + se(ph_cr_beta_offset_div2, -12, 12); |
| 2604 | + se(ph_cr_tc_offset_div2, -12, 12); |
| 2605 | + } else { |
| 2606 | + infer(ph_cb_beta_offset_div2, |
| 2607 | + current->ph_luma_beta_offset_div2); |
| 2608 | + infer(ph_cb_tc_offset_div2, |
| 2609 | + current->ph_luma_tc_offset_div2); |
| 2610 | + infer(ph_cr_beta_offset_div2, |
| 2611 | + current->ph_luma_beta_offset_div2); |
| 2612 | + infer(ph_cr_tc_offset_div2, |
| 2613 | + current->ph_luma_tc_offset_div2); |
| 2614 | + } |
| 2615 | + } else { |
| 2616 | + infer(ph_luma_beta_offset_div2, pps->pps_luma_beta_offset_div2); |
| 2617 | + infer(ph_luma_tc_offset_div2, pps->pps_luma_tc_offset_div2); |
| 2618 | + if (pps->pps_chroma_tool_offsets_present_flag) { |
| 2619 | + infer(ph_cb_beta_offset_div2, pps->pps_cb_beta_offset_div2); |
| 2620 | + infer(ph_cb_tc_offset_div2, pps->pps_cb_tc_offset_div2); |
| 2621 | + infer(ph_cr_beta_offset_div2, pps->pps_cr_beta_offset_div2); |
| 2622 | + infer(ph_cr_tc_offset_div2, pps->pps_cr_tc_offset_div2); |
| 2623 | + } else { |
| 2624 | + infer(ph_cb_beta_offset_div2, |
| 2625 | + current->ph_luma_beta_offset_div2); |
| 2626 | + infer(ph_cb_tc_offset_div2, |
| 2627 | + current->ph_luma_tc_offset_div2); |
| 2628 | + infer(ph_cr_beta_offset_div2, |
| 2629 | + current->ph_luma_beta_offset_div2); |
| 2630 | + infer(ph_cr_tc_offset_div2, |
| 2631 | + current->ph_luma_tc_offset_div2); |
| 2632 | + } |
| 2633 | + } |
| 2634 | + } |
| 2635 | + } |
| 2636 | + |
| 2637 | + if (pps->pps_picture_header_extension_present_flag) { |
| 2638 | + ue(ph_extension_length, 0, 256); |
| 2639 | + for (i = 0; i < current->ph_extension_length; i++) |
| 2640 | + us(8, ph_extension_data_byte[i], 0x00, 0xff, 1, i); |
| 2641 | + } |
| 2642 | + |
| 2643 | + return 0; |
| 2644 | +} |
| 2645 | + |
| 2646 | +static int FUNC(ph) (CodedBitstreamContext *ctx, RWContext *rw, |
| 2647 | + H266RawPH *current) |
| 2648 | +{ |
| 2649 | + int err; |
| 2650 | + |
| 2651 | + HEADER("Picture Header"); |
| 2652 | + |
| 2653 | + CHECK(FUNC(nal_unit_header) (ctx, rw, ¤t->nal_unit_header, VVC_PH_NUT)); |
| 2654 | + CHECK(FUNC(picture_header) (ctx, rw, current)); |
| 2655 | + CHECK(FUNC(rbsp_trailing_bits) (ctx, rw)); |
| 2656 | + return 0; |
| 2657 | +} |
| 2658 | + |
| 2659 | +static int FUNC(slice_header) (CodedBitstreamContext *ctx, RWContext *rw, |
| 2660 | + H266RawSliceHeader *current) |
| 2661 | +{ |
| 2662 | + CodedBitstreamH266Context *h266 = ctx->priv_data; |
| 2663 | + const H266RawSPS *sps; |
| 2664 | + const H266RawPPS *pps; |
| 2665 | + const H266RawPH *ph; |
| 2666 | + const H266RefPicLists *ref_pic_lists; |
| 2667 | + int err, i; |
| 2668 | + uint8_t nal_unit_type, qp_bd_offset; |
| 2669 | + uint16_t curr_subpic_idx; |
| 2670 | + uint16_t num_slices_in_subpic; |
| 2671 | + |
| 2672 | + HEADER("Slice Header"); |
| 2673 | + |
| 2674 | + CHECK(FUNC(nal_unit_header) (ctx, rw, ¤t->nal_unit_header, -1)); |
| 2675 | + |
| 2676 | + flag(sh_picture_header_in_slice_header_flag); |
| 2677 | + if (current->sh_picture_header_in_slice_header_flag) { |
| 2678 | + CHECK(FUNC(picture_header) (ctx, rw, ¤t->sh_picture_header)); |
| 2679 | + ph = ¤t->sh_picture_header; |
| 2680 | + //7.4.8 if sh_picture_header_in_slice_header_flag is true, we do not have PH NAL unit |
| 2681 | + h266->priv.ph = NULL; |
| 2682 | + } else { |
| 2683 | + ph = h266->priv.ph; |
| 2684 | + if (!ph) { |
| 2685 | + av_log(ctx->log_ctx, AV_LOG_ERROR, |
| 2686 | + "Picture header not available.\n"); |
| 2687 | + return AVERROR_INVALIDDATA; |
| 2688 | + } |
| 2689 | + } |
| 2690 | + |
| 2691 | + pps = h266->pps[ph->ph_pic_parameter_set_id]; |
| 2692 | + if (!pps) { |
| 2693 | + av_log(ctx->log_ctx, AV_LOG_ERROR, "PPS id %d not available.\n", |
| 2694 | + ph->ph_pic_parameter_set_id); |
| 2695 | + return AVERROR_INVALIDDATA; |
| 2696 | + } |
| 2697 | + sps = h266->sps[pps->pps_seq_parameter_set_id]; |
| 2698 | + if (!sps) { |
| 2699 | + av_log(ctx->log_ctx, AV_LOG_ERROR, "SPS id %d not available.\n", |
| 2700 | + pps->pps_seq_parameter_set_id); |
| 2701 | + return AVERROR_INVALIDDATA; |
| 2702 | + } |
| 2703 | + |
| 2704 | + if (sps->sps_subpic_info_present_flag) { |
| 2705 | + ub(sps->sps_subpic_id_len_minus1 + 1, sh_subpic_id); |
| 2706 | + if (sps->sps_subpic_id_mapping_explicitly_signalled_flag) { |
| 2707 | + for (i = 0; i <= sps->sps_num_subpics_minus1; i++) { |
| 2708 | + uint16_t subpic_id_val = |
| 2709 | + pps->pps_subpic_id_mapping_present_flag ? |
| 2710 | + pps->pps_subpic_id[i] : sps->sps_subpic_id[i]; |
| 2711 | + if (subpic_id_val == current->sh_subpic_id) { |
| 2712 | + curr_subpic_idx = i; |
| 2713 | + break; |
| 2714 | + } |
| 2715 | + } |
| 2716 | + } else { |
| 2717 | + curr_subpic_idx = current->sh_subpic_id; |
| 2718 | + if (curr_subpic_idx > sps->sps_num_subpics_minus1) { |
| 2719 | + av_log(ctx->log_ctx, AV_LOG_ERROR, |
| 2720 | + "sh_subpic_id(%d) should in range [0, %d]\n", |
| 2721 | + curr_subpic_idx, sps->sps_num_subpics_minus1); |
| 2722 | + return AVERROR_INVALIDDATA; |
| 2723 | + } |
| 2724 | + } |
| 2725 | + } else { |
| 2726 | + curr_subpic_idx = 0; |
| 2727 | + } |
| 2728 | + |
| 2729 | + num_slices_in_subpic = pps->num_slices_in_subpic[curr_subpic_idx]; |
| 2730 | + |
| 2731 | + if ((pps->pps_rect_slice_flag && num_slices_in_subpic > 1) || |
| 2732 | + (!pps->pps_rect_slice_flag && pps->num_tiles_in_pic > 1)) { |
| 2733 | + unsigned int bits, max; |
| 2734 | + if (!pps->pps_rect_slice_flag) { |
| 2735 | + bits = av_ceil_log2(pps->num_tiles_in_pic); |
| 2736 | + max = pps->num_tiles_in_pic - 1; |
| 2737 | + } else { |
| 2738 | + bits = av_ceil_log2(num_slices_in_subpic); |
| 2739 | + max = num_slices_in_subpic - 1; |
| 2740 | + } |
| 2741 | + u(bits, sh_slice_address, 0, max); |
| 2742 | + } else { |
| 2743 | + infer(sh_slice_address, 0); |
| 2744 | + } |
| 2745 | + |
| 2746 | + for (i = 0; i < sps->sps_num_extra_sh_bytes * 8; i++) { |
| 2747 | + if (sps->sps_extra_sh_bit_present_flag[i]) |
| 2748 | + flags(sh_extra_bit[i], 1, i); |
| 2749 | + } |
| 2750 | + |
| 2751 | + if (!pps->pps_rect_slice_flag && |
| 2752 | + pps->num_tiles_in_pic - current->sh_slice_address > 1) |
| 2753 | + ue(sh_num_tiles_in_slice_minus1, 0, pps->num_tiles_in_pic - 1); |
| 2754 | + else |
| 2755 | + infer(sh_num_tiles_in_slice_minus1, 0); |
| 2756 | + |
| 2757 | + if (ph->ph_inter_slice_allowed_flag) |
| 2758 | + ue(sh_slice_type, 0, 2); |
| 2759 | + else |
| 2760 | + infer(sh_slice_type, 2); |
| 2761 | + |
| 2762 | + nal_unit_type = current->nal_unit_header.nal_unit_type; |
| 2763 | + if (nal_unit_type == VVC_IDR_W_RADL || nal_unit_type == VVC_IDR_N_LP || |
| 2764 | + nal_unit_type == VVC_CRA_NUT || nal_unit_type == VVC_GDR_NUT) |
| 2765 | + flag(sh_no_output_of_prior_pics_flag); |
| 2766 | + if (sps->sps_alf_enabled_flag && !pps->pps_alf_info_in_ph_flag) { |
| 2767 | + flag(sh_alf_enabled_flag); |
| 2768 | + if (current->sh_alf_enabled_flag) { |
| 2769 | + ub(3, sh_num_alf_aps_ids_luma); |
| 2770 | + for (i = 0; i < current->sh_num_alf_aps_ids_luma; i++) |
| 2771 | + ubs(3, sh_alf_aps_id_luma[i], 1, i); |
| 2772 | + if (sps->sps_chroma_format_idc != 0) { |
| 2773 | + flag(sh_alf_cb_enabled_flag); |
| 2774 | + flag(sh_alf_cr_enabled_flag); |
| 2775 | + } else { |
| 2776 | + infer(sh_alf_cb_enabled_flag, ph->ph_alf_cb_enabled_flag); |
| 2777 | + infer(sh_alf_cr_enabled_flag, ph->ph_alf_cr_enabled_flag); |
| 2778 | + } |
| 2779 | + if (current->sh_alf_cb_enabled_flag || |
| 2780 | + current->sh_alf_cr_enabled_flag) |
| 2781 | + ub(3, sh_alf_aps_id_chroma); |
| 2782 | + else |
| 2783 | + infer(sh_alf_aps_id_chroma, ph->ph_alf_aps_id_chroma); |
| 2784 | + if (sps->sps_ccalf_enabled_flag) { |
| 2785 | + flag(sh_alf_cc_cb_enabled_flag); |
| 2786 | + if (current->sh_alf_cc_cb_enabled_flag) |
| 2787 | + ub(3, sh_alf_cc_cb_aps_id); |
| 2788 | + else |
| 2789 | + infer(sh_alf_cc_cb_aps_id, ph->ph_alf_cc_cb_aps_id); |
| 2790 | + flag(sh_alf_cc_cr_enabled_flag); |
| 2791 | + if (current->sh_alf_cc_cr_enabled_flag) |
| 2792 | + ub(3, sh_alf_cc_cr_aps_id); |
| 2793 | + else |
| 2794 | + infer(sh_alf_cc_cr_aps_id, ph->ph_alf_cc_cr_aps_id); |
| 2795 | + } else { |
| 2796 | + infer(sh_alf_cc_cb_enabled_flag, ph->ph_alf_cc_cb_enabled_flag); |
| 2797 | + infer(sh_alf_cc_cr_enabled_flag, ph->ph_alf_cc_cr_enabled_flag); |
| 2798 | + infer(sh_alf_cc_cb_aps_id, ph->ph_alf_cc_cb_aps_id); |
| 2799 | + infer(sh_alf_cc_cr_aps_id, ph->ph_alf_cc_cr_aps_id); |
| 2800 | + } |
| 2801 | + } |
| 2802 | + } else { |
| 2803 | + infer(sh_alf_enabled_flag, 0); |
| 2804 | + } |
| 2805 | + |
| 2806 | + if (ph->ph_lmcs_enabled_flag && |
| 2807 | + !current->sh_picture_header_in_slice_header_flag) |
| 2808 | + flag(sh_lmcs_used_flag); |
| 2809 | + else |
| 2810 | + infer(sh_lmcs_used_flag, 0); |
| 2811 | + |
| 2812 | + if (ph->ph_explicit_scaling_list_enabled_flag && |
| 2813 | + !current->sh_picture_header_in_slice_header_flag) |
| 2814 | + flag(sh_explicit_scaling_list_used_flag); |
| 2815 | + else |
| 2816 | + infer(sh_explicit_scaling_list_used_flag, 0); |
| 2817 | + |
| 2818 | + if (!pps->pps_rpl_info_in_ph_flag && |
| 2819 | + ((nal_unit_type != VVC_IDR_W_RADL && |
| 2820 | + nal_unit_type != VVC_IDR_N_LP) || sps->sps_idr_rpl_present_flag)) { |
| 2821 | + CHECK(FUNC(ref_pic_lists) |
| 2822 | + (ctx, rw, sps, pps, ¤t->sh_ref_pic_lists)); |
| 2823 | + ref_pic_lists = ¤t->sh_ref_pic_lists; |
| 2824 | + } else { |
| 2825 | + ref_pic_lists = &h266->priv.ph->ph_ref_pic_lists; |
| 2826 | + } |
| 2827 | + if ((current->sh_slice_type != VVC_SLICE_TYPE_I && |
| 2828 | + ref_pic_lists->rpl_ref_list[0].num_ref_entries > 1) || |
| 2829 | + (current->sh_slice_type == VVC_SLICE_TYPE_B && |
| 2830 | + ref_pic_lists->rpl_ref_list[1].num_ref_entries > 1)) { |
| 2831 | + flag(sh_num_ref_idx_active_override_flag); |
| 2832 | + if (current->sh_num_ref_idx_active_override_flag) { |
| 2833 | + for (i = 0; |
| 2834 | + i < (current->sh_slice_type == VVC_SLICE_TYPE_B ? 2 : 1); i++) |
| 2835 | + if (ref_pic_lists->rpl_ref_list[i].num_ref_entries > 1) |
| 2836 | + ues(sh_num_ref_idx_active_minus1[i], 0, 14, 1, i); |
| 2837 | + else |
| 2838 | + infer(sh_num_ref_idx_active_minus1[i], 0); |
| 2839 | + } |
| 2840 | + } else { |
| 2841 | + infer(sh_num_ref_idx_active_override_flag, 1); |
| 2842 | + } |
| 2843 | + if (current->sh_slice_type != VVC_SLICE_TYPE_I) { |
| 2844 | + if (pps->pps_cabac_init_present_flag) |
| 2845 | + flag(sh_cabac_init_flag); |
| 2846 | + else |
| 2847 | + infer(sh_cabac_init_flag, 0); |
| 2848 | + if (ph->ph_temporal_mvp_enabled_flag && !pps->pps_rpl_info_in_ph_flag) { |
| 2849 | + uint8_t num_ref_idx_active[2]; |
| 2850 | + for (i = 0; i < 2; i++) { |
| 2851 | + if (current->sh_slice_type == VVC_SLICE_TYPE_B || |
| 2852 | + (current->sh_slice_type == VVC_SLICE_TYPE_P && i == 0)) { |
| 2853 | + if (current->sh_num_ref_idx_active_override_flag) { |
| 2854 | + num_ref_idx_active[i] = |
| 2855 | + current->sh_num_ref_idx_active_minus1[i] + 1; |
| 2856 | + } else { |
| 2857 | + num_ref_idx_active[i] = |
| 2858 | + FFMIN(ref_pic_lists->rpl_ref_list[i].num_ref_entries, |
| 2859 | + pps->pps_num_ref_idx_default_active_minus1[i] + 1); |
| 2860 | + } |
| 2861 | + } else { |
| 2862 | + num_ref_idx_active[i] = 0; |
| 2863 | + } |
| 2864 | + } |
| 2865 | + |
| 2866 | + if (current->sh_slice_type == VVC_SLICE_TYPE_B) |
| 2867 | + flag(sh_collocated_from_l0_flag); |
| 2868 | + else |
| 2869 | + infer(sh_collocated_from_l0_flag, 1); |
| 2870 | + if ((current->sh_collocated_from_l0_flag && |
| 2871 | + num_ref_idx_active[0] > 1) || |
| 2872 | + (!current->sh_collocated_from_l0_flag && |
| 2873 | + num_ref_idx_active[1] > 1)) { |
| 2874 | + unsigned int idx = current->sh_collocated_from_l0_flag ? 0 : 1; |
| 2875 | + ue(sh_collocated_ref_idx, 0, num_ref_idx_active[idx] - 1); |
| 2876 | + } else { |
| 2877 | + infer(sh_collocated_ref_idx, 0); |
| 2878 | + } |
| 2879 | + if (!pps->pps_wp_info_in_ph_flag && |
| 2880 | + ((pps->pps_weighted_pred_flag && |
| 2881 | + current->sh_slice_type == VVC_SLICE_TYPE_P) || |
| 2882 | + (pps->pps_weighted_bipred_flag && |
| 2883 | + current->sh_slice_type == VVC_SLICE_TYPE_B))) { |
| 2884 | + CHECK(FUNC(pred_weight_table) (ctx, rw, sps, pps, ref_pic_lists, |
| 2885 | + num_ref_idx_active, |
| 2886 | + ¤t->sh_pred_weight_table)); |
| 2887 | + } |
| 2888 | + } |
| 2889 | + |
| 2890 | + } |
| 2891 | + qp_bd_offset = 6 * sps->sps_bitdepth_minus8; |
| 2892 | + if (!pps->pps_qp_delta_info_in_ph_flag) |
| 2893 | + se(sh_qp_delta, -qp_bd_offset - (26 + pps->pps_init_qp_minus26), |
| 2894 | + 63 - (26 + pps->pps_init_qp_minus26)); |
| 2895 | + if (pps->pps_slice_chroma_qp_offsets_present_flag) { |
| 2896 | + int8_t off; |
| 2897 | + |
| 2898 | + se(sh_cb_qp_offset, -12, 12); |
| 2899 | + off = pps->pps_cb_qp_offset + current->sh_cb_qp_offset; |
| 2900 | + if (off < -12 || off > 12) { |
| 2901 | + av_log(ctx->log_ctx, AV_LOG_ERROR, |
| 2902 | + "pps_cb_qp_offset + sh_cb_qp_offset (%d) not in range [-12, 12].\n", |
| 2903 | + off); |
| 2904 | + return AVERROR_INVALIDDATA; |
| 2905 | + } |
| 2906 | + |
| 2907 | + se(sh_cr_qp_offset, -12, 12); |
| 2908 | + off = pps->pps_cr_qp_offset + current->sh_cr_qp_offset; |
| 2909 | + if (off < -12 || off > 12) { |
| 2910 | + av_log(ctx->log_ctx, AV_LOG_ERROR, |
| 2911 | + "pps_cr_qp_offset + sh_cr_qp_offset (%d) not in range [-12, 12].\n", |
| 2912 | + off); |
| 2913 | + return AVERROR_INVALIDDATA; |
| 2914 | + } |
| 2915 | + |
| 2916 | + if (sps->sps_joint_cbcr_enabled_flag) { |
| 2917 | + se(sh_joint_cbcr_qp_offset, -12, 12); |
| 2918 | + off = |
| 2919 | + pps->pps_joint_cbcr_qp_offset_value + |
| 2920 | + current->sh_joint_cbcr_qp_offset; |
| 2921 | + if (off < -12 || off > 12) { |
| 2922 | + av_log(ctx->log_ctx, AV_LOG_ERROR, |
| 2923 | + "pps_joint_cbcr_qp_offset_value + sh_joint_cbcr_qp_offset (%d)" |
| 2924 | + "not in range [-12, 12]. \n", off); |
| 2925 | + return AVERROR_INVALIDDATA; |
| 2926 | + } |
| 2927 | + } else { |
| 2928 | + infer(sh_joint_cbcr_qp_offset, 0); |
| 2929 | + } |
| 2930 | + } else { |
| 2931 | + infer(sh_cb_qp_offset, 0); |
| 2932 | + infer(sh_cr_qp_offset, 0); |
| 2933 | + infer(sh_joint_cbcr_qp_offset, 0); |
| 2934 | + } |
| 2935 | + if (pps->pps_cu_chroma_qp_offset_list_enabled_flag) |
| 2936 | + flag(sh_cu_chroma_qp_offset_enabled_flag); |
| 2937 | + else |
| 2938 | + infer(sh_cu_chroma_qp_offset_enabled_flag, 0); |
| 2939 | + if (sps->sps_sao_enabled_flag && !pps->pps_sao_info_in_ph_flag) { |
| 2940 | + flag(sh_sao_luma_used_flag); |
| 2941 | + if (sps->sps_chroma_format_idc != 0) |
| 2942 | + flag(sh_sao_chroma_used_flag); |
| 2943 | + else |
| 2944 | + infer(sh_sao_chroma_used_flag, ph->ph_sao_chroma_enabled_flag); |
| 2945 | + } else { |
| 2946 | + infer(sh_sao_luma_used_flag, ph->ph_sao_luma_enabled_flag); |
| 2947 | + infer(sh_sao_chroma_used_flag, ph->ph_sao_chroma_enabled_flag); |
| 2948 | + } |
| 2949 | + |
| 2950 | + if (pps->pps_deblocking_filter_override_enabled_flag && |
| 2951 | + !pps->pps_dbf_info_in_ph_flag) |
| 2952 | + flag(sh_deblocking_params_present_flag); |
| 2953 | + else |
| 2954 | + infer(sh_deblocking_params_present_flag, 0); |
| 2955 | + if (current->sh_deblocking_params_present_flag) { |
| 2956 | + if (!pps->pps_deblocking_filter_disabled_flag) |
| 2957 | + flag(sh_deblocking_filter_disabled_flag); |
| 2958 | + else |
| 2959 | + infer(sh_deblocking_filter_disabled_flag, |
| 2960 | + !(pps->pps_deblocking_filter_disabled_flag && |
| 2961 | + current->sh_deblocking_params_present_flag)); |
| 2962 | + if (!current->sh_deblocking_filter_disabled_flag) { |
| 2963 | + se(sh_luma_beta_offset_div2, -12, 12); |
| 2964 | + se(sh_luma_tc_offset_div2, -12, 12); |
| 2965 | + if (pps->pps_chroma_tool_offsets_present_flag) { |
| 2966 | + se(sh_cb_beta_offset_div2, -12, 12); |
| 2967 | + se(sh_cb_tc_offset_div2, -12, 12); |
| 2968 | + se(sh_cr_beta_offset_div2, -12, 12); |
| 2969 | + se(sh_cr_tc_offset_div2, -12, 12); |
| 2970 | + } else { |
| 2971 | + infer(sh_cb_beta_offset_div2, |
| 2972 | + current->sh_luma_beta_offset_div2); |
| 2973 | + infer(sh_cb_tc_offset_div2, current->sh_luma_tc_offset_div2); |
| 2974 | + infer(sh_cr_beta_offset_div2, |
| 2975 | + current->sh_luma_beta_offset_div2); |
| 2976 | + infer(sh_cr_tc_offset_div2, current->sh_luma_tc_offset_div2); |
| 2977 | + } |
| 2978 | + } else { |
| 2979 | + infer(sh_luma_beta_offset_div2, ph->ph_luma_beta_offset_div2); |
| 2980 | + infer(sh_luma_tc_offset_div2, ph->ph_luma_tc_offset_div2); |
| 2981 | + if (pps->pps_chroma_tool_offsets_present_flag) { |
| 2982 | + infer(sh_cb_beta_offset_div2, ph->ph_cb_beta_offset_div2); |
| 2983 | + infer(sh_cb_tc_offset_div2, ph->ph_cb_tc_offset_div2); |
| 2984 | + infer(sh_cr_beta_offset_div2, ph->ph_cr_beta_offset_div2); |
| 2985 | + infer(sh_cr_tc_offset_div2, ph->ph_cr_beta_offset_div2); |
| 2986 | + } else { |
| 2987 | + infer(sh_cb_beta_offset_div2, |
| 2988 | + current->sh_luma_beta_offset_div2); |
| 2989 | + infer(sh_cb_tc_offset_div2, current->sh_luma_tc_offset_div2); |
| 2990 | + infer(sh_cr_beta_offset_div2, |
| 2991 | + current->sh_luma_beta_offset_div2); |
| 2992 | + infer(sh_cr_tc_offset_div2, current->sh_luma_tc_offset_div2); |
| 2993 | + } |
| 2994 | + } |
| 2995 | + } |
| 2996 | + |
| 2997 | + if (sps->sps_dep_quant_enabled_flag) |
| 2998 | + flag(sh_dep_quant_used_flag); |
| 2999 | + else |
| 3000 | + infer(sh_dep_quant_used_flag, 0); |
| 3001 | + |
| 3002 | + if (sps->sps_sign_data_hiding_enabled_flag && |
| 3003 | + !current->sh_dep_quant_used_flag) |
| 3004 | + flag(sh_sign_data_hiding_used_flag); |
| 3005 | + else |
| 3006 | + infer(sh_sign_data_hiding_used_flag, 0); |
| 3007 | + |
| 3008 | + if (sps->sps_transform_skip_enabled_flag && |
| 3009 | + !current->sh_dep_quant_used_flag && |
| 3010 | + !current->sh_sign_data_hiding_used_flag) |
| 3011 | + flag(sh_ts_residual_coding_disabled_flag); |
| 3012 | + else |
| 3013 | + infer(sh_ts_residual_coding_disabled_flag, 0); |
| 3014 | + if (pps->pps_slice_header_extension_present_flag) { |
| 3015 | + ue(sh_slice_header_extension_length, 0, 256); |
| 3016 | + for (i = 0; i < current->sh_slice_header_extension_length; i++) |
| 3017 | + us(8, sh_slice_header_extension_data_byte[i], 0x00, 0xff, 1, i); |
| 3018 | + } |
| 3019 | + if (sps->sps_entry_point_offsets_present_flag) { |
| 3020 | + int num_entry_points = 0; |
| 3021 | + uint8_t entropy_sync = sps->sps_entropy_coding_sync_enabled_flag; |
| 3022 | + int height; |
| 3023 | + if (pps->pps_rect_slice_flag) { |
| 3024 | + int width_in_tiles; |
| 3025 | + int slice_idx = current->sh_slice_address; |
| 3026 | + for (i = 0; i < curr_subpic_idx; i++) { |
| 3027 | + slice_idx += pps->num_slices_in_subpic[i]; |
| 3028 | + } |
| 3029 | + width_in_tiles = |
| 3030 | + pps->pps_slice_width_in_tiles_minus1[slice_idx] + 1; |
| 3031 | + |
| 3032 | + if (entropy_sync) |
| 3033 | + height = pps->slice_height_in_ctus[slice_idx]; |
| 3034 | + else |
| 3035 | + height = pps->pps_slice_height_in_tiles_minus1[slice_idx] + 1; |
| 3036 | + |
| 3037 | + num_entry_points = width_in_tiles * height; |
| 3038 | + } else { |
| 3039 | + int tile_idx; |
| 3040 | + int tile_y; |
| 3041 | + for (tile_idx = current->sh_slice_address; |
| 3042 | + tile_idx <= |
| 3043 | + current->sh_slice_address + |
| 3044 | + current->sh_num_tiles_in_slice_minus1; tile_idx++) { |
| 3045 | + tile_y = tile_idx / pps->num_tile_rows; |
| 3046 | + height = pps->pps_tile_row_height_minus1[tile_y] + 1; |
| 3047 | + num_entry_points += (entropy_sync ? height : 1); |
| 3048 | + } |
| 3049 | + } |
| 3050 | + num_entry_points--; |
| 3051 | + if (num_entry_points > VVC_MAX_ENTRY_POINTS) { |
| 3052 | + av_log(ctx->log_ctx, AV_LOG_ERROR, "Too many entry points: " |
| 3053 | + "%" PRIu16 ".\n", num_entry_points); |
| 3054 | + return AVERROR_PATCHWELCOME; |
| 3055 | + } |
| 3056 | + if (num_entry_points > 0) { |
| 3057 | + ue(sh_entry_offset_len_minus1, 0, 31); |
| 3058 | + for (i = 0; i < num_entry_points; i++) { |
| 3059 | + ubs(current->sh_entry_offset_len_minus1 + 1, |
| 3060 | + sh_entry_point_offset_minus1[i], 1, i); |
| 3061 | + } |
| 3062 | + } |
| 3063 | + } |
| 3064 | + CHECK(FUNC(byte_alignment) (ctx, rw)); |
| 3065 | + |
| 3066 | + return 0; |
| 3067 | +} |
| 3068 | + |
| 3069 | +static int FUNC(sei_decoded_picture_hash) (CodedBitstreamContext *ctx, |
| 3070 | + RWContext *rw, |
| 3071 | + H266RawSEIDecodedPictureHash * |
| 3072 | + current) |
| 3073 | +{ |
| 3074 | + int err, c_idx, i; |
| 3075 | + |
| 3076 | + HEADER("Decoded Picture Hash"); |
| 3077 | + |
| 3078 | + u(8, dph_sei_hash_type, 0, 2); |
| 3079 | + flag(dph_sei_single_component_flag); |
| 3080 | + ub(7, dph_sei_reserved_zero_7bits); |
| 3081 | + |
| 3082 | + for (c_idx = 0; c_idx < (current->dph_sei_single_component_flag ? 1 : 3); |
| 3083 | + c_idx++) { |
| 3084 | + if (current->dph_sei_hash_type == 0) { |
| 3085 | + for (i = 0; i < 16; i++) |
| 3086 | + us(8, dph_sei_picture_md5[c_idx][i], 0x00, 0xff, 2, c_idx, i); |
| 3087 | + } else if (current->dph_sei_hash_type == 1) { |
| 3088 | + us(16, dph_sei_picture_crc[c_idx], 0x0000, 0xffff, 1, c_idx); |
| 3089 | + } else if (current->dph_sei_hash_type == 2) { |
| 3090 | + us(32, dph_sei_picture_checksum[c_idx], 0x00000000, 0xffffffff, 1, |
| 3091 | + c_idx); |
| 3092 | + } |
| 3093 | + } |
| 3094 | + return 0; |
| 3095 | +} |
| 3096 | + |
| 3097 | +static int FUNC(sei) (CodedBitstreamContext *ctx, RWContext *rw, |
| 3098 | + H266RawSEI *current, int prefix) |
| 3099 | +{ |
| 3100 | + int err; |
| 3101 | + |
| 3102 | + if (prefix) |
| 3103 | + HEADER("Prefix Supplemental Enhancement Information"); |
| 3104 | + else |
| 3105 | + HEADER("Suffix Supplemental Enhancement Information"); |
| 3106 | + |
| 3107 | + CHECK(FUNC(nal_unit_header) (ctx, rw, ¤t->nal_unit_header, |
| 3108 | + prefix ? VVC_PREFIX_SEI_NUT |
| 3109 | + : VVC_SUFFIX_SEI_NUT)); |
| 3110 | + |
| 3111 | + CHECK(FUNC_SEI(message_list) (ctx, rw, ¤t->message_list, prefix)); |
| 3112 | + |
| 3113 | + CHECK(FUNC(rbsp_trailing_bits) (ctx, rw)); |
| 3114 | + |
| 3115 | + return 0; |
| 3116 | +} |