From 0658496d5fc7d238f41e10ea4d0a10113a8eed84 Mon Sep 17 00:00:00 2001
From: ireader <tao3@outlook.com>
Date: Sat, 16 Nov 2024 13:17:12 +0800
Subject: [PATCH] fix: #361 read mpeg-ts AF origin PCR extension value

---
 libflv/source/flv-muxer.c           |  8 ++++----
 libmpeg/source/mpeg-system-header.c |  3 ++-
 libmpeg/source/mpeg-ts-dec.c        |  2 +-
 test/test.cpp                       | 20 ++++++++++----------
 test/test.vcxproj.filters           |  3 +++
 5 files changed, 20 insertions(+), 16 deletions(-)

diff --git a/libflv/source/flv-muxer.c b/libflv/source/flv-muxer.c
index 7fc0d4ee..5cbd007b 100644
--- a/libflv/source/flv-muxer.c
+++ b/libflv/source/flv-muxer.c
@@ -388,7 +388,7 @@ static int flv_muxer_h265(struct flv_muxer_t* flv, uint32_t pts, uint32_t dts)
 		video.keyframe = 1 == flv->vcl ? FLV_VIDEO_KEY_FRAME : FLV_VIDEO_INTER_FRAME;
 		video.avpacket = FLV_AVPACKET;
 		n = flv_video_tag_header_write(&video, flv->ptr, flv->capacity);
-		assert(n == 5 + (flv->enhanced_rtmp && dts == pts) ? 0 : 3);
+		assert(n == 5 + ((flv->enhanced_rtmp && dts != pts) ? 3 : 0));
 		return flv->handler(flv->param, FLV_TYPE_VIDEO, flv->ptr, flv->bytes, dts);
 	}
 	return 0;
@@ -402,7 +402,7 @@ int flv_muxer_hevc(struct flv_muxer_t* flv, const void* data, size_t bytes, uint
 			return -ENOMEM;
 	}
 
-	flv->bytes = 5 + (flv->enhanced_rtmp && dts == pts) ? 0 : 3;
+	flv->bytes = 5 + ((flv->enhanced_rtmp && dts != pts) ? 3 : 0);
 	flv->bytes += h265_annexbtomp4(&flv->v.hevc, data, bytes, flv->ptr + flv->bytes, flv->capacity - flv->bytes, &flv->vcl, &flv->update);
 	if (flv->bytes <= 5)
 		return -ENOMEM;
@@ -441,7 +441,7 @@ static int flv_muxer_h266(struct flv_muxer_t* flv, uint32_t pts, uint32_t dts)
 		video.keyframe = 1 == flv->vcl ? FLV_VIDEO_KEY_FRAME : FLV_VIDEO_INTER_FRAME;
 		video.avpacket = FLV_AVPACKET;
 		n = flv_video_tag_header_write(&video, flv->ptr, flv->capacity);
-		assert(n == 5 + (flv->enhanced_rtmp && dts == pts) ? 0 : 3);
+		assert(n == 5 + ((flv->enhanced_rtmp && dts != pts) ? 3 : 0));
 		return flv->handler(flv->param, FLV_TYPE_VIDEO, flv->ptr, flv->bytes, dts);
 	}
 	return 0;
@@ -455,7 +455,7 @@ int flv_muxer_vvc(struct flv_muxer_t* flv, const void* data, size_t bytes, uint3
 			return -ENOMEM;
 	}
 
-	flv->bytes = 5 + (flv->enhanced_rtmp && dts == pts) ? 0 : 3;
+	flv->bytes = 5 + ((flv->enhanced_rtmp && dts != pts) ? 3 : 0);
 	flv->bytes += h266_annexbtomp4(&flv->v.vvc, data, bytes, flv->ptr + flv->bytes, flv->capacity - flv->bytes, &flv->vcl, &flv->update);
 	if (flv->bytes <= 5)
 		return -ENOMEM;
diff --git a/libmpeg/source/mpeg-system-header.c b/libmpeg/source/mpeg-system-header.c
index e7d5d6e8..47a7deaa 100644
--- a/libmpeg/source/mpeg-system-header.c
+++ b/libmpeg/source/mpeg-system-header.c
@@ -56,7 +56,8 @@ int system_header_read(struct ps_system_header_t* h, struct mpeg_bits_t* reader)
     }
 
     assert(0 == mpeg_bits_error(reader));
