/[pcsx2_0.9.7]/trunk/pcsx2/IPU/mpeg2lib/Mpeg.cpp
ViewVC logotype

Diff of /trunk/pcsx2/IPU/mpeg2lib/Mpeg.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

--- trunk/pcsx2/IPU/mpeg2lib/Mpeg.cpp	2010/09/07 11:08:22	62
+++ trunk/pcsx2/IPU/mpeg2lib/Mpeg.cpp	2010/09/20 05:35:51	191
@@ -47,10 +47,14 @@
 	into 1st slot is copied to the 2nd slot. Which will later be copied
 	back to the 1st slot when 128bits have been read.
 */
-extern void ReorderBitstream();
 const DCTtab * tab;
 int mbaCount = 0;
 
+int bitstream_init ()
+{
+	return g_BP.FillBuffer(32);
+}
+
 int get_macroblock_modes()
 {
 	int macroblock_modes;
@@ -221,9 +225,7 @@
 
 int __fi get_dmv()
 {
-	const DMVtab * tab;
-
-	tab = DMV_2 + UBITS(2);
+	const DMVtab* tab = DMV_2 + UBITS(2);
 	DUMPBITS(tab->len);
 	return tab->dmv;
 }
@@ -239,22 +241,21 @@
 	else if (code >= 768)
 		mba = MBA.mba11 + (UBITS(11) - 24);
 	else switch (UBITS(11))
-		{
+	{
+		case 8:		/* macroblock_escape */
+			DUMPBITS(11);
+			return 0x23;
 
-			case 8:		/* macroblock_escape */
+		case 15:	/* macroblock_stuffing (MPEG1 only) */
+			if (decoder.mpeg1)
+			{
 				DUMPBITS(11);
-				return 0x23;
-
-			case 15:	/* macroblock_stuffing (MPEG1 only) */
-				if (decoder.mpeg1)
-				{
-					DUMPBITS(11);
-					return 0x22;
-				}
+				return 0x22;
+			}
 
-			default:
-				return 0;//error
-		}
+		default:
+			return 0;//error
+	}
 
 	DUMPBITS(mba->len);
 
@@ -336,11 +337,8 @@
 	val = (((s32)val) >> 31) ^ 2047;			\
 } while (0)
 
-static __fi bool get_intra_block()
+static bool get_intra_block()
 {
-	int i;
-	int j;
-	int val;
 	const u8 * scan = decoder.scantype ? mpeg2_scan.alt : mpeg2_scan.norm;
 	const u8 (&quant_matrix)[64] = decoder.iq;
 	int quantizer_scale = decoder.quantizer_scale;
@@ -348,7 +346,7 @@
 	u16 code; 
 
 	/* decode AC coefficients */
-  for (i=1 + ipu_cmd.pos[4]; ; i++)
+  for (int i=1 + ipu_cmd.pos[4]; ; i++)
   {
 	  switch (ipu_cmd.pos[5])
 	  {
@@ -427,60 +425,65 @@
 			return true;
 		}
 		
-		i+= tab->run == 65 ? GETBITS(6) : tab->run;
+		i += (tab->run == 65) ? GETBITS(6) : tab->run;
 		if (i >= 64)
 		{
 			ipu_cmd.pos[4] = 0;
 			return true;
 		}
+
 	  case 1:
-		if (!GETWORD())
-		{
-		  ipu_cmd.pos[4] = i - 1;
-		  ipu_cmd.pos[5] = 1;
-		  return false;
-		}
+	  {
+			if (!GETWORD())
+			{
+				ipu_cmd.pos[4] = i - 1;
+				ipu_cmd.pos[5] = 1;
+				return false;
+			}
 
-		j = scan[i];
+			uint j = scan[i];
+			int val;
 
-		if (tab->run==65) /* escape */
-		{
-		  if(!decoder.mpeg1)
-		  {
-			  val = (SBITS(12) * quantizer_scale * quant_matrix[i]) >> 4;
-			  DUMPBITS(12);
-		  }
-		  else
-		  {
-			  val = SBITS(8);
-			  DUMPBITS(8);
-
-			  if (!(val & 0x7f))
-			  {
-				val = GETBITS(8) + 2 * val;
-			  }
-			
-			  val = (val * quantizer_scale * quant_matrix[i]) >> 4;
-			  val = (val + ~ (((s32)val) >> 31)) | 1;
-		  }
-		}
-		else
-		{
-		  val = (tab->level * quantizer_scale * quant_matrix[i]) >> 4;
-		  if(decoder.mpeg1)
-		  {
-			/* oddification */
-			val = (val - 1) | 1;
-		  }
-
- 		  /* if (bitstream_get (1)) val = -val; */
-		  val = (val ^ SBITS(1)) - SBITS(1);
-		  DUMPBITS(1);
-		}
+			if (tab->run==65) /* escape */
+			{
+				if(!decoder.mpeg1)
+				{
+				  val = (SBITS(12) * quantizer_scale * quant_matrix[i]) >> 4;
+				  DUMPBITS(12);
+				}
+				else
+				{
+				  val = SBITS(8);
+				  DUMPBITS(8);
+
+				  if (!(val & 0x7f))
+				  {
+					val = GETBITS(8) + 2 * val;
+				  }
 
-		SATURATE(val);
-		dest[j] = val;
-		ipu_cmd.pos[5] = 0;
+				  val = (val * quantizer_scale * quant_matrix[i]) >> 4;
+				  val = (val + ~ (((s32)val) >> 31)) | 1;
+				}
+			}
+			else
+			{
+				val = (tab->level * quantizer_scale * quant_matrix[i]) >> 4;
+				if(decoder.mpeg1)
+				{
+					/* oddification */
+					val = (val - 1) | 1;
+				}
+
+				/* if (bitstream_get (1)) val = -val; */
+				int bit1 = SBITS(1);
+				val = (val ^ bit1) - bit1;
+				DUMPBITS(1);
+			}
+
+			SATURATE(val);
+			dest[j] = val;
+			ipu_cmd.pos[5] = 0;
+		}
 	 }
   }
 
