Mailing List archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[vdr] M2VRequantizer main.c patch



-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

i have experimented with it on some DVB-S video ES streams
and have learned that assertions and a floating point exception
happens sometimes on maybe a bit corrupted streams.

well, such corruption may happen on dvb-s stream sometime,
'cause of non 100% reception or/and error correction.

anyway, i have checked out the "source" of assertion abortions ;-),
and made some patches ...

maybe somebody else can make a better approach for a bugfix, of course,
but after all - this little patch makes it working at least ;-)

it works either with some optimizations "-O3 .." using gcc 3.3.1
on p4 and athlon ..

cheers, sven
- -- 
health & wealth
mailto:sgoethel@jausoft.com
www   : http://www.jausoft.com ; pgp: http://www.jausoft.com/gpg/
voice : +49-5121-999600 ; fax : +49-5121-999602
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)

iD8DBQE/dX8nHdOA30NoFAARAtgRAJ9ctheBj6XNFJAfAGWZAxCoD5KLJACgsRfi
EdsrTIlpw/H9Z2+g0AccOew=
=bphu
-----END PGP SIGNATURE-----
--- orig/main.c	2003-09-14 22:43:34.000000000 +0200
+++ src/main.c	2003-09-27 09:02:42.000000000 +0200
@@ -4,14 +4,14 @@
 
 // toggles:
 // #define STAT // print stats on exit
-#define NDEBUG // turns off asserts
+// #define NDEBUG // turns off asserts
 #define REMOVE_BYTE_STUFFING	// removes 0x 00 00 00 00 00 00 used in cbr streams (look for 6 0x00 and remove 1 0x00)
 								/*	4 0x00 might be legit, for exemple:
 									00 00 01 b5 14 82 00 01 00 00 00 00 01 b8 .. ..
 												 these two: -- -- are part of the seq. header ext.
 									AFAIK 5 0x00 should never happen except for byte stuffing but to be safe look for 6 */
 // #define USE_FD // use 2 lasts args for input/output paths
-#define DEMO // demo mode
+// #define DEMO // demo mode
 
 #define REACT_DELAY (1024.0*128.0)
 
