#! /bin/sh /usr/share/dpatch/dpatch-run
## 20_write_header_fix.dpatch by  <eriks@debian.org>
##
## All lines beginning with `## DP:' are a description of the patch.
## DP: There was a problem, if there is not PACK header directly before
## DP: first video. Now there is the correct check for it, before we use
## DP: PACK data. 

@DPATCH@
diff -urNad mpgtx-1.3.1~/mpegOut.cxx mpgtx-1.3.1/mpegOut.cxx
--- mpgtx-1.3.1~/mpegOut.cxx	2005-04-07 18:49:09.000000000 +0200
+++ mpgtx-1.3.1/mpegOut.cxx	2007-03-04 12:21:45.000000000 +0100
@@ -97,12 +97,19 @@
 	// correct the packet length before outputing it (stdout is not
 	// seekable:/
 	off_t PACKlength = 0;
-        mpeg2stuff_bytes = 4;
-	if ((Mpeg->System->first_video_packet[4] & 0xF0) == 0x20) {
-		//standard mpeg1 pack
-		PACKlength = 12;
-	} else {
-		if ((Mpeg->System->first_video_packet[4] & 0xC0) == 0x40) {
+	mpeg2stuff_bytes = 4;
+
+	if ( Mpeg->System->first_video_packet[0] == 0x00 &&
+		Mpeg->System->first_video_packet[1] == 0x00 &&
+		Mpeg->System->first_video_packet[2] == 0x01 &&
+		Mpeg->System->first_video_packet[3] == 0xBA )
+	{
+		/* we have a PACK in this buffer */
+		if ((Mpeg->System->first_video_packet[4] & 0xF0) == 0x20) {
+			//standard mpeg1 pack
+			PACKlength = 12;
+		}
+		else if ((Mpeg->System->first_video_packet[4] & 0xC0) == 0x40) {
 			//new mpeg2 pack : 14 bytes + what stuffing ?
 			mpeg2stuff_bytes = Mpeg->System->first_video_packet[13] & 0x07;
 			PACKlength = 14;
@@ -116,7 +123,13 @@
 			}		
 			fprintf(stderr,"\n-------\n");
 #endif
-		} else {
+		}
+		else
+		{
+			fprintf(stderr, "We found a system packet, but it is not MPEG1 or MPEG2. Bye\n");
+			exit(1);
+		}
+	} else {
 			//wazup?
 			// May 28 2001: maybe there's no pack at all...
 			byte packetcode = Mpeg->System->first_video_packet[3] & 0xF0;
@@ -160,7 +173,7 @@
 			}
 			// end May 28
 		}
-	}
+
 	fwrite(Mpeg->System->first_video_packet,
 			1, PACKlength + mpeg2stuff_bytes, MpegOut);
 	// now allocate required mem