@@ -488,7 +491,7 @@
   return true;
 }
 
-static __fi bool get_non_intra_block(int * last)
+static bool get_non_intra_block(int * last)
 {
 	int i;
 	int j;
@@ -614,8 +617,9 @@
 			}
 			else
 			{
+				int bit1 = SBITS(1);
 				val = ((2 * tab->level + 1) * quantizer_scale * quant_matrix[i]) >> 5;
-				val = (val ^ SBITS(1)) - SBITS(1);
+				val = (val ^ bit1) - bit1;
 				DUMPBITS(1);
 			}
 
@@ -682,25 +686,11 @@
 {
 	ipuRegs.ctrl.SCD = 0;
 	coded_block_pattern = decoder.coded_block_pattern;
-
-	g_BP.BP += decoder.bitstream_bits - 16;
-
-	if ((int)g_BP.BP < 0)
-	{
-		g_BP.BP = 128 + (int)g_BP.BP;
-
-		// After BP is positioned correctly, we need to reload the old buffer
-		// so that reading may continue properly
-		ReorderBitstream();
-	}
-
-	FillInternalBuffer(&g_BP.BP, 1, 0);
 }
 
 bool mpeg2sliceIDEC()
 {
 	u16 code;
-	u8 bit8;
 
 	switch (ipu_cmd.pos[0])
 	{
@@ -798,6 +788,9 @@
 						ipu_cmd.pos[2] = 6;
 						return false;
 					}
+					break;
+
+				jNO_DEFAULT;
 				}
 
 				// Send The MacroBlock via DmaIpuFrom
@@ -812,23 +805,23 @@
 				}
 
 			case 2:
-				while (decoder.ipu0_data > 0)
-				{
-					uint read = ipu_fifo.out.write((u32*)decoder.GetIpuDataPtr(), decoder.ipu0_data);
+			{
+				pxAssume(decoder.ipu0_data > 0);
 
-					if (read == 0)
-					{
-						ipu_cmd.pos[1] = 2;
-						return false;
-					}
-					else
-					{
-						decoder.AdvanceIpuDataBy(read);
-					}
+				uint read = ipu_fifo.out.write((u32*)decoder.GetIpuDataPtr(), decoder.ipu0_data);
+				decoder.AdvanceIpuDataBy(read);
+
+				if (decoder.ipu0_data != 0)
+				{
+					// IPU FIFO filled up -- Will have to finish transferring later.
+					ipu_cmd.pos[1] = 2;
+					return false;
 				}
 
 				decoder.mbc++;
 				mbaCount = 0;
+			}
+			
 			case 3:
 				while (1)
 				{
@@ -851,18 +844,18 @@
 					}
 					else switch (UBITS(11))
 					{
-							case 8:		/* macroblock_escape */
-								mbaCount += 33;
-								/* pass through */
-
-							case 15:	/* macroblock_stuffing (MPEG1 only) */
-								DUMPBITS(11);
-								continue;
-
-							default:	/* end of slice/frame, or error? */
-							{
-								goto finish_idec;	
-							}
+						case 8:		/* macroblock_escape */
+							mbaCount += 33;
+							/* pass through */
+
+						case 15:	/* macroblock_stuffing (MPEG1 only) */
+							DUMPBITS(11);
+							continue;
+
+						default:	/* end of slice/frame, or error? */
+						{
+							goto finish_idec;	
+						}
 					}
 				}
 
@@ -886,17 +879,20 @@
 				}
 
 				break;
+
+			jNO_DEFAULT;
 			}
 
 			ipu_cmd.pos[1] = 0;
 			ipu_cmd.pos[2] = 0;
 		}
-		
+
 finish_idec:
 		finishmpeg2sliceIDEC();
 
 	case 3:
-		bit8 = 1;
+	{
+		u8 bit8;
 		if (!getBits8((u8*)&bit8, 0))
 		{
 			ipu_cmd.pos[0] = 3;
@@ -905,10 +901,10 @@
 
 		if (bit8 == 0)
 		{
-			if (g_BP.BP & 7) g_BP.BP += 8 - (g_BP.BP & 7);
-
+			g_BP.Align();
 			ipuRegs.ctrl.SCD = 1;
 		}
+	}
 
 	case 4:
 		if (!getBits32((u8*)&ipuRegs.top, 0))
@@ -917,8 +913,10 @@
 			return false;
 		}
 
