Index: libmpc/branches/r2d/libmpcdec/mpc_decoder.c
===================================================================
--- libmpc/branches/r2d/libmpcdec/mpc_decoder.c	(revision 151)
+++ libmpc/branches/r2d/libmpcdec/mpc_decoder.c	(revision 158)
@@ -91,4 +91,5 @@
 {
 	memset(d, 0, sizeof *d);
+	mpc_decoder_reset_scf(d); // to be able to decode a cutted stream without big quirks
 
 	d->__r1 = 1;
@@ -342,142 +343,84 @@
     static const mpc_int32_t idx51[] = { -2,-2,-2,-2,-2,-1,-1,-1,-1,-1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2};
 
-    mpc_int32_t n,k;
-    mpc_int32_t Max_used_Band=0;
-    const mpc_huffman *Table;
-    mpc_int32_t idx;
-    mpc_int32_t *L   ,*R;
-    mpc_int32_t *ResL,*ResR;
-    mpc_uint32_t tmp;
+    mpc_int32_t n, idx, Max_used_Band = 0;
 
     /***************************** Header *****************************/
-    ResL  = d->Res_L;
-    ResR  = d->Res_R;
 
     // first subband
-    *ResL = mpc_bits_read(r, 4);
-    *ResR = mpc_bits_read(r, 4);
-	if (d->ms && !(*ResL==0 && *ResR==0)) {
+	d->Res_L[0] = mpc_bits_read(r, 4);
+	d->Res_R[0] = mpc_bits_read(r, 4);
+	if (d->ms && !(d->Res_L[0] == 0 && d->Res_R[0] == 0))
         d->MS_Flag[0] = mpc_bits_read(r, 1);
-    }
 
     // consecutive subbands
-    ++ResL; ++ResR; // increase pointers
-	for (n=1; n <= d->max_band; ++n, ++ResL, ++ResR)
-    {
+	for ( n = 1; n <= d->max_band; n++ ) {
 		idx   = mpc_bits_huff_dec(r, mpc_table_HuffHdr);
-        *ResL = (idx!=4) ? *(ResL-1) + idx : (int) mpc_bits_read(r, 4);
+		d->Res_L[n] = (idx!=4) ? d->Res_L[n - 1] + idx : (int) mpc_bits_read(r, 4);
 
 		idx   = mpc_bits_huff_dec(r, mpc_table_HuffHdr);
-        *ResR = (idx!=4) ? *(ResR-1) + idx : (int) mpc_bits_read(r, 4);
-
-		if (d->ms && !(*ResL==0 && *ResR==0)) {
-            d->MS_Flag[n] = mpc_bits_read(r, 1);
-        }
-
-        // only perform following procedures up to the maximum non-zero subband
-        if (*ResL!=0 || *ResR!=0) {
-            Max_used_Band = n;
-        }
+		d->Res_R[n] = (idx!=4) ? d->Res_R[n - 1] + idx : (int) mpc_bits_read(r, 4);
+
+		if (!(d->Res_L[n] == 0 && d->Res_R[n] == 0)) {
+			if (d->ms)
+            	d->MS_Flag[n] = mpc_bits_read(r, 1);
+			Max_used_Band = n;
+		}
     }
     /****************************** SCFI ******************************/
-    L     = d->SCFI_L;
-    R     = d->SCFI_R;
-    ResL  = d->Res_L;
-    ResR  = d->Res_R;
-    for (n=0; n <= Max_used_Band; ++n, ++L, ++R, ++ResL, ++ResR) {
-		if (*ResL) *L = mpc_bits_huff_dec(r, mpc_table_HuffSCFI);
-		if (*ResR) *R = mpc_bits_huff_dec(r, mpc_table_HuffSCFI);
+    for ( n = 0; n <= Max_used_Band; n++ ) {
+		if (d->Res_L[n])
+			d->SCFI_L[n] = mpc_bits_huff_dec(r, mpc_table_HuffSCFI);
+		if (d->Res_R[n])
+			d->SCFI_R[n] = mpc_bits_huff_dec(r, mpc_table_HuffSCFI);
     }
 
     /**************************** SCF/DSCF ****************************/
-    ResL  = d->Res_L;
-    ResR  = d->Res_R;
-    L     = d->SCF_Index_L[0];
-    R     = d->SCF_Index_R[0];
-    for (n=0; n<=Max_used_Band; ++n, ++ResL, ++ResR, L+=3, R+=3) {
-        if (*ResL)
-        {
-            switch (d->SCFI_L[n])
-            {
-            case 1:
-                idx  = mpc_bits_huff_dec(r, mpc_table_HuffDSCF);
-                L[0] = (idx!=8) ? L[2] + idx : (int) mpc_bits_read(r, 6);
-                idx  = mpc_bits_huff_dec(r, mpc_table_HuffDSCF);
-                L[1] = (idx!=8) ? L[0] + idx : (int) mpc_bits_read(r, 6);
-                L[2] = L[1];
-                break;
-            case 3:
-                idx  = mpc_bits_huff_dec(r, mpc_table_HuffDSCF);
-                L[0] = (idx!=8) ? L[2] + idx : (int) mpc_bits_read(r, 6);
-                L[1] = L[0];
-                L[2] = L[1];
-                break;
-            case 2:
-                idx  = mpc_bits_huff_dec(r, mpc_table_HuffDSCF);
-                L[0] = (idx!=8) ? L[2] + idx : (int) mpc_bits_read(r, 6);
-                L[1] = L[0];
-                idx  = mpc_bits_huff_dec(r, mpc_table_HuffDSCF);
-                L[2] = (idx!=8) ? L[1] + idx : (int) mpc_bits_read(r, 6);
-                break;
-            case 0:
-                idx  = mpc_bits_huff_dec(r, mpc_table_HuffDSCF);
-                L[0] = (idx!=8) ? L[2] + idx : (int) mpc_bits_read(r, 6);
-                idx  = mpc_bits_huff_dec(r, mpc_table_HuffDSCF);
-                L[1] = (idx!=8) ? L[0] + idx : (int) mpc_bits_read(r, 6);
-                idx  = mpc_bits_huff_dec(r, mpc_table_HuffDSCF);
-                L[2] = (idx!=8) ? L[1] + idx : (int) mpc_bits_read(r, 6);
-                break;
-            default:
-                return;
-            }
-            if (L[0] > 1024)
-                L[0] = 0x8080;
-            if (L[1] > 1024)
-                L[1] = 0x8080;
-            if (L[2] > 1024)
-                L[2] = 0x8080;
-        }
-        if (*ResR)
-        {
-            switch (d->SCFI_R[n])
-            {
-            case 1:
-                idx  = mpc_bits_huff_dec(r, mpc_table_HuffDSCF);
-                R[0] = (idx!=8) ? R[2] + idx : (int) mpc_bits_read(r, 6);
-                idx  = mpc_bits_huff_dec(r, mpc_table_HuffDSCF);
-                R[1] = (idx!=8) ? R[0] + idx : (int) mpc_bits_read(r, 6);
-                R[2] = R[1];
-                break;
-            case 3:
-                idx  = mpc_bits_huff_dec(r, mpc_table_HuffDSCF);
-                R[0] = (idx!=8) ? R[2] + idx : (int) mpc_bits_read(r, 6);
-                R[1] = R[0];
-                R[2] = R[1];
-                break;
-            case 2:
-                idx  = mpc_bits_huff_dec(r, mpc_table_HuffDSCF);
-                R[0] = (idx!=8) ? R[2] + idx : (int) mpc_bits_read(r, 6);
-                R[1] = R[0];
-                idx  = mpc_bits_huff_dec(r, mpc_table_HuffDSCF);
-                R[2] = (idx!=8) ? R[1] + idx : (int) mpc_bits_read(r, 6);
-                break;
-            case 0:
-                idx  = mpc_bits_huff_dec(r, mpc_table_HuffDSCF);
-                R[0] = (idx!=8) ? R[2] + idx : (int) mpc_bits_read(r, 6);
-                idx  = mpc_bits_huff_dec(r, mpc_table_HuffDSCF);
-                R[1] = (idx!=8) ? R[0] + idx : (int) mpc_bits_read(r, 6);
-                idx  = mpc_bits_huff_dec(r, mpc_table_HuffDSCF);
-                R[2] = (idx!=8) ? R[1] + idx : (int) mpc_bits_read(r, 6);
-                break;
-            default:
-                return;
-            }
-            if (R[0] > 1024)
-                R[0] = 0x8080;
-            if (R[1] > 1024)
-                R[1] = 0x8080;
-            if (R[2] > 1024)
-                R[2] = 0x8080;
-        }
+    for ( n = 0; n <= Max_used_Band; n++ ) {
+		mpc_int32_t * SCF = d->SCF_Index_L[n];
+		mpc_uint32_t Res  = d->Res_L[n], SCFI = d->SCFI_L[n];
+		do {
+			if (Res) {
+				switch (SCFI) {
+					case 1:
+						idx  = mpc_bits_huff_dec(r, mpc_table_HuffDSCF);
+						SCF[0] = (idx!=8) ? SCF[2] + idx : (int) mpc_bits_read(r, 6);
+						idx  = mpc_bits_huff_dec(r, mpc_table_HuffDSCF);
+						SCF[1] = (idx!=8) ? SCF[0] + idx : (int) mpc_bits_read(r, 6);
+						SCF[2] = SCF[1];
+						break;
+					case 3:
+						idx  = mpc_bits_huff_dec(r, mpc_table_HuffDSCF);
+						SCF[0] = (idx!=8) ? SCF[2] + idx : (int) mpc_bits_read(r, 6);
+						SCF[1] = SCF[0];
+						SCF[2] = SCF[1];
+						break;
+					case 2:
+						idx  = mpc_bits_huff_dec(r, mpc_table_HuffDSCF);
+						SCF[0] = (idx!=8) ? SCF[2] + idx : (int) mpc_bits_read(r, 6);
+						SCF[1] = SCF[0];
+						idx  = mpc_bits_huff_dec(r, mpc_table_HuffDSCF);
+						SCF[2] = (idx!=8) ? SCF[1] + idx : (int) mpc_bits_read(r, 6);
+						break;
+					case 0:
+						idx  = mpc_bits_huff_dec(r, mpc_table_HuffDSCF);
+						SCF[0] = (idx!=8) ? SCF[2] + idx : (int) mpc_bits_read(r, 6);
+						idx  = mpc_bits_huff_dec(r, mpc_table_HuffDSCF);
+						SCF[1] = (idx!=8) ? SCF[0] + idx : (int) mpc_bits_read(r, 6);
+						idx  = mpc_bits_huff_dec(r, mpc_table_HuffDSCF);
+						SCF[2] = (idx!=8) ? SCF[1] + idx : (int) mpc_bits_read(r, 6);
+						break;
+					default:
+						return;
+				}
+				if (SCF[0] > 1024)
+					SCF[0] = 0x8080;
+				if (SCF[1] > 1024)
+					SCF[1] = 0x8080;
+				if (SCF[2] > 1024)
+					SCF[2] = 0x8080;
+			}
+			Res = d->Res_R[n];
+			SCFI = d->SCFI_R[n];
+		} while ( SCF == d->SCF_Index_L[n] && (SCF = d->SCF_Index_R[n]));
     }
 
@@ -486,130 +429,55 @@
 
     /***************************** Samples ****************************/
-    ResL = d->Res_L;
-    ResR = d->Res_R;
-    L    = d->Q[0].L;
-    R    = d->Q[0].R;
-    for (n=0; n <= Max_used_Band; ++n, ++ResL, ++ResR, L+=36, R+=36)
-    {
-        /************** links **************/
-        switch (*ResL)
-        {
-        case  -2: case  -3: case  -4: case  -5: case  -6: case  -7: case  -8: case  -9:
-        case -10: case -11: case -12: case -13: case -14: case -15: case -16: case -17:
-            L += 36;
-            break;
-        case -1:
-            for (k=0; k<36; k++ ) {
-                tmp  = mpc_random_int(d);
-                *L++ = ((tmp >> 24) & 0xFF) + ((tmp >> 16) & 0xFF) + ((tmp >>  8) & 0xFF) + ((tmp >>  0) & 0xFF) - 510;
-            }
-            break;
-        case 0:
-            L += 36;// increase pointer
-            break;
-        case 1:
-            Table = mpc_table_HuffQ[mpc_bits_read(r, 1)][1];
-            for (k=0; k<12; ++k)
-            {
-                idx = mpc_bits_huff_dec(r, Table);
-                *L++ = idx30[idx];
-                *L++ = idx31[idx];
-                *L++ = idx32[idx];
-            }
-            break;
-        case 2:
-            Table = mpc_table_HuffQ[mpc_bits_read(r, 1)][2];
-            for (k=0; k<18; ++k)
-            {
-                idx = mpc_bits_huff_dec(r, Table);
-                *L++ = idx50[idx];
-                *L++ = idx51[idx];
-            }
-            break;
-        case 3:
-        case 4:
-            Table = mpc_table_HuffQ[mpc_bits_read(r, 1)][*ResL];
-            for (k=0; k<36; ++k)
-                *L++ = mpc_bits_huff_dec(r, Table);
-            break;
-        case 5:
-            Table = mpc_table_HuffQ[mpc_bits_read(r, 1)][*ResL];
-            for (k=0; k<36; ++k)
-                *L++ = mpc_bits_huff_dec(r, Table);
-            break;
-        case 6:
-        case 7:
-            Table = mpc_table_HuffQ[mpc_bits_read(r, 1)][*ResL];
-            for (k=0; k<36; ++k)
-                *L++ = mpc_bits_huff_dec(r, Table);
-            break;
-        case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17:
-            tmp = Dc[*ResL];
-            for (k=0; k<36; ++k)
-                *L++ = (mpc_int32_t)mpc_bits_read(r, Res_bit[*ResL]) - tmp;
-            break;
-        default:
-            return;
-        }
-        /************** rechts **************/
-        switch (*ResR)
-        {
-        case  -2: case  -3: case  -4: case  -5: case  -6: case  -7: case  -8: case  -9:
-        case -10: case -11: case -12: case -13: case -14: case -15: case -16: case -17:
-            R += 36;
-            break;
-        case -1:
-                for (k=0; k<36; k++ ) {
-                    tmp  = mpc_random_int(d);
-                    *R++ = ((tmp >> 24) & 0xFF) + ((tmp >> 16) & 0xFF) + ((tmp >>  8) & 0xFF) + ((tmp >>  0) & 0xFF) - 510;
-                }
-                break;
-            case 0:
-                R += 36;// increase pointer
-                break;
-            case 1:
-                Table = mpc_table_HuffQ[mpc_bits_read(r, 1)][1];
-                for (k=0; k<12; ++k)
-                {
-                    idx = mpc_bits_huff_dec(r, Table);
-                    *R++ = idx30[idx];
-                    *R++ = idx31[idx];
-                    *R++ = idx32[idx];
-                }
-                break;
-            case 2:
-                Table = mpc_table_HuffQ[mpc_bits_read(r, 1)][2];
-                for (k=0; k<18; ++k)
-                {
-                    idx = mpc_bits_huff_dec(r, Table);
-                    *R++ = idx50[idx];
-                    *R++ = idx51[idx];
-                }
-                break;
-            case 3:
-            case 4:
-                Table = mpc_table_HuffQ[mpc_bits_read(r, 1)][*ResR];
-                for (k=0; k<36; ++k)
-                    *R++ = mpc_bits_huff_dec(r, Table);
-                break;
-            case 5:
-                Table = mpc_table_HuffQ[mpc_bits_read(r, 1)][*ResR];
-                for (k=0; k<36; ++k)
-                    *R++ = mpc_bits_huff_dec(r, Table);
-                break;
-            case 6:
-            case 7:
-                Table = mpc_table_HuffQ[mpc_bits_read(r, 1)][*ResR];
-                for (k=0; k<36; ++k)
-                    *R++ = mpc_bits_huff_dec(r, Table);
-                break;
-            case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17:
-                tmp = Dc[*ResR];
-                for (k=0; k<36; ++k)
-                    *R++ = (mpc_int32_t)mpc_bits_read(r, Res_bit[*ResR]) - tmp;
-                break;
-            default:
-                return;
-        }
+    for ( n = 0; n <= Max_used_Band; n++ ) {
+		mpc_int32_t *q = d->Q[n].L, Res = d->Res_L[n];
+		do {
+			mpc_int32_t k;
+			const mpc_huffman *Table;
+			switch (Res) {
+				case  -2: case  -3: case  -4: case  -5: case  -6: case  -7: case  -8: case  -9:
+				case -10: case -11: case -12: case -13: case -14: case -15: case -16: case -17: case 0:
+					break;
+				case -1:
+					for (k=0; k<36; k++ ) {
+						mpc_uint32_t tmp = mpc_random_int(d);
+						q[k] = ((tmp >> 24) & 0xFF) + ((tmp >> 16) & 0xFF) + ((tmp >>  8) & 0xFF) + ((tmp >>  0) & 0xFF) - 510;
+					}
+					break;
+				case 1:
+					Table = mpc_table_HuffQ[mpc_bits_read(r, 1)][1];
+					for ( k = 0; k < 36; k += 3) {
+						idx = mpc_bits_huff_dec(r, Table);
+						q[k] = idx30[idx];
+						q[k + 1] = idx31[idx];
+						q[k + 2] = idx32[idx];
+					}
+					break;
+				case 2:
+					Table = mpc_table_HuffQ[mpc_bits_read(r, 1)][2];
+					for ( k = 0; k < 36; k += 2) {
+						idx = mpc_bits_huff_dec(r, Table);
+						q[k] = idx50[idx];
+						q[k + 1] = idx51[idx];
+					}
+					break;
+				case 3:
+				case 4:
+				case 5:
+				case 6:
+				case 7:
+					Table = mpc_table_HuffQ[mpc_bits_read(r, 1)][Res];
+					for ( k = 0; k < 36; k++ )
+						q[k] = mpc_bits_huff_dec(r, Table);
+					break;
+				case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17:
+					for ( k = 0; k < 36; k++ )
+						q[k] = (mpc_int32_t)mpc_bits_read(r, Res_bit[Res]) - Dc[Res];
+					break;
+				default:
+					return;
+			}
+
+			Res = d->Res_R[n];
+		} while (q == d->Q[n].L && (q = d->Q[n].R));
     }
 }