@@ -322,6 +322,28 @@
 	{
 		//uint val = Show_Bits(i);
 		//putbits(val, i);
+		if ( ! (((unsigned int)inbitbuf) >> (32 - i) == 0) )
+		{
+			unsigned int k,l;
+
+#ifndef NDEBUG
+			k = (unsigned int)inbitbuf;
+			l = k >> (32 - i);
+
+			fprintf(stderr, "%s:%d illegal inbitbuf: %d, %d, %d, %d\n", 
+					__FILE__, __LINE__, 
+					k, l, i, 32-i);
+#endif
+			k = (1 << (32 - i))-1;
+			l = k >> (32 - i);
+
+#ifndef NDEBUG
+			fprintf(stderr, "%s:%d guessing inbitbuf: %d, %d, %d, %d\n", 
+					__FILE__, __LINE__, 
+					k, l, i, 32-i);
+#endif
+			inbitbuf = k;
+		}
 		assert(((unsigned int)inbitbuf) >> (32 - i) == 0);
 		inbitbuf <<= i;
 		inbitcnt -= i;
@@ -396,7 +418,20 @@
 #endif
 	if (q_scale_type)
 	{
-		assert(quant >= 1 && quant <= 112);
+		// assert(quant >= 1 && quant <= 112);
+
+		if(! (quant >= 1 && quant <= 112))
+		{
+#ifndef NDEBUG
+			fprintf(stderr, "%s:%d illegal quant: %d\n", __FILE__, __LINE__, quant);
+#endif
+			if(quant>122) quant=122;
+			else if(quant<1) quant=1;
+#ifndef NDEBUG
+			fprintf(stderr, "%s:%d illegal quant set to min/max: %d\n", __FILE__, __LINE__, quant);
+#endif
+		}
+
 		quant = map_non_linear_mquant[quant] + 1;
 		if (quant_corr < -60.0f) quant++;
 		if (quant > 31) quant = 31;
@@ -462,14 +497,40 @@
 
 #include "putvlc.h"
 
-void putAC(int run, int signed_level, int vlcformat)
+/**
+ * returns:
+ * 	0: ERROR
+ * 	1: OK
+ */
+int putAC(int run, int signed_level, int vlcformat)
 {
 	int level, len;
 	const VLCtable *ptab = NULL;
 	
 	level = (signed_level<0) ? -signed_level : signed_level; /* abs(signed_level) */
 	
-	assert(!(run<0 || run>63 || level==0 || level>2047));
+	// assert(!(run<0 || run>63 || level==0 || level>2047));
+
+	if(! (0<=run && run<=63))
+	{
+#ifndef NDEBUG
+		fprintf(stderr, "%s:%d illegal run: %d\n", __FILE__, __LINE__, run);
+#endif
+		return 0;
+	}
+
+	if(! (level!=0 && level<=2047))
+	{
+#ifndef NDEBUG
+		fprintf(stderr, "%s:%d illegal level: %d\n", __FILE__, __LINE__, level);
+#endif
+		return 0;
+	}
+	
+/*
+	assert(0<=run && run<=63);
+	assert(level!=0 && level<=2047);
+*/
 	
 	len = 0;
 	
@@ -497,25 +558,32 @@
 		putbits(run, 6); /* 6 bit code for run */
 		putbits(((uint)signed_level) & 0xFFF, 12);
 	}
+	
+	return 1;
 }
 
 
-static inline void putACfirst(int run, int val)
+/**
+ * returns:
+ * 	0: ERROR
+ * 	1: OK
+ */
+static inline int putACfirst(int run, int val)
 {
-	if (run==0 && (val==1 || val==-1)) putbits(2|(val<0),2);
-	else putAC(run,val,0);
+	if (run==0 && (val==1 || val==-1)) { putbits(2|(val<0),2); return 1; }
+	else return putAC(run,val,0);
 }
 
 void putnonintrablk(RunLevel *blk)
 {
 	assert(blk->level);
 	
-	putACfirst(blk->run, blk->level);
+	if ( ! putACfirst(blk->run, blk->level) ) return;
 	blk++;
 	
 	while(blk->level)
 	{
-		putAC(blk->run, blk->level, 0);
+		if ( ! putAC(blk->run, blk->level, 0) ) return;
 		blk++;
 	}
 	
@@ -645,12 +713,22 @@
 static inline int get_quantizer_scale ()
 {
     int quantizer_scale_code;
+    int _quantizer_scale;
 
     quantizer_scale_code = UBITS (bit_buf, 5);
 	DUMPBITS (bit_buf, bits, 5); 
 	
-	if (q_scale_type) return non_linear_quantizer_scale[quantizer_scale_code];
-    else return quantizer_scale_code << 1;
+	if (q_scale_type) _quantizer_scale = non_linear_quantizer_scale[quantizer_scale_code];
+    else _quantizer_scale = quantizer_scale_code << 1;
+
+    if ( _quantizer_scale == 0 )
+    {
+#ifndef NDEBUG
+	fprintf(stderr, "%s:%d quant scale is ZERO: %d\n", __FILE__, __LINE__, _quantizer_scale);
+#endif
+	_quantizer_scale = 1;
+    }
+    return _quantizer_scale;
 }
 
 static inline int get_motion_delta (const int f_code)
@@ -829,7 +907,7 @@
 			if (val >= tst)
 			{
 				val = (val ^ SBITS (bit_buf, 1)) - SBITS (bit_buf, 1);
-				putAC(i - li - 1, (val * q) / nq, 0);
+				if ( ! putAC(i - li - 1, (val * q) / nq, 0) ) break;
 				li = i;
 			}
 	
@@ -852,7 +930,7 @@
 			val = SBITS (bit_buf, 12);
 			if (abs(val) >= tst)
 			{
-				putAC(i - li - 1, (val * q) / nq, 0);
+				if ( ! putAC(i - li - 1, (val * q) / nq, 0) ) break;
 				li = i;
 			}
 	
@@ -917,7 +995,7 @@
 				if (val >= tst)
 				{
 					val = (val ^ SBITS (bit_buf, 1)) - SBITS (bit_buf, 1);
-					putAC(i - li - 1, (val * q) / nq, 1);
+					if ( ! putAC(i - li - 1, (val * q) / nq, 1) ) break;
 					li = i;
 				}
 		
@@ -935,7 +1013,7 @@
 				val = SBITS (bit_buf, 12);
 				if (abs(val) >= tst)
 				{
-					putAC(i - li - 1, (val * q) / nq, 1);
+					if ( ! putAC(i - li - 1, (val * q) / nq, 1) ) break;
 					li = i;
 				}
 		

Attachment: config-build-linux-x86-debug
Description: application/shellscript

Attachment: config-build-linux-x86-p4
Description: application/shellscript


Home | Main Index | Thread Index