-		BigEndian(ipuRegs.top, ipuRegs.top);
+		ipuRegs.top = BigEndian(ipuRegs.top);
 		break;
+
+	jNO_DEFAULT;
 	}
 
 	return true;
@@ -927,7 +925,6 @@
 bool mpeg2_slice()
 {
 	int DCT_offset, DCT_stride;
-	u8 bit8;
 
 	macroblock_8& mb8 = decoder.mb8;
 	macroblock_16& mb16 = decoder.mb16;
@@ -1010,9 +1007,35 @@
 					return false;
 				}
 				break;
+
+			jNO_DEFAULT;
 			}
 
-			ipu_copy(mb8, mb16);
+			// Copy macroblock8 to macroblock16 - without sign extension.
+			// Manually inlined due to MSVC refusing to inline the SSE-optimized version.
+			{
+				const u8	*s = (const u8*)&mb8;
+				u16			*d = (u16*)&mb16;
+
+				//Y  bias	- 16 * 16
+				//Cr bias	- 8 * 8
+				//Cb bias	- 8 * 8
+
+				__m128i zeroreg = _mm_setzero_si128();
+
+				for (uint i = 0; i < (256+64+64) / 32; ++i)
+				{
+					//*d++ = *s++;
+					__m128i woot1 = _mm_load_si128((__m128i*)s);
+					__m128i woot2 = _mm_load_si128((__m128i*)s+1);
+					_mm_store_si128((__m128i*)d,	_mm_unpacklo_epi8(woot1, zeroreg));
+					_mm_store_si128((__m128i*)d+1,	_mm_unpackhi_epi8(woot1, zeroreg));
+					_mm_store_si128((__m128i*)d+2,	_mm_unpacklo_epi8(woot2, zeroreg));
+					_mm_store_si128((__m128i*)d+3,	_mm_unpackhi_epi8(woot2, zeroreg));
+					s += 32;
+					d += 32;
+				}
+			}
 		}
 		else
 		{
@@ -1077,6 +1100,8 @@
 						}
 					}
 					break;
+
+				jNO_DEFAULT;
 				}
 			}
 		}
@@ -1084,40 +1109,31 @@
 		// Send The MacroBlock via DmaIpuFrom
 		ipuRegs.ctrl.SCD = 0;
 		coded_block_pattern = decoder.coded_block_pattern;
-		g_BP.BP += (int)decoder.bitstream_bits - 16;
-
-		// BP goes from 0 to 128, so negative values mean to read old buffer
-		// so we minus from 128 to get the correct BP
-		if ((int)g_BP.BP < 0)
-		{
-			g_BP.BP = 128 + (int)g_BP.BP;
-
-			// After BP is positioned correctly, we need to reload the old buffer
-			// so that reading may continue properly
-			ReorderBitstream();
-		}
 
 		decoder.mbc = 1;
 		decoder.SetOutputTo(mb16);
 
 	case 3:
-		while (decoder.ipu0_data > 0)
-		{
-			uint size = ipu_fifo.out.write((u32*)decoder.GetIpuDataPtr(), decoder.ipu0_data);
+	{
+		pxAssume(decoder.ipu0_data > 0);
 
-			if (size == 0)
-			{
-				ipu_cmd.pos[0] = 3;
-				return false;
-			}
-			else
-			{
-				decoder.AdvanceIpuDataBy(size);
-			}
+		uint read = ipu_fifo.out.write((u32*)decoder.GetIpuDataPtr(), decoder.ipu0_data);
+		decoder.AdvanceIpuDataBy(read);
+
+		if (decoder.ipu0_data != 0)
+		{
+			// IPU FIFO filled up -- Will have to finish transferring later.
+			ipu_cmd.pos[0] = 3;
+			return false;
 		}
 
+		decoder.mbc++;
+		mbaCount = 0;
+	}
+	
 	case 4:
-		bit8 = 1;
+	{
+		u8 bit8;
 		if (!getBits8((u8*)&bit8, 0))
 		{
 			ipu_cmd.pos[0] = 4;
@@ -1126,11 +1142,11 @@
 
 		if (bit8 == 0)
 		{
-			if (g_BP.BP & 7) g_BP.BP += 8 - (g_BP.BP & 7);
-
+			g_BP.Align();
 			ipuRegs.ctrl.SCD = 1;
 		}
-
+	}
+	
 	case 5:
 		if (!getBits32((u8*)&ipuRegs.top, 0))
 		{
@@ -1138,8 +1154,7 @@
 			return false;
 		}
 
-		BigEndian(ipuRegs.top, ipuRegs.top);
-		decoder.bitstream_bits = 0;
+		ipuRegs.top = BigEndian(ipuRegs.top);
 		break;
 	}
 

 

  ViewVC Help
Powered by ViewVC 1.1.22