Index: source/FrameHeaderParser.cpp
===================================================================
--- source/FrameHeaderParser.cpp	(revision 28247)
+++ source/FrameHeaderParser.cpp	(working copy)
@@ -42,8 +42,6 @@
 #define countof(array) (sizeof(array)/sizeof(array[0]))
 #define DNew new
 
-#define MAX_SPS			256			// Max size for a SPS packet
-
 int CFrameHeaderParser::MakeAACInitData(BYTE* pData, int profile, int freq, int channels)
 {
 	int srate_idx;
@@ -1226,19 +1224,28 @@
 		{
 			//LogDebug("SPS found");
 
-			BYTE			SPSTemp[MAX_SPS];
-			BYTE			SPSBuff[MAX_SPS];
-			CGolombBuffer	gb (SPSBuff, MAX_SPS);
 			__int64			num_units_in_tick;
 			__int64			time_scale;
+			__int64			pos;
 			long			fixed_frame_rate_flag;
 
-			h.spspos = GetPos() - 5;
-			h.spslen = next_nal - h.spspos;
+			// Copy the full SPS packet in case the PPS is not found in the same packet,
+			// but make sure we don't change the current position in the buffer.
+			pos = GetPos() - 5;
+			if (h.spslen != 0)
+			{
+				free(h.sps);
+			}
+			h.spslen = next_nal - pos;
+			h.sps = (BYTE*) malloc(h.spslen - 4);
+			ByteRead(h.sps, h.spslen - 4);
+			Seek(pos + 5);
 
 			// Manage H264 escape codes (see "remove escapes (very rare 1:2^22)" in ffmpeg h264.c file)
-			ByteRead((BYTE*)SPSTemp, min(MAX_SPS, GetRemaining()));
-			RemoveMpegEscapeCode (SPSBuff, SPSTemp, MAX_SPS);
+			//ByteRead((BYTE*)SPSTemp, min(MAX_SPS, GetRemaining()));
+			BYTE* buff = (BYTE*) malloc(h.spslen - 4);
+			CGolombBuffer	gb (buff, h.spslen - 4);
+			RemoveMpegEscapeCode (buff, h.sps, h.spslen - 4);
 
 			h.profile = (BYTE)gb.BitRead(8);
 			gb.BitRead(8);
@@ -1367,12 +1374,19 @@
 				}
 			}
 
-			Seek(h.spspos+gb.GetPos());
+			Seek(pos + gb.GetPos());
+			free(buff);
 		}
 		else if(nal_type==0x8)
 		{
-			h.ppspos = GetPos() - 5;
-			h.ppslen = next_nal - h.ppspos;
+			__int64 pos = GetPos() - 5;
+			if (h.ppslen != 0)
+			{
+				free(h.pps);
+			}
+			h.ppslen = next_nal - pos;
+			h.pps = (BYTE*) malloc(h.ppslen - 4);
+			ByteRead(h.pps, h.ppslen - 4);
 		}
 
 		BitByteAlign();
@@ -1448,13 +1462,11 @@
 		BYTE* p = (BYTE*)&vi->dwSequenceHeader[0];
 		*p++ = (h.spslen-4) >> 8;
 		*p++ = (h.spslen-4) & 0xff;
-		Seek(h.spspos+4);
-		ByteRead(p, h.spslen-4);
+		memcpy(p, h.sps, h.spslen-4);
 		p += h.spslen-4;
 		*p++ = (h.ppslen-4) >> 8;
 		*p++ = (h.ppslen-4) & 0xff;
-		Seek(h.ppspos+4);
-		ByteRead(p, h.ppslen-4);
+		memcpy(p, h.pps, h.ppslen-4);
 		p += h.ppslen-4;
 		pmt->SetFormat((BYTE*)vi, len);
 	}
@@ -1638,9 +1650,7 @@
 	LogDebug("height: %i",h.height);
 	LogDebug("level: %i",h.level);
 	LogDebug("profile: %i",h.profile);
-	LogDebug("PPS pos: %i ",h.ppspos);
 	LogDebug("PPS len: %i",h.ppslen);
-	LogDebug("SPS pos: %i",h.spspos);
 	LogDebug("SPS len: %i",h.spslen);
 	LogDebug("=================================");
 }
Index: source/FrameHeaderParser.h
===================================================================
--- source/FrameHeaderParser.h	(revision 28247)
+++ source/FrameHeaderParser.h	(working copy)
@@ -305,23 +305,23 @@
 	{
 		BYTE profile, level;
 		unsigned int width, height;
-    bool progressive;
-		__int64 spspos, spslen;
-		__int64 ppspos, ppslen;
+		bool progressive;
+		BYTE * sps;
+		BYTE * pps;
+		__int64 spslen;
+		__int64 ppslen;
 		__int64 AvgTimePerFrame;
 		int arx, ary;
 		BYTE ar;
 		avchdr()
 		{
-      progressive = true;
-			spspos = 0;
+			progressive = true;
 			spslen = 0;
-			ppspos = 0;
 			ppslen = 0;
 			AvgTimePerFrame = 0;
-      ar = 0;
-      arx = 0;
-      ary = 0;
+			ar = 0;
+			arx = 0;
+			ary = 0;
 		}
 	};
 
Index: source/MpegPesParser.cpp
===================================================================
--- source/MpegPesParser.cpp	(revision 28247)
+++ source/MpegPesParser.cpp	(working copy)
@@ -76,7 +76,6 @@
 	}
 	else 
 	{
-		avchdr avc;
 		if (hdrParser.Read(avc,framesize,&pmt))
 		{
 			//hdrParser.DumpAvcHeader(avc);
Index: source/MpegPesParser.h
===================================================================
--- source/MpegPesParser.h	(revision 28247)
+++ source/MpegPesParser.h	(working copy)
@@ -40,6 +40,7 @@
 {
 private:
 	CFrameHeaderParser hdrParser;
+	avchdr avc;
 	bool ParseVideo(byte* tsPacket,bool isMpeg2);
 
 public:
