+static int FUNC(sei_buffering_period)(CodedBitstreamContext *ctx, RWContext *rw,
+ H265RawSEIBufferingPeriod *current,
+ uint32_t *payload_size,
+ int *more_data)
+{
+ CodedBitstreamH265Context *h265 = ctx->priv_data;
+ const H265RawSPS *sps;
+ const H265RawHRDParameters *hrd;
+ int err, i, length;
+
+#ifdef READ
+ int start_pos, end_pos;
+ start_pos = get_bits_count(rw);
+#endif
+
+ HEADER("Buffering Period");
+
+ ue(bp_seq_parameter_set_id, 0, HEVC_MAX_SPS_COUNT - 1);
+
+ sps = h265->sps[current->bp_seq_parameter_set_id];
+ if (!sps) {
+ av_log(ctx->log_ctx, AV_LOG_ERROR, "SPS id %d not available.\n",
+ current->bp_seq_parameter_set_id);
+ return AVERROR_INVALIDDATA;
+ }
+ h265->active_sps = sps;
+
+ if (!sps->vui_parameters_present_flag ||
+ !sps->vui.vui_hrd_parameters_present_flag) {
+ av_log(ctx->log_ctx, AV_LOG_ERROR, "Buffering period SEI requires "
+ "HRD parameters to be present in SPS.\n");
+ return AVERROR_INVALIDDATA;
+ }
+ hrd = &sps->vui.hrd_parameters;
+ if (!hrd->nal_hrd_parameters_present_flag &&
+ !hrd->vcl_hrd_parameters_present_flag) {
+ av_log(ctx->log_ctx, AV_LOG_ERROR, "Buffering period SEI requires "
+ "NAL or VCL HRD parameters to be present.\n");
+ return AVERROR_INVALIDDATA;
+ }
+
+ if (!hrd->sub_pic_hrd_params_present_flag)
+ flag(irap_cpb_params_present_flag);
+ else
+ infer(irap_cpb_params_present_flag, 0);
+ if (current->irap_cpb_params_present_flag) {
+ length = hrd->au_cpb_removal_delay_length_minus1 + 1;
+ ub(length, cpb_delay_offset);
+ length = hrd->dpb_output_delay_length_minus1 + 1;
+ ub(length, dpb_delay_offset);
+ } else {
+ infer(cpb_delay_offset, 0);
+ infer(dpb_delay_offset, 0);
+ }
+
+ flag(concatenation_flag);
+
+ length = hrd->au_cpb_removal_delay_length_minus1 + 1;
+ ub(length, au_cpb_removal_delay_delta_minus1);
+
+ if (hrd->nal_hrd_parameters_present_flag) {
+ for (i = 0; i <= hrd->cpb_cnt_minus1[0]; i++) {
+ length = hrd->initial_cpb_removal_delay_length_minus1 + 1;
+
+ ubs(length, nal_initial_cpb_removal_delay[i], 1, i);
+ ubs(length, nal_initial_cpb_removal_offset[i], 1, i);
+
+ if (hrd->sub_pic_hrd_params_present_flag ||
+ current->irap_cpb_params_present_flag) {
+ ubs(length, nal_initial_alt_cpb_removal_delay[i], 1, i);
+ ubs(length, nal_initial_alt_cpb_removal_offset[i], 1, i);
+ }
+ }
+ }
+ if (hrd->vcl_hrd_parameters_present_flag) {
+ for (i = 0; i <= hrd->cpb_cnt_minus1[0]; i++) {
+ length = hrd->initial_cpb_removal_delay_length_minus1 + 1;
+
+ ubs(length, vcl_initial_cpb_removal_delay[i], 1, i);
+ ubs(length, vcl_initial_cpb_removal_offset[i], 1, i);
+
+ if (hrd->sub_pic_hrd_params_present_flag ||
+ current->irap_cpb_params_present_flag) {
+ ubs(length, vcl_initial_alt_cpb_removal_delay[i], 1, i);
+ ubs(length, vcl_initial_alt_cpb_removal_offset[i], 1, i);
+ }
+ }
+ }
+
+#ifdef READ
+ end_pos = get_bits_count(rw);
+ if (cbs_h265_payload_extension_present(rw, *payload_size,
+ end_pos - start_pos))
+ flag(use_alt_cpb_params_flag);
+ else
+ infer(use_alt_cpb_params_flag, 0);
+#else
+ // If unknown extension data exists, then use_alt_cpb_params_flag is
+ // coded in the bitstream and must be written even if it's 0.
+ if (current->use_alt_cpb_params_flag || *more_data) {
+ flag(use_alt_cpb_params_flag);
+ // Ensure this bit is not the last in the payload by making the
+ // more_data_in_payload() check evaluate to true, so it may not
+ // be mistaken as something else by decoders.
+ *more_data = 1;
+ }
+#endif
+
+ return 0;
+}
+
+static int FUNC(sei_pic_timing)(CodedBitstreamContext *ctx, RWContext *rw,
+ H265RawSEIPicTiming *current)
+{
+ CodedBitstreamH265Context *h265 = ctx->priv_data;
+ const H265RawSPS *sps;
+ const H265RawHRDParameters *hrd;
+ int err, expected_source_scan_type, i, length;
+
+ HEADER("Picture Timing");
+
+ sps = h265->active_sps;
+ if (!sps) {
+ av_log(ctx->log_ctx, AV_LOG_ERROR,
+ "No active SPS for pic_timing.\n");
+ return AVERROR_INVALIDDATA;
+ }
+
+ expected_source_scan_type = 2 -
+ 2 * sps->profile_tier_level.general_interlaced_source_flag -
+ sps->profile_tier_level.general_progressive_source_flag;
+
+ if (sps->vui.frame_field_info_present_flag) {
+ u(4, pic_struct, 0, 12);
+ u(2, source_scan_type,
+ expected_source_scan_type >= 0 ? expected_source_scan_type : 0,
+ expected_source_scan_type >= 0 ? expected_source_scan_type : 2);
+ flag(duplicate_flag);
+ } else {
+ infer(pic_struct, 0);
+ infer(source_scan_type,
+ expected_source_scan_type >= 0 ? expected_source_scan_type : 2);
+ infer(duplicate_flag, 0);
+ }
+
+ if (sps->vui_parameters_present_flag &&
+ sps->vui.vui_hrd_parameters_present_flag)
+ hrd = &sps->vui.hrd_parameters;
+ else
+ hrd = NULL;
+ if (hrd && (hrd->nal_hrd_parameters_present_flag ||
+ hrd->vcl_hrd_parameters_present_flag)) {
+ length = hrd->au_cpb_removal_delay_length_minus1 + 1;
+ ub(length, au_cpb_removal_delay_minus1);
+
+ length = hrd->dpb_output_delay_length_minus1 + 1;
+ ub(length, pic_dpb_output_delay);
+
+ if (hrd->sub_pic_hrd_params_present_flag) {
+ length = hrd->dpb_output_delay_du_length_minus1 + 1;
+ ub(length, pic_dpb_output_du_delay);
+ }
+
+ if (hrd->sub_pic_hrd_params_present_flag &&
+ hrd->sub_pic_cpb_params_in_pic_timing_sei_flag) {
+ // Each decoding unit must contain at least one slice segment.
+ ue(num_decoding_units_minus1, 0, HEVC_MAX_SLICE_SEGMENTS);
+ flag(du_common_cpb_removal_delay_flag);
+
+ length = hrd->du_cpb_removal_delay_increment_length_minus1 + 1;
+ if (current->du_common_cpb_removal_delay_flag)
+ ub(length, du_common_cpb_removal_delay_increment_minus1);
+
+ for (i = 0; i <= current->num_decoding_units_minus1; i++) {
+ ues(num_nalus_in_du_minus1[i],
+ 0, HEVC_MAX_SLICE_SEGMENTS, 1, i);
+ if (!current->du_common_cpb_removal_delay_flag &&
+ i < current->num_decoding_units_minus1)
+ ubs(length, du_cpb_removal_delay_increment_minus1[i], 1, i);
+ }
+ }
+ }
+
+ return 0;
+}
+
+static int FUNC(sei_pan_scan_rect)(CodedBitstreamContext *ctx, RWContext *rw,
+ H265RawSEIPanScanRect *current)
+{
+ int err, i;
+
+ HEADER("Pan-Scan Rectangle");
+
+ ue(pan_scan_rect_id, 0, UINT32_MAX - 1);
+ flag(pan_scan_rect_cancel_flag);
+
+ if (!current->pan_scan_rect_cancel_flag) {
+ ue(pan_scan_cnt_minus1, 0, 2);
+
+ for (i = 0; i <= current->pan_scan_cnt_minus1; i++) {
+ ses(pan_scan_rect_left_offset[i], INT32_MIN + 1, INT32_MAX, 1, i);
+ ses(pan_scan_rect_right_offset[i], INT32_MIN + 1, INT32_MAX, 1, i);
+ ses(pan_scan_rect_top_offset[i], INT32_MIN + 1, INT32_MAX, 1, i);
+ ses(pan_scan_rect_bottom_offset[i], INT32_MIN + 1, INT32_MAX, 1, i);
+ }
+
+ flag(pan_scan_rect_persistence_flag);
+ }
+
+ return 0;
+}
+
+static int FUNC(sei_user_data_registered)(CodedBitstreamContext *ctx, RWContext *rw,
+ H265RawSEIUserDataRegistered *current,
+ uint32_t *payload_size)
+{
+ int err, i, j;
+
+ HEADER("User Data Registered ITU-T T.35");
+
+ u(8, itu_t_t35_country_code, 0x00, 0xff);
+ if (current->itu_t_t35_country_code != 0xff)
+ i = 1;
+ else {
+ u(8, itu_t_t35_country_code_extension_byte, 0x00, 0xff);
+ i = 2;
+ }
+
+#ifdef READ
+ if (*payload_size < i) {
+ av_log(ctx->log_ctx, AV_LOG_ERROR,
+ "Invalid SEI user data registered payload.\n");
+ return AVERROR_INVALIDDATA;
+ }
+ current->data_length = *payload_size - i;
+#else
+ *payload_size = i + current->data_length;
+#endif
+
+ allocate(current->data, current->data_length);
+ for (j = 0; j < current->data_length; j++)
+ xu(8, itu_t_t35_payload_byte[i], current->data[j], 0x00, 0xff, 1, i + j);
+
+ return 0;
+}
+
+static int FUNC(sei_user_data_unregistered)(CodedBitstreamContext *ctx, RWContext *rw,
+ H265RawSEIUserDataUnregistered *current,
+ uint32_t *payload_size)
+{
+ int err, i;
+
+ HEADER("User Data Unregistered");
+
+#ifdef READ
+ if (*payload_size < 16) {
+ av_log(ctx->log_ctx, AV_LOG_ERROR,
+ "Invalid SEI user data unregistered payload.\n");
+ return AVERROR_INVALIDDATA;
+ }
+ current->data_length = *payload_size - 16;
+#else
+ *payload_size = 16 + current->data_length;
+#endif
+
+ for (i = 0; i < 16; i++)
+ us(8, uuid_iso_iec_11578[i], 0x00, 0xff, 1, i);
+
+ allocate(current->data, current->data_length);
+
+ for (i = 0; i < current->data_length; i++)
+ xu(8, user_data_payload_byte[i], current->data[i], 0x00, 0xff, 1, i);
+
+ return 0;
+}
+
+static int FUNC(sei_recovery_point)(CodedBitstreamContext *ctx, RWContext *rw,
+ H265RawSEIRecoveryPoint *current)
+{
+ int err;
+
+ HEADER("Recovery Point");
+
+ se(recovery_poc_cnt, -32768, 32767);
+
+ flag(exact_match_flag);
+ flag(broken_link_flag);
+
+ return 0;
+}
+
+static int FUNC(sei_display_orientation)(CodedBitstreamContext *ctx, RWContext *rw,
+ H265RawSEIDisplayOrientation *current)
+{
+ int err;
+
+ HEADER("Display Orientation");
+
+ flag(display_orientation_cancel_flag);
+ if (!current->display_orientation_cancel_flag) {
+ flag(hor_flip);
+ flag(ver_flip);
+ ub(16, anticlockwise_rotation);
+ flag(display_orientation_persistence_flag);
+ }
+
+ return 0;
+}
+
+static int FUNC(sei_active_parameter_sets)(CodedBitstreamContext *ctx, RWContext *rw,
+ H265RawSEIActiveParameterSets *current)
+{
+ CodedBitstreamH265Context *h265 = ctx->priv_data;
+ const H265RawVPS *vps;
+ int err, i;
+
+ HEADER("Active Parameter Sets");
+
+ u(4, active_video_parameter_set_id, 0, HEVC_MAX_VPS_COUNT);
+ vps = h265->vps[current->active_video_parameter_set_id];
+ if (!vps) {
+ av_log(ctx->log_ctx, AV_LOG_ERROR, "VPS id %d not available for active "
+ "parameter sets.\n", current->active_video_parameter_set_id);
+ return AVERROR_INVALIDDATA;
+ }
+ h265->active_vps = vps;
+
+ flag(self_contained_cvs_flag);
+ flag(no_parameter_set_update_flag);
+
+ ue(num_sps_ids_minus1, 0, HEVC_MAX_SPS_COUNT - 1);
+ for (i = 0; i <= current->num_sps_ids_minus1; i++)
+ ues(active_seq_parameter_set_id[i], 0, HEVC_MAX_SPS_COUNT - 1, 1, i);
+
+ for (i = vps->vps_base_layer_internal_flag;
+ i <= FFMIN(62, vps->vps_max_layers_minus1); i++) {
+ ues(layer_sps_idx[i], 0, current->num_sps_ids_minus1, 1, i);
+
+ if (i == 0)
+ h265->active_sps = h265->sps[current->active_seq_parameter_set_id[current->layer_sps_idx[0]]];
+ }
+
+ return 0;
+}
+
+static int FUNC(sei_decoded_picture_hash)(CodedBitstreamContext *ctx, RWContext *rw,
+ H265RawSEIDecodedPictureHash *current)
+{
+ CodedBitstreamH265Context *h265 = ctx->priv_data;
+ const H265RawSPS *sps = h265->active_sps;
+ int err, c, i;
+
+ HEADER("Decoded Picture Hash");
+
+ if (!sps) {
+ av_log(ctx->log_ctx, AV_LOG_ERROR,
+ "No active SPS for decoded picture hash.\n");
+ return AVERROR_INVALIDDATA;
+ }
+
+ u(8, hash_type, 0, 2);
+
+ for (c = 0; c < (sps->chroma_format_idc == 0 ? 1 : 3); c++) {
+ if (current->hash_type == 0) {
+ for (i = 0; i < 16; i++)
+ us(8, picture_md5[c][i], 0x00, 0xff, 2, c, i);
+ } else if (current->hash_type == 1) {
+ us(16, picture_crc[c], 0x0000, 0xffff, 1, c);
+ } else if (current->hash_type == 2) {
+ us(32, picture_checksum[c], 0x00000000, 0xffffffff, 1, c);
+ }
+ }
+
+ return 0;
+}
+
+static int FUNC(sei_time_code)(CodedBitstreamContext *ctx, RWContext *rw,
+ H265RawSEITimeCode *current)
+{
+ int err, i;
+
+ HEADER("Time Code");
+
+ u(2, num_clock_ts, 1, 3);
+
+ for (i = 0; i < current->num_clock_ts; i++) {
+ flags(clock_timestamp_flag[i], 1, i);
+
+ if (current->clock_timestamp_flag[i]) {
+ flags(units_field_based_flag[i], 1, i);
+ us(5, counting_type[i], 0, 6, 1, i);
+ flags(full_timestamp_flag[i], 1, i);
+ flags(discontinuity_flag[i], 1, i);
+ flags(cnt_dropped_flag[i], 1, i);
+
+ ubs(9, n_frames[i], 1, i);
+
+ if (current->full_timestamp_flag[i]) {
+ us(6, seconds_value[i], 0, 59, 1, i);
+ us(6, minutes_value[i], 0, 59, 1, i);
+ us(5, hours_value[i], 0, 23, 1, i);
+ } else {
+ flags(seconds_flag[i], 1, i);
+ if (current->seconds_flag[i]) {
+ us(6, seconds_value[i], 0, 59, 1, i);
+ flags(minutes_flag[i], 1, i);
+ if (current->minutes_flag[i]) {
+ us(6, minutes_value[i], 0, 59, 1, i);
+ flags(hours_flag[i], 1, i);
+ if (current->hours_flag[i])
+ us(5, hours_value[i], 0, 23, 1, i);
+ }
+ }
+ }
+
+ ubs(5, time_offset_length[i], 1, i);
+ if (current->time_offset_length[i] > 0)
+ ibs(current->time_offset_length[i], time_offset_value[i], 1, i);
+ else
+ infer(time_offset_value[i], 0);
+ }
+ }
+
+ return 0;
+}
+