From 4eca7534e722c25861890beca361f6be0aeddbe9 Mon Sep 17 00:00:00 2001
From: CaiYuHan <240947104@qq.com>
Date: Wed, 15 Sep 2021 10:18:56 +0800
Subject: [PATCH] libavcodec/h264_ps.c:Add SVC decoding function based on
 Temporal scalability for H.264/AVC

Signed-off-by: CaiYuHan <240947104@qq.com>
---
 libavcodec/h264_ps.c | 60 +++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 59 insertions(+), 1 deletion(-)
 mode change 100644 => 100755 libavcodec/h264_ps.c

diff --git a/libavcodec/h264_ps.c b/libavcodec/h264_ps.c
old mode 100644
new mode 100755
index e21c2b5..2c11162
--- a/libavcodec/h264_ps.c
+++ b/libavcodec/h264_ps.c
@@ -362,7 +362,7 @@ int ff_h264_decode_seq_parameter_set(GetBitContext *gb, AVCodecContext *avctx,
     skip_bits(gb, 2);                             // reserved_zero_2bits
     level_idc = get_bits(gb, 8);
     sps_id    = get_ue_golomb_31(gb);
-
+	
     if (sps_id >= MAX_SPS_COUNT) {
         av_log(avctx, AV_LOG_ERROR, "sps_id %u out of range\n", sps_id);
         goto fail;
@@ -627,6 +627,12 @@ int ff_h264_decode_seq_parameter_set(GetBitContext *gb, AVCodecContext *avctx,
     /* check if this is a repeat of an already parsed SPS, then keep the
      * original one.
      * otherwise drop all PPSes that depend on it */
+/**
+ *Add SVC decoding function based on Temporal scalability for H.264/AVC
+*/
+#if SVC_DEC_H264
+	ps->sps_id = sps_id;
+#endif
     if (ps->sps_list[sps_id] &&
         !memcmp(ps->sps_list[sps_id]->data, sps_buf->data, sps_buf->size)) {
         av_buffer_unref(&sps_buf);
@@ -641,6 +647,58 @@ fail:
     av_buffer_unref(&sps_buf);
     return AVERROR_INVALIDDATA;
 }
+/**
+ *Add SVC decoding function based on Temporal scalability for H.264/AVC
+*/
+#if SVC_DEC_H264
+/** G.7.3.2.1.4 */
+void ff_h264_decode_sps_svc_ext(GetBitContext *gb, SPS *sps) {
+	int chromaArrayType = sps->chroma_format_idc;
+	sps->inter_layer_deblocking_filter_control_present_flag = get_bits1(gb);
+	sps->extended_spatial_scalability_idc = get_bits(gb, 2);
+	if (chromaArrayType == 1 || chromaArrayType == 2) {
+		sps->chroma_phase_x_plus1_flag = get_bits1(gb);
+	}
+	if (chromaArrayType == 1) {
+		sps->chroma_phase_y_plus1 = get_bits(gb, 2);
+	}
+	if (sps->extended_spatial_scalability_idc == 1) {
+		if (chromaArrayType > 0) {
+			sps->seq_ref_layer_chroma_phase_x_plus1_flag = get_bits1(gb);
+			sps->seq_ref_layer_chroma_phase_y_plus1 = get_bits(gb, 2);
+		}
+		sps->seq_scaled_ref_layer_left_offset = get_se_golomb(gb);
+		sps->seq_scaled_ref_layer_top_offset = get_se_golomb(gb);
+		sps->seq_scaled_ref_layer_right_offset = get_se_golomb(gb);
+		sps->seq_scaled_ref_layer_bottom_offset = get_se_golomb(gb);
+	}
+	sps->seq_tcoeff_level_prediction_flag = get_bits1(gb);
+	if (sps->seq_tcoeff_level_prediction_flag) {
+		sps->adaptive_tcoeff_level_prediction_flag = get_bits1(gb);
+	}
+	sps->slice_header_restriction_flag = get_bits1(gb);
+}
+
+//7.3.2.1.3
+int ff_h264_decode_subset_seq_parameter_set(GetBitContext *gb, AVCodecContext *avctx,
+                                     H264ParamSets *ps, int ignore_truncation)
+{
+	SPS *sps;
+	int ret;
+	ret = ff_h264_decode_seq_parameter_set(gb, avctx, ps, ignore_truncation);
+	if(ret < 0)
+		return -1;
+	
+	sps = (SPS*)ps->sps_list[ps->sps_id]->data;
+	
+	if (sps->profile_idc == 83 || sps->profile_idc == 86) {
+		ff_h264_decode_sps_svc_ext(gb, sps); /* specified in Annex G */
+		sps->svc_vui_parameters_present_flag = get_bits1(gb);
+	} 
+	
+	return 0;
+}
+#endif
 
 static void init_dequant8_coeff_table(PPS *pps, const SPS *sps)
 {
-- 
2.29.2

