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

Signed-off-by: CaiYuHan <240947104@qq.com>
---
 libavcodec/h264_parser.c | 62 +++++++++++++++++++++++++++++++++++++---
 1 file changed, 58 insertions(+), 4 deletions(-)
 mode change 100644 => 100755 libavcodec/h264_parser.c

diff --git a/libavcodec/h264_parser.c b/libavcodec/h264_parser.c
old mode 100644
new mode 100755
index 01ea016..c979133
--- a/libavcodec/h264_parser.c
+++ b/libavcodec/h264_parser.c
@@ -109,14 +109,34 @@ static int h264_find_frame_end(H264ParseContext *p, const uint8_t *buf,
                 state >>= 1;           // 2->1, 1->0, 0->0
         } else if (state <= 5) {
             int nalu_type = buf[i] & 0x1F;
+/**
+ *Add SVC decoding function based on Temporal scalability for H.264/AVC
+*/
+#if SVC_DEC_H264
+			if (nalu_type == H264_NAL_SEI || nalu_type == H264_NAL_SPS ||
+                nalu_type == H264_NAL_PPS || nalu_type == H264_NAL_AUD ||
+                nalu_type == H264_NAL_SUB_SPS)
+#else
             if (nalu_type == H264_NAL_SEI || nalu_type == H264_NAL_SPS ||
-                nalu_type == H264_NAL_PPS || nalu_type == H264_NAL_AUD) {
+                nalu_type == H264_NAL_PPS || nalu_type == H264_NAL_AUD)
+#endif	
+			{
                 if (pc->frame_start_found) {
                     i++;
                     goto found;
                 }
-            } else if (nalu_type == H264_NAL_SLICE || nalu_type == H264_NAL_DPA ||
-                       nalu_type == H264_NAL_IDR_SLICE) {
+            }
+/**
+ *Add SVC decoding function based on Temporal scalability for H.264/AVC
+*/
+#if SVC_DEC_H264
+			else if (nalu_type == H264_NAL_SLICE || nalu_type == H264_NAL_DPA ||
+                       nalu_type == H264_NAL_IDR_SLICE || nalu_type == H264_NAL_EXTEN_SLICE)
+#else
+			else if (nalu_type == H264_NAL_SLICE || nalu_type == H264_NAL_DPA ||
+                       nalu_type == H264_NAL_IDR_SLICE)
+#endif
+			{
                 state += 8;
                 continue;
             }
@@ -256,6 +276,12 @@ static inline int parse_nal_units(AVCodecParserContext *s,
     int q264 = buf_size >=4 && !memcmp("Q264", buf, 4);
     int field_poc[2];
     int ret;
+/**
+ *Add SVC decoding function based on Temporal scalability for H.264/AVC
+*/
+#if SVC_DEC_H264
+	int idr_svc;
+#endif
 
     /* set some sane default values */
     s->pict_type         = AV_PICTURE_TYPE_I;
@@ -321,7 +347,35 @@ static inline int parse_nal_units(AVCodecParserContext *s,
         get_bits1(&nal.gb);
         nal.ref_idc = get_bits(&nal.gb, 2);
         nal.type    = get_bits(&nal.gb, 5);
-
+/**
+ *Add SVC decoding function based on Temporal scalability for H.264/AVC
+*/		
+#if SVC_DEC_H264
+			if(nal.type == H264_NAL_EXTEN_SLICE || nal.type == H264_NAL_DEPTH_EXTEN_SLICE)
+			{
+				nal.svc_ext_flag = get_bits1(&nal.gb); //svc_extension_flag
+				
+				if(nal.svc_ext_flag)
+					idr_svc = get_bits1(&nal.gb); //idr_flag for svc
+				else
+					idr_svc = get_bits1(&nal.gb); //non_idr_flag for mvc
+				if( (nal.type == H264_NAL_EXTEN_SLICE && idr_svc == 1) ||
+					(nal.type == H264_NAL_DEPTH_EXTEN_SLICE && idr_svc == 0))
+				{
+					nal.type = H264_NAL_IDR_SLICE;
+				}
+				else
+				{
+					nal.type = H264_NAL_SLICE;
+				}
+				skip_bits(&nal.gb, 22); //left 22 bits are skip
+			}
+			else if(nal.type == H264_NAL_SUB_SPS)
+			{
+				nal.type = H264_NAL_SPS; //some of vui not necessary for decode, so skip
+			}
+#endif
+		
         switch (nal.type) {
         case H264_NAL_SPS:
             ff_h264_decode_seq_parameter_set(&nal.gb, avctx, &p->ps, 0);
-- 
2.29.2