-    //assert(end == mpeg_bits_tell(reader));
+    //assert(end >= mpeg_bits_tell(reader));
+    mpeg_bits_seek(reader, end); // fix: read stream out of range
     return MPEG_ERROR_OK;
 }
 
diff --git a/libmpeg/source/mpeg-ts-dec.c b/libmpeg/source/mpeg-ts-dec.c
index c0e8637c..7f0ff847 100644
--- a/libmpeg/source/mpeg-ts-dec.c
+++ b/libmpeg/source/mpeg-ts-dec.c
@@ -57,7 +57,7 @@ static uint32_t adaptation_filed_read(struct ts_adaptation_field_t *adp, const u
 		if(adp->OPCR_flag && i + 6 <= adp->adaptation_field_length + 1)
 		{
 			adp->original_program_clock_reference_base = (((uint64_t)data[i]) << 25) | ((uint64_t)data[i+1] << 17) | ((uint64_t)data[i+2] << 9) | ((uint64_t)data[i+3] << 1) | ((data[i+4] >> 7) & 0x01);
-			adp->original_program_clock_reference_extension = ((data[i+4] & 0x01) << 1) | data[i+5];
+			adp->original_program_clock_reference_extension = ((data[i+4] & 0x01) << 8) | data[i+5];
 
 			i += 6;
 		}
diff --git a/test/test.cpp b/test/test.cpp
index aa5a4bc8..fe1717ee 100755
--- a/test/test.cpp
+++ b/test/test.cpp
@@ -51,7 +51,7 @@ void usage(int argc, char const *argv[]){
 /* 用于套壳调用函数,参数 char* */
 #define DEF_FUN_PCHAR(name, ...)  void name ( __VA_ARGS__ ); \
     int t_##name(int argc, char const *argv[]){\
-        if(4 != argc) return -1;\
+        if(4 > argc) return -1;\
         name(argv[3]);return 0;\
     } \
     RE_REGISTER(name, "void " #name "(" #__VA_ARGS__ ")", t_##name)
@@ -59,14 +59,14 @@ void usage(int argc, char const *argv[]){
 /* 用于套壳调用函数,参数 char*, char* */
 #define DEF_FUN_2PCHAR(name, ...)  void name ( __VA_ARGS__ ); \
     int t_##name(int argc, char const *argv[]){\
-        if(5 != argc) return -1;\
+        if(5 > argc) return -1;\
         name(argv[3], argv[4]);return 0;\
     } \
     RE_REGISTER(name, "void " #name "(" #__VA_ARGS__ ")", t_##name)
 
 #define DEF_FUN_3PCHAR(name, ...)  void name ( __VA_ARGS__ ); \
     int t_##name(int argc, char const *argv[]){\
-        if(6 != argc) return -1;\
+        if(6 > argc) return -1;\
         name(argv[3], argv[4], argv[5]);return 0;\
     } \
     RE_REGISTER(name, "void " #name "(" #__VA_ARGS__ ")", t_##name)
@@ -74,7 +74,7 @@ void usage(int argc, char const *argv[]){
 /* 用于套壳调用函数,参数 char*, char*, char*, char* */
 #define DEF_FUN_4PCHAR(name, ...)  void name ( __VA_ARGS__ ); \
     int t_##name(int argc, char const *argv[]){\
-        if(7 != argc) return -1;\
+        if(7 > argc) return -1;\
         name(argv[3], argv[4], argv[5], argv[6]);return 0;\
     } \
     RE_REGISTER(name, "void " #name  "(" #__VA_ARGS__ ")", t_##name)
@@ -82,7 +82,7 @@ void usage(int argc, char const *argv[]){
 /* 用于套壳调用函数,参数 char*, int */
 #define DEF_FUN_PCHAR_INT(name, ...)  void name ( __VA_ARGS__ ); \
     int t_##name(int argc, char const *argv[]){\
-        if(5 != argc) return -1;\
+        if(5 > argc) return -1;\
         name(argv[3], (int)atoi(argv[4]));return 0;\
     } \
     RE_REGISTER(name, "void " #name "(" #__VA_ARGS__ ")", t_##name)
@@ -90,7 +90,7 @@ void usage(int argc, char const *argv[]){
 /* 用于套壳调用函数,参数 int, int, char*, char* */
 #define DEF_FUN_INT_INT_PCHAR_PCHAR(name, ...)  void name ( __VA_ARGS__ ); \
     int t_##name(int argc, char const *argv[]){\
-        if(7 != argc) return -1;\
+        if(7 > argc) return -1;\
         name((int)atoi(argv[3]), (int)atoi(argv[4]), argv[5], argv[6]);return 0;\
     }\
      RE_REGISTER(name, "void " #name "(" #__VA_ARGS__ ")", t_##name)
@@ -98,7 +98,7 @@ void usage(int argc, char const *argv[]){
 /* 用于套壳调用函数,参数 char*, int, int, char* */
 #define DEF_FUN_PCHAR_INT_INT_PCHAR(name, ...)  void name ( __VA_ARGS__ ); \
     int t_##name(int argc, char const *argv[]){\
-        if(7 != argc) return -1;\
+        if(7 > argc) return -1;\
         name(argv[3], (int)atoi(argv[4]), (int)atoi(argv[5]), argv[6]);return 0;\
     } \
     RE_REGISTER(name, "void " #name "(" # __VA_ARGS__ ")", t_##name)
@@ -106,7 +106,7 @@ void usage(int argc, char const *argv[]){
 /* 用于套壳调用函数,参数 char*, int, char* */
 #define DEF_FUN_PCHAR_INT_PCHAR(name, ...)  void name ( __VA_ARGS__ ); \
     int t_##name(int argc, char const *argv[]){\
-        if(6 != argc) return -1;\
+        if(6 > argc) return -1;\
         name(argv[3], (int)atoi(argv[4]), argv[5]);return 0;\
     } \
     RE_REGISTER(name, "void " #name "(" # __VA_ARGS__ ")", t_##name)
@@ -114,7 +114,7 @@ void usage(int argc, char const *argv[]){
 /* 用于套壳调用函数,参数 char*, int, char*,int, int */
 #define DEF_FUN_PCHAR_INT_PCHAR_INT_INT(name, ...)  void name ( __VA_ARGS__ ); \
     int t_##name(int argc, char const *argv[]){\
-        if(8 != argc) return -1;\
+        if(8 > argc) return -1;\
         name(argv[3], (int)atoi(argv[4]), argv[5], (int)atoi(argv[6]), (int)atoi(argv[7]));return 0;\
     } \
     RE_REGISTER(name, "void " #name "(" # __VA_ARGS__ ")", t_##name)
@@ -122,7 +122,7 @@ void usage(int argc, char const *argv[]){
 /* 用于套壳调用函数,参数 int, const char*, uint16_t, uint32_t, const char* */
 #define DEF_FUN_INT_PCHAR_INT_INT_PCHAR(name, ...)  void name ( __VA_ARGS__ ); \
     int t_##name(int argc, char const *argv[]){\
-        if(8 != argc) return -1;\
+        if(8 > argc) return -1;\
         name((int)atoi(argv[3]), argv[4], (uint16_t)atoi(argv[5]), (uint32_t)atoi(argv[6]), argv[7]);return 0;\
     } \
     RE_REGISTER(name, "void " #name "(" # __VA_ARGS__ ")", t_##name)
diff --git a/test/test.vcxproj.filters b/test/test.vcxproj.filters
index f3a547ee..63c60aab 100644
--- a/test/test.vcxproj.filters
+++ b/test/test.vcxproj.filters
@@ -524,6 +524,9 @@
     <ClCompile Include="..\libmpeg\test\mov-2-mpeg-ps-test.cpp">
       <Filter>libmpeg</Filter>
     </ClCompile>
+    <ClCompile Include="..\librtsp\source\sdp\sdp-h266.c">
+      <Filter>librtsp\sdp</Filter>
+    </ClCompile>
     <ClCompile Include="..\librtsp\test\media\ps-file-reader.cpp">
       <Filter>librtsp\media</Filter>
     </ClCompile>