shithub: aacenc

Download patch

ref: 38656d3a3250370f3ee7dd32899c2c053642c21d
parent: b83f41c9cd91e348ce825521d6ba10b4ef31e8b8
author: lenox <lenox>
date: Thu Mar 23 05:40:25 EST 2000

restored gain control bits in the stream

--- a/aac_se_enc.c
+++ b/aac_se_enc.c
@@ -31,113 +31,111 @@
 /*****************************************************************************/
 int WriteAACFillBits(BsBitStream* ptrBs,  /* Pointer to bitstream */
 		     int numBits,         /* Number of bits needed to fill */
-			 int writeFlag)
+		     int writeFlag)
 {
-	int numberOfBitsLeft=numBits;
+  int numberOfBitsLeft=numBits;
 
-	/* Need at least (LEN_SE_ID + LEN_F_CNT) bits for a fill_element */
-	int minNumberOfBits = LEN_SE_ID + LEN_F_CNT;
-	while (numberOfBitsLeft>=minNumberOfBits) {
-		int numberOfBytes;
-		int maxCount;
+  /* Need at least (LEN_SE_ID + LEN_F_CNT) bits for a fill_element */
+  int minNumberOfBits = LEN_SE_ID + LEN_F_CNT;
+  while (numberOfBitsLeft>=minNumberOfBits) {
+    int numberOfBytes;
+    int maxCount;
 
-		if (writeFlag) {
-			BsPutBit(ptrBs,ID_FIL,LEN_SE_ID);	/* Write fill_element ID */
-		}
-		numberOfBitsLeft-=minNumberOfBits;	/* Subtract for ID,count */
+    if (writeFlag) {
+      BsPutBit(ptrBs,ID_FIL,LEN_SE_ID);	/* Write fill_element ID */
+    }
+    numberOfBitsLeft-=minNumberOfBits;	/* Subtract for ID,count */
 
-		numberOfBytes=(int)(numberOfBitsLeft/LEN_BYTE);
-		maxCount = (1<<LEN_F_CNT) - 1;  /* Max count without escaping */
+    numberOfBytes=(int)(numberOfBitsLeft/LEN_BYTE);
+    maxCount = (1<<LEN_F_CNT) - 1;  /* Max count without escaping */
 
-		/* if we have less than maxCount bytes, write them now */
-		if (numberOfBytes<maxCount) {
-			int i;
-			if (writeFlag) {
-				BsPutBit(ptrBs,numberOfBytes,LEN_F_CNT);
-				for (i=0;i<numberOfBytes;i++) {
-					BsPutBit(ptrBs,0,LEN_BYTE);
-				}
-			}
-			/* otherwise, we need to write an escape count */
-		} else {
-			int maxEscapeCount,maxNumberOfBytes,escCount;
-			int i;
-			if (writeFlag) {
-				BsPutBit(ptrBs,maxCount,LEN_F_CNT);
-			}
-			maxEscapeCount = (1<<LEN_BYTE) - 1;  /* Max escape count */
-			maxNumberOfBytes = maxCount + maxEscapeCount;
-			numberOfBytes = (numberOfBytes > maxNumberOfBytes ) ?
-				(maxNumberOfBytes) : (numberOfBytes);
-			escCount = numberOfBytes - maxCount;
-			if (writeFlag) {
-				BsPutBit(ptrBs,escCount,LEN_BYTE);
-				for (i=0;i<numberOfBytes-1;i++) {
-					BsPutBit(ptrBs,0,LEN_BYTE);
-				}
-			}
-		}
-		numberOfBitsLeft -= LEN_BYTE*numberOfBytes;
-	}
-	return numberOfBitsLeft;
+    /* if we have less than maxCount bytes, write them now */
+    if (numberOfBytes<maxCount) {
+      int i;
+      if (writeFlag) {
+	BsPutBit(ptrBs,numberOfBytes,LEN_F_CNT);
+	for (i=0;i<numberOfBytes;i++) {
+	  BsPutBit(ptrBs,0,LEN_BYTE);
+        }
+      }
+      /* otherwise, we need to write an escape count */
+    }
+    else {
+      int maxEscapeCount,maxNumberOfBytes,escCount;
+      int i;
+      if (writeFlag) {
+	BsPutBit(ptrBs,maxCount,LEN_F_CNT);
+      }
+      maxEscapeCount = (1<<LEN_BYTE) - 1;  /* Max escape count */
+      maxNumberOfBytes = maxCount + maxEscapeCount;
+      numberOfBytes = (numberOfBytes > maxNumberOfBytes ) ? (maxNumberOfBytes) : (numberOfBytes);
+      escCount = numberOfBytes - maxCount;
+      if (writeFlag) {
+	BsPutBit(ptrBs,escCount,LEN_BYTE);
+	for (i=0;i<numberOfBytes-1;i++) {
+	  BsPutBit(ptrBs,0,LEN_BYTE);
+        }
+      }
+    }
+    numberOfBitsLeft -= LEN_BYTE*numberOfBytes;
+  }
+  return numberOfBitsLeft;
 }
 
 int WriteADTSHeader(AACQuantInfo* quantInfo,   /* AACQuantInfo structure */
-					BsBitStream* fixedStream,  /* Pointer to bitstream */
-					int used_bits,
-					int writeFlag)             /* 1 means write, 0 means count only */
+		    BsBitStream* fixedStream,  /* Pointer to bitstream */
+		    int used_bits,
+		    int writeFlag)             /* 1 means write, 0 means count only */
 {
-	if (writeFlag) {
-		/* Fixed ADTS header */
-		BsPutBit(fixedStream, 0xFFFF, 12); // 12 bit Syncword
-		BsPutBit(fixedStream, 1, 1); // ID
-		BsPutBit(fixedStream, 0, 2); // layer
-		BsPutBit(fixedStream, 1, 1); // protection absent
-		BsPutBit(fixedStream, quantInfo->profile, 2); // profile
-		BsPutBit(fixedStream, quantInfo->srate_idx, 4); // sampling rate
-		BsPutBit(fixedStream, 0, 1); // private bit
-		BsPutBit(fixedStream, 1, 3); // ch. config (must be > 0)
-		BsPutBit(fixedStream, 0, 1); // original/copy
-		BsPutBit(fixedStream, 0, 1); // home
-		BsPutBit(fixedStream, 0, 2); // emphasis
+  if (writeFlag) {
+    /* Fixed ADTS header */
+    BsPutBit(fixedStream, 0xFFFF, 12); // 12 bit Syncword
+    BsPutBit(fixedStream, 1, 1); // ID
+    BsPutBit(fixedStream, 0, 2); // layer
+    BsPutBit(fixedStream, 1, 1); // protection absent
+    BsPutBit(fixedStream, quantInfo->profile, 2); // profile
+    BsPutBit(fixedStream, quantInfo->srate_idx, 4); // sampling rate
+    BsPutBit(fixedStream, 0, 1); // private bit
+    BsPutBit(fixedStream, 1, 3); // ch. config (must be > 0)
+    BsPutBit(fixedStream, 0, 1); // original/copy
+    BsPutBit(fixedStream, 0, 1); // home
+    BsPutBit(fixedStream, 0, 2); // emphasis
 
-		/* Variable ADTS header */
-		BsPutBit(fixedStream, 0, 1); // copyr. id. bit
-		BsPutBit(fixedStream, 0, 1); // copyr. id. start
-		BsPutBit(fixedStream, bit2byte(used_bits), 13); // number of bits
-		BsPutBit(fixedStream, 0x7FF, 11); // buffer fullness (0x7FF for VBR)
-		BsPutBit(fixedStream, 0, 2); // raw data blocks (0+1=1)
-	}
-
-	return 58;
+    /* Variable ADTS header */
+    BsPutBit(fixedStream, 0, 1); // copyr. id. bit
+    BsPutBit(fixedStream, 0, 1); // copyr. id. start
+    BsPutBit(fixedStream, bit2byte(used_bits), 13); // number of bits
+    BsPutBit(fixedStream, 0x7FF, 11); // buffer fullness (0x7FF for VBR)
+    BsPutBit(fixedStream, 0, 2); // raw data blocks (0+1=1)
+  }
+  return 58;
 }
 
-
 /*****************************************************************************/
 /* WriteSCE(...), write a single-channel element to the bitstream.           */
 /*****************************************************************************/
 int WriteSCE(AACQuantInfo* quantInfo,   /* AACQuantInfo structure */
-			 int tag,
-			 BsBitStream* fixedStream,  /* Pointer to bitstream */
-			 int writeFlag)             /* 1 means write, 0 means count only */
+	     int tag,
+	     BsBitStream* fixedStream,  /* Pointer to bitstream */
+	     int writeFlag)             /* 1 means write, 0 means count only */
 {
-	int bit_count=0;
+  int bit_count=0;
 
-	if (writeFlag) {
-		/* write ID_SCE, single_element_channel() identifier */
-		BsPutBit(fixedStream,ID_SCE,LEN_SE_ID);  
+  if (writeFlag) {
+    /* write ID_SCE, single_element_channel() identifier */
+    BsPutBit(fixedStream,ID_SCE,LEN_SE_ID);
 
-		/* write the element_identifier_tag */
-		BsPutBit(fixedStream,tag,LEN_TAG);  
-	}
-	
-	bit_count += LEN_SE_ID;
-	bit_count += LEN_TAG;
-	
-	/* Write an individual_channel_stream element */
-	bit_count += WriteICS(quantInfo,0,fixedStream,writeFlag);
-	
-	return bit_count;
+    /* write the element_identifier_tag */
+    BsPutBit(fixedStream,tag,LEN_TAG);
+  }
+
+  bit_count += LEN_SE_ID;
+  bit_count += LEN_TAG;
+
+  /* Write an individual_channel_stream element */
+  bit_count += WriteICS(quantInfo,0,fixedStream,writeFlag);
+
+  return bit_count;
 }
 
 /*****************************************************************************/
@@ -144,133 +142,133 @@
 /* WriteLFE(...), write a lfe-channel element to the bitstream.              */
 /*****************************************************************************/
 int WriteLFE(AACQuantInfo* quantInfo,   /* AACQuantInfo structure */
-			 int tag,
-			 BsBitStream* fixedStream,  /* Pointer to bitstream */
-			 int writeFlag)             /* 1 means write, 0 means count only */
+	     int tag,
+	     BsBitStream* fixedStream,  /* Pointer to bitstream */
+	     int writeFlag)             /* 1 means write, 0 means count only */
 {
-	int bit_count=0;
-	
-	if (writeFlag) {
-		/* write ID_LFE, lfe_element_channel() identifier */
-		BsPutBit(fixedStream,ID_LFE,LEN_SE_ID);  
+  int bit_count=0;
 
-		/* write the element_identifier_tag */
-		BsPutBit(fixedStream,tag,LEN_TAG);  
-	}
-	
-	bit_count += LEN_SE_ID;
-	bit_count += LEN_TAG;
-	
-	/* Write an individual_channel_stream element */
-	bit_count += WriteICS(quantInfo,0,fixedStream,writeFlag);
-	
-	return bit_count;
-}
+  if (writeFlag) {
+    /* write ID_LFE, lfe_element_channel() identifier */
+    BsPutBit(fixedStream,ID_LFE,LEN_SE_ID);
 
+    /* write the element_identifier_tag */
+    BsPutBit(fixedStream,tag,LEN_TAG);
+  }
 
+  bit_count += LEN_SE_ID;
+  bit_count += LEN_TAG;
+
+  /* Write an individual_channel_stream element */
+  bit_count += WriteICS(quantInfo,0,fixedStream,writeFlag);
+
+  return bit_count;
+}
+
 /*****************************************************************************/
 /* WriteCPE(...), write a channel_pair_element to the bitstream.           */
 /*****************************************************************************/
 int WriteCPE(AACQuantInfo* quantInfoL,   /* AACQuantInfo structure, left */
              AACQuantInfo* quantInfoR,   /* AACQuantInfo structure, right */
-			 int tag,
-			 int commonWindow,          /* common_window flag */
-			 MS_Info* ms_info,          /* MS stereo information */
-			 BsBitStream* fixedStream,  /* Pointer to bitstream */
-			 int writeFlag)             /* 1 means write, 0 means count only */
+	     int tag,
+	     int commonWindow,          /* common_window flag */
+	     MS_Info* ms_info,          /* MS stereo information */
+	     BsBitStream* fixedStream,  /* Pointer to bitstream */
+	     int writeFlag)             /* 1 means write, 0 means count only */
 {
-	int bit_count=0;
+  int bit_count=0;
 
-	if (writeFlag) {
-		/* write ID_CPE, single_element_channel() identifier */
-		BsPutBit(fixedStream,ID_CPE,LEN_SE_ID);  
+  if (writeFlag) {
+    /* write ID_CPE, single_element_channel() identifier */
+    BsPutBit(fixedStream,ID_CPE,LEN_SE_ID);
 
-		/* write the element_identifier_tag */
-		BsPutBit(fixedStream,tag,LEN_TAG);  /* Currently, this is zero */
+    /* write the element_identifier_tag */
+    BsPutBit(fixedStream,tag,LEN_TAG);  /* Currently, this is zero */
 
-		/* common_window? */
-		BsPutBit(fixedStream,commonWindow,LEN_COM_WIN);
-	}
-	
-	bit_count += LEN_SE_ID;
-	bit_count += LEN_TAG;
-	bit_count += LEN_COM_WIN;
+    /* common_window? */
+    BsPutBit(fixedStream,commonWindow,LEN_COM_WIN);
+  }
 
-	/* if common_window, write ics_info */
-	if (commonWindow) {
-		int numWindows,maxSfb;
-		bit_count += WriteICSInfo(quantInfoL,fixedStream,writeFlag);
-		numWindows=quantInfoL->num_window_groups;
-		maxSfb = quantInfoL->max_sfb;
-		if (writeFlag) {
-			BsPutBit(fixedStream,ms_info->is_present,LEN_MASK_PRES);
-			if (ms_info->is_present==1) {
-				int g;
-				int b;
-				for (g=0;g<numWindows;g++) {
-					for (b=0;b<maxSfb;b++) {
-						BsPutBit(fixedStream,ms_info->ms_used[g*maxSfb+b],LEN_MASK);
-					}
-				}
-			}
-		}
-		bit_count += LEN_MASK_PRES;
-		if (ms_info->is_present==1)
-			bit_count += (numWindows*maxSfb*LEN_MASK);
-	}
+  bit_count += LEN_SE_ID;
+  bit_count += LEN_TAG;
+  bit_count += LEN_COM_WIN;
 
-	/* Write individual_channel_stream elements */
-	bit_count += WriteICS(quantInfoL,commonWindow,fixedStream,writeFlag);
-	bit_count += WriteICS(quantInfoR,commonWindow,fixedStream,writeFlag);
-	
-	return bit_count;
-}
+  /* if common_window, write ics_info */
+  if (commonWindow) {
+    int numWindows,maxSfb;
+    bit_count += WriteICSInfo(quantInfoL,fixedStream,writeFlag);
+    numWindows=quantInfoL->num_window_groups;
+    maxSfb = quantInfoL->max_sfb;
+    if (writeFlag) {
+      BsPutBit(fixedStream,ms_info->is_present,LEN_MASK_PRES);
+      if (ms_info->is_present==1) {
+	int g;
+	int b;
+	for (g=0;g<numWindows;g++) {
+	  for (b=0;b<maxSfb;b++) {
+	    BsPutBit(fixedStream,ms_info->ms_used[g*maxSfb+b],LEN_MASK);
+          }
+        }
+      }
+    }
+    bit_count += LEN_MASK_PRES;
+    if (ms_info->is_present==1)
+      bit_count += (numWindows*maxSfb*LEN_MASK);
+  }
 
+  /* Write individual_channel_stream elements */
+  bit_count += WriteICS(quantInfoL,commonWindow,fixedStream,writeFlag);
+  bit_count += WriteICS(quantInfoR,commonWindow,fixedStream,writeFlag);
 
+  return bit_count;
+}
+
 /*****************************************************************************/
 /* WriteICS(...), write an individual_channel_stream element to the bitstream.*/
 /*****************************************************************************/
 int WriteICS(AACQuantInfo* quantInfo,    /* AACQuantInfo structure */
-			 int commonWindow,           /* Common window flag */
-			 BsBitStream* fixed_stream,  /* Pointer to bitstream */
-			 int writeFlag)              /* 1 means write, 0 means count only */
+	     int commonWindow,           /* Common window flag */
+	     BsBitStream* fixed_stream,  /* Pointer to bitstream */
+	     int writeFlag)              /* 1 means write, 0 means count only */
 {
-	/* this function writes out an individual_channel_stream to the bitstream and */
-	/* returns the number of bits written to the bitstream */
-	int bit_count = 0;
-//	int output_book_vector[SFB_NUM_MAX*2];
-	writeFlag = ( writeFlag != 0 );
+  /* this function writes out an individual_channel_stream to the bitstream and */
+  /* returns the number of bits written to the bitstream */
+  int bit_count = 0;
+//  int output_book_vector[SFB_NUM_MAX*2];
+  writeFlag = ( writeFlag != 0 );
 
-	/* Write the 8-bit global_gain */
-	BsPutBit(fixed_stream,quantInfo->common_scalefac,writeFlag*LEN_GLOB_GAIN);  
-	bit_count += LEN_GLOB_GAIN;
+  /* Write the 8-bit global_gain */
+  BsPutBit(fixed_stream,quantInfo->common_scalefac,writeFlag*LEN_GLOB_GAIN);
+  bit_count += LEN_GLOB_GAIN;
 
-	/* Write ics information */
-	if (!commonWindow) {
-		bit_count += WriteICSInfo(quantInfo,fixed_stream,writeFlag);
-	}
+  /* Write ics information */
+  if (!commonWindow) {
+    bit_count += WriteICSInfo(quantInfo,fixed_stream,writeFlag);
+  }
 
-	/* Write section_data() information to the bitstream */
-//	bit_count += sort_book_numbers(quantInfo,output_book_vector,fixed_stream,writeFlag);
-	bit_count += sort_book_numbers(quantInfo,fixed_stream,writeFlag);
+  /* Write section_data() information to the bitstream */
+//  bit_count += sort_book_numbers(quantInfo,output_book_vector,fixed_stream,writeFlag);
+  bit_count += sort_book_numbers(quantInfo,fixed_stream,writeFlag);
 
-	/* Write scale_factor_data() information */
-	bit_count += write_scalefactor_bitstream(fixed_stream,writeFlag,quantInfo);
+  /* Write scale_factor_data() information */
+  bit_count += write_scalefactor_bitstream(fixed_stream,writeFlag,quantInfo);
 
-	/* Write pulse_data() */
-	bit_count += WritePulseData(quantInfo,fixed_stream,writeFlag);
+  /* Write pulse_data() */
+  bit_count += WritePulseData(quantInfo,fixed_stream,writeFlag);
 
-	/* Write TNS data */
-	bit_count += WriteTNSData(quantInfo,fixed_stream,writeFlag);
-	
-	/* Write out spectral_data() */
-	bit_count += WriteSpectralData(quantInfo,fixed_stream,writeFlag);
+  /* Write TNS data */
+  bit_count += WriteTNSData(quantInfo,fixed_stream,writeFlag);
 
-	/* Return number of bits */
-	return(bit_count);
-}
+  /* Write gain control data */
+  bit_count += WriteGainControlData(fixed_stream,writeFlag);
 
+  /* Write out spectral_data() */
+  bit_count += WriteSpectralData(quantInfo,fixed_stream,writeFlag);
 
+  /* Return number of bits */
+  return(bit_count);
+}
+
 /*****************************************************************************/
 /* WriteICSInfo(...), write individual_channel_stream information            */
 /*  to the bitstream.                                                        */
@@ -291,14 +289,14 @@
   if (writeFlag) {
     /* write out ics_info() information */
     BsPutBit(fixed_stream,0,LEN_ICS_RESERV);  /* reserved Bit*/
-  
+
     /* Write out window sequence */
     BsPutBit(fixed_stream,quantInfo->block_type,LEN_WIN_SEQ);  /* short window */
 
-    /* Write out window shape */ 
+    /* Write out window shape */
     BsPutBit(fixed_stream,quantInfo->window_shape,LEN_WIN_SH);  /* window shape */
   }
-    
+
   bit_count += LEN_ICS_RESERV;
   bit_count += LEN_WIN_SEQ;
   bit_count += LEN_WIN_SH;
@@ -306,7 +304,7 @@
   /* For short windows, write out max_sfb and scale_factor_grouping */
   if (quantInfo -> block_type == ONLY_SHORT_WINDOW){
     if (writeFlag) {
-      BsPutBit(fixed_stream,max_sfb,LEN_MAX_SFBS); 
+      BsPutBit(fixed_stream,max_sfb,LEN_MAX_SFBS);
       grouping_bits = find_grouping_bits(quantInfo->window_group_length,quantInfo->num_window_groups);
       BsPutBit(fixed_stream,grouping_bits,MAX_SHORT_IN_LONG_BLOCK - 1);  /* the grouping bits */
     }
@@ -313,7 +311,6 @@
     bit_count += LEN_MAX_SFBS;
     bit_count += MAX_SHORT_IN_LONG_BLOCK - 1;
   }
-
   /* Otherwise, write out max_sfb and predictor data */
   else { /* block type is either start, stop, or long */
     if (writeFlag) {
@@ -331,14 +328,14 @@
 /*****************************************************************************/
 int WriteLTP_PredictorData(AACQuantInfo* quantInfo,    /* AACQuantInfo structure */
                            BsBitStream* fixed_stream,  /* Pointer to bitstream */
-                           int writeFlag)              /* 1 means write, 0 means count only */  
+                           int writeFlag)              /* 1 means write, 0 means count only */
 {
-	int bit_count = 0;
+  int bit_count = 0;
 
-	bit_count += nok_ltp_encode (fixed_stream, quantInfo->block_type, quantInfo->nr_of_sfb, 
-		quantInfo->ltpInfo, writeFlag);
+  bit_count += nok_ltp_encode (fixed_stream, quantInfo->block_type, quantInfo->nr_of_sfb,
+                               quantInfo->ltpInfo, writeFlag);
 
-	return (bit_count);
+  return (bit_count);
 }
 
 /*****************************************************************************/
@@ -348,30 +345,31 @@
 		   BsBitStream* fixed_stream,  /* Pointer to bitstream */
 		   int writeFlag)              /* 1 means write, 0 means count only */
 {
-	int i, bit_count = 0;
+  int i, bit_count = 0;
 
-	if (quantInfo->pulseInfo.pulse_data_present) {
-		if (writeFlag) {
-			BsPutBit(fixed_stream,1,LEN_PULSE_PRES);  /* no pulse_data_present */
-			BsPutBit(fixed_stream,quantInfo->pulseInfo.number_pulse,LEN_NEC_NPULSE);  /* no pulse_data_present */
-			BsPutBit(fixed_stream,quantInfo->pulseInfo.pulse_start_sfb,LEN_NEC_ST_SFB);  /* no pulse_data_present */
-		}
-		bit_count += (LEN_NEC_NPULSE + LEN_NEC_ST_SFB);
+  if (quantInfo->pulseInfo.pulse_data_present) {
+    if (writeFlag) {
+      BsPutBit(fixed_stream,1,LEN_PULSE_PRES);  /* no pulse_data_present */
+      BsPutBit(fixed_stream,quantInfo->pulseInfo.number_pulse,LEN_NEC_NPULSE);  /* no pulse_data_present */
+      BsPutBit(fixed_stream,quantInfo->pulseInfo.pulse_start_sfb,LEN_NEC_ST_SFB);  /* no pulse_data_present */
+    }
+    bit_count += (LEN_NEC_NPULSE + LEN_NEC_ST_SFB);
 
-		for (i = 0; i < quantInfo->pulseInfo.number_pulse+1; i++) {
-			if (writeFlag) {
-				BsPutBit(fixed_stream,quantInfo->pulseInfo.pulse_offset[i],LEN_NEC_POFF);
-				BsPutBit(fixed_stream,quantInfo->pulseInfo.pulse_amp[i],LEN_NEC_PAMP);
-			}
-			bit_count += (LEN_NEC_POFF + LEN_NEC_PAMP);
-		}
-	} else {
-		if (writeFlag) {
-			BsPutBit(fixed_stream,0,LEN_PULSE_PRES);  /* no pulse_data_present */
-		}
-	}
-	bit_count += LEN_PULSE_PRES;
-	return bit_count;
+    for (i = 0; i < quantInfo->pulseInfo.number_pulse+1; i++) {
+      if (writeFlag) {
+	BsPutBit(fixed_stream,quantInfo->pulseInfo.pulse_offset[i],LEN_NEC_POFF);
+	BsPutBit(fixed_stream,quantInfo->pulseInfo.pulse_amp[i],LEN_NEC_PAMP);
+      }
+      bit_count += (LEN_NEC_POFF + LEN_NEC_PAMP);
+    }
+  }
+  else {
+    if (writeFlag) {
+      BsPutBit(fixed_stream,0,LEN_PULSE_PRES);  /* no pulse_data_present */
+    }
+  }
+  bit_count += LEN_PULSE_PRES;
+  return bit_count;
 }
 
 /*****************************************************************************/
@@ -410,7 +408,8 @@
     len_tns_nfilt = LEN_TNS_NFILTS;
     len_tns_length = LEN_TNS_LENGTHS;
     len_tns_order = LEN_TNS_ORDERS;
-  } else {
+  }
+  else {
     numWindows = 1;
     len_tns_nfilt = LEN_TNS_NFILTL;
     len_tns_length = LEN_TNS_LENGTHL;
@@ -420,45 +419,59 @@
   /* Write TNS data */
   bit_count += (numWindows * len_tns_nfilt);
   for (w=0;w<numWindows;w++) {
-	  TNS_WINDOW_DATA* windowDataPtr = &tnsInfoPtr->windowData[w];
-	  int numFilters = windowDataPtr->numFilters;
+    TNS_WINDOW_DATA* windowDataPtr = &tnsInfoPtr->windowData[w];
+    int numFilters = windowDataPtr->numFilters;
+    if (writeFlag) {
+      BsPutBit(fixed_stream,numFilters,len_tns_nfilt); /* n_filt[] = 0 */
+    }
+    if (numFilters) {
+      bit_count += LEN_TNS_COEFF_RES;
+      resInBits = windowDataPtr->coefResolution;
+      if (writeFlag) {
+	BsPutBit(fixed_stream,resInBits-DEF_TNS_RES_OFFSET,LEN_TNS_COEFF_RES);
+      }
+      bit_count += numFilters * (len_tns_length+len_tns_order);
+      for (filtNumber=0;filtNumber<numFilters;filtNumber++) {
+	TNS_FILTER_DATA* tnsFilterPtr=&windowDataPtr->tnsFilter[filtNumber];
+	int order = tnsFilterPtr->order;
+	if (writeFlag) {
+	  BsPutBit(fixed_stream,tnsFilterPtr->length,len_tns_length);
+	  BsPutBit(fixed_stream,order,len_tns_order);
+        }
+	if (order) {
+	  bit_count += (LEN_TNS_DIRECTION + LEN_TNS_COMPRESS);
 	  if (writeFlag) {
-		  BsPutBit(fixed_stream,numFilters,len_tns_nfilt); /* n_filt[] = 0 */
-	  }
-	  if (numFilters) {
-		  bit_count += LEN_TNS_COEFF_RES;
-		  resInBits = windowDataPtr->coefResolution;
-		  if (writeFlag) {
-			  BsPutBit(fixed_stream,resInBits-DEF_TNS_RES_OFFSET,LEN_TNS_COEFF_RES);
-		  }
-		  bit_count += numFilters * (len_tns_length+len_tns_order);
-		  for (filtNumber=0;filtNumber<numFilters;filtNumber++) {
-			  TNS_FILTER_DATA* tnsFilterPtr=&windowDataPtr->tnsFilter[filtNumber];
-			  int order = tnsFilterPtr->order;
-			  if (writeFlag) {
-				  BsPutBit(fixed_stream,tnsFilterPtr->length,len_tns_length);
-				  BsPutBit(fixed_stream,order,len_tns_order);
-			  }
-			  if (order) {
-				  bit_count += (LEN_TNS_DIRECTION + LEN_TNS_COMPRESS);
-				  if (writeFlag) {
-					  BsPutBit(fixed_stream,tnsFilterPtr->direction,LEN_TNS_DIRECTION);
-					  BsPutBit(fixed_stream,tnsFilterPtr->coefCompress,LEN_TNS_COMPRESS);
-				  }
-				  bitsToTransmit = resInBits - tnsFilterPtr->coefCompress;
-				  bit_count += order * bitsToTransmit;
-				  if (writeFlag) {
-					  int i;
-					  for (i=1;i<=order;i++) {
-						  unsignedIndex = (unsigned long) (tnsFilterPtr->index[i])&(~(~0<<bitsToTransmit));
-						  BsPutBit(fixed_stream,unsignedIndex,bitsToTransmit);
-					  }
-				  }
-			  }
-		  }
-	  }
+	    BsPutBit(fixed_stream,tnsFilterPtr->direction,LEN_TNS_DIRECTION);
+	    BsPutBit(fixed_stream,tnsFilterPtr->coefCompress,LEN_TNS_COMPRESS);
+          }
+	  bitsToTransmit = resInBits - tnsFilterPtr->coefCompress;
+	  bit_count += order * bitsToTransmit;
+	  if (writeFlag) {
+	    int i;
+	    for (i=1;i<=order;i++) {
+	      unsignedIndex = (unsigned long) (tnsFilterPtr->index[i])&(~(~0<<bitsToTransmit));
+	      BsPutBit(fixed_stream,unsignedIndex,bitsToTransmit);
+            }
+          }
+        }
+      }
+    }
   }
+  return bit_count;
+}
 
+/*****************************************************************************/
+/* WriteGainControlData(...), write gain control data.                       */
+/*****************************************************************************/
+int WriteGainControlData(BsBitStream* fixed_stream,  /* Pointer to bitstream */
+			 int writeFlag)              /* 1 means write, 0 means count only */
+{
+  int bit_count = 0;
+  bit_count += LEN_GAIN_PRES;
+
+  if (writeFlag) {
+    BsPutBit(fixed_stream,0,LEN_GAIN_PRES);
+  }
   return bit_count;
 }
 /*****************************************************************************/
@@ -468,30 +481,31 @@
 		      BsBitStream* fixed_stream,  /* Pointer to bitstream */
 		      int writeFlag)              /* 1 means write, 0 means count only */
 {
-	int bit_count = 0;
-	int numSpectral = quantInfo->spectralCount;
+  int bit_count = 0;
+  int numSpectral = quantInfo->spectralCount;
 
-	/* set up local pointers to data and len */
-	/* data array contains data to be written */
-	/* len array contains lengths of data words */
-	int* data = quantInfo -> data;
-	int* len = quantInfo -> len;
+  /* set up local pointers to data and len */
+  /* data array contains data to be written */
+  /* len array contains lengths of data words */
+  int* data = quantInfo -> data;
+  int* len = quantInfo -> len;
 
-	if (writeFlag) {
-		int i;
-		for(i=0;i<numSpectral;i++) {
-			if (len[i] > 0) {  /* only send out non-zero codebook data */
-				BsPutBit(fixed_stream,data[i],len[i]); /* write data */
-				bit_count += len[i];                   /* update bit_count */
-			}
-		}
-	} else {
-		int i;
-		for(i=0;i<numSpectral;i++) {
-			bit_count += len[i];              /* update bit_count */
-		}
-	}
+  if (writeFlag) {
+    int i;
+    for(i=0;i<numSpectral;i++) {
+      if (len[i] > 0) {  /* only send out non-zero codebook data */
+      	BsPutBit(fixed_stream,data[i],len[i]); /* write data */
+	bit_count += len[i];                   /* update bit_count */
+      }
+    }
+  }
+  else {
+    int i;
+    for(i=0;i<numSpectral;i++) {
+      bit_count += len[i];              /* update bit_count */
+    }
+  }
 
-	return bit_count;
+  return bit_count;
 }
 
--- a/enc_tf.c
+++ b/enc_tf.c
@@ -75,24 +75,23 @@
 
 void EncTfFree (void)
 {
-	int chanNum;
+  int chanNum;
 
-	for (chanNum=0;chanNum<MAX_TIME_CHANNELS;chanNum++) {
-		if (DTimeSigBuf[chanNum]) free(DTimeSigBuf[chanNum]);
-		if (spectral_line_vector[chanNum]) free(spectral_line_vector[chanNum]);
+  for (chanNum=0;chanNum<MAX_TIME_CHANNELS;chanNum++) {
+    if (DTimeSigBuf[chanNum]) free(DTimeSigBuf[chanNum]);
+    if (spectral_line_vector[chanNum]) free(spectral_line_vector[chanNum]);
 
-		if (reconstructed_spectrum[chanNum]) free(reconstructed_spectrum[chanNum]);
-		if (overlap_buffer[chanNum]) free(overlap_buffer[chanNum]);
-		if (nok_lt_status[chanNum].delay) free(nok_lt_status[chanNum].delay);
-		if (nok_tmp_DTimeSigBuf[chanNum]) free(nok_tmp_DTimeSigBuf[chanNum]);
-	}
-	for (chanNum=0;chanNum<MAX_TIME_CHANNELS+2;chanNum++) {
-		if (DTimeSigLookAheadBuf[chanNum]) free(DTimeSigLookAheadBuf[chanNum]);
-	}
+    if (reconstructed_spectrum[chanNum]) free(reconstructed_spectrum[chanNum]);
+    if (overlap_buffer[chanNum]) free(overlap_buffer[chanNum]);
+    if (nok_lt_status[chanNum].delay) free(nok_lt_status[chanNum].delay);
+    if (nok_tmp_DTimeSigBuf[chanNum]) free(nok_tmp_DTimeSigBuf[chanNum]);
+  }
+  for (chanNum=0;chanNum<MAX_TIME_CHANNELS+2;chanNum++) {
+    if (DTimeSigLookAheadBuf[chanNum]) free(DTimeSigLookAheadBuf[chanNum]);
+  }
 }
 
-
-/*****************************************************************************************
+/*******************************************************************************
  ***
  *** Function: EncTfInit
  ***
@@ -99,8 +98,8 @@
  *** Purpose:  Initialize the T/F-part and the macro blocks of the T/F part of the VM
  ***
  *** Description:
- *** 
  ***
+ ***
  *** Parameters:
  ***
  ***
@@ -108,106 +107,102 @@
  ***
  *** **** MPEG-4 VM ****
  ***
- ****************************************************************************************/
+ ******************************************************************************/
 
 void EncTfInit (faacAACStream *as)
 {
-	int chanNum, i;
-	int SampleRates[] = {
-		96000,88200,64000,48000,44100,32000,24000,22050,16000,12000,11025,8000,0
-	};
+  int chanNum, i;
+  int SampleRates[] = { 96000,88200,64000,48000,44100,32000,24000,22050,16000,12000,11025,8000,0};
 //	int BitRates[] = {
 //		64000,80000,96000,112000,128000,160000,192000,224000,256000,0
 //	};
 
-	sampling_rate = as->out_sampling_rate;
-	bit_rate = as->bit_rate;
+  sampling_rate = as->out_sampling_rate;
+  bit_rate = as->bit_rate;
 
-	for (i = 0; ; i++)
-	{
-		if (SampleRates[i] == sampling_rate) {
-			srate_idx = i;
-			break;
-		}
-	}
+  for (i = 0; ; i++) {
+    if (SampleRates[i] == sampling_rate) {
+      srate_idx = i;
+      break;
+    }
+  }
 
-	profile = MAIN;
-	qc_select = AAC_PRED;           /* enable prediction */
+  profile = MAIN;
+  qc_select = AAC_PRED;           /* enable prediction */
 
-	if (as->profile == LOW) {
-		profile = LOW;
-		qc_select = AAC_QC;          /* disable prediction */
-	}
+  if (as->profile == LOW) {
+    profile = LOW;
+    qc_select = AAC_QC;          /* disable prediction */
+  }
 
-	if (as->use_PNS)
-		pns_sfb_start = 0;
-	else
-		pns_sfb_start = 60;
+  if (as->use_PNS)
+    pns_sfb_start = 0;
+  else
+    pns_sfb_start = 60;
 
-	/* set the return values */
-	max_ch = as->channels;
+  /* set the return values */
+  max_ch = as->channels;
 
-	/* some global initializations */
-	for (chanNum=0;chanNum<MAX_TIME_CHANNELS;chanNum++) {
-		DTimeSigBuf[chanNum]            = (double*)malloc(block_size_samples*sizeof(double));
-		memset(DTimeSigBuf[chanNum],0,(block_size_samples)*sizeof(double));
-		spectral_line_vector[chanNum]   = (double*)malloc(2*block_size_samples*sizeof(double));
+  /* some global initializations */
+  for (chanNum=0;chanNum<MAX_TIME_CHANNELS;chanNum++) {
+    DTimeSigBuf[chanNum]            = (double*)malloc(block_size_samples*sizeof(double));
+    memset(DTimeSigBuf[chanNum],0,(block_size_samples)*sizeof(double));
+    spectral_line_vector[chanNum]   = (double*)malloc(2*block_size_samples*sizeof(double));
+    reconstructed_spectrum[chanNum] = (double*)malloc(block_size_samples*sizeof(double));
+    memset(reconstructed_spectrum[chanNum], 0, block_size_samples*sizeof(double));
+    overlap_buffer[chanNum] = (double*)malloc(sizeof(double)*block_size_samples);
+    memset(overlap_buffer[chanNum],0,(block_size_samples)*sizeof(double));
+    block_type[chanNum] = ONLY_LONG_WINDOW;
+    nok_lt_status[chanNum].delay =  (int*)malloc(MAX_SHORT_WINDOWS*sizeof(int));
+    nok_tmp_DTimeSigBuf[chanNum]  = (double*)malloc(2*block_size_samples*sizeof(double));
+    memset(nok_tmp_DTimeSigBuf[chanNum],0,(2*block_size_samples)*sizeof(double));
+  }
+  for (chanNum=0;chanNum<MAX_TIME_CHANNELS+2;chanNum++) {
+    DTimeSigLookAheadBuf[chanNum]   = (double*)malloc((block_size_samples)*sizeof(double));
+    memset(DTimeSigLookAheadBuf[chanNum],0,(block_size_samples)*sizeof(double));
+  }
 
-		reconstructed_spectrum[chanNum] = (double*)malloc(block_size_samples*sizeof(double));
-		memset(reconstructed_spectrum[chanNum], 0, block_size_samples*sizeof(double));
-		overlap_buffer[chanNum] = (double*)malloc(sizeof(double)*block_size_samples);
-		memset(overlap_buffer[chanNum],0,(block_size_samples)*sizeof(double));
-		block_type[chanNum] = ONLY_LONG_WINDOW;
-		nok_lt_status[chanNum].delay =  (int*)malloc(MAX_SHORT_WINDOWS*sizeof(int));
-		nok_tmp_DTimeSigBuf[chanNum]  = (double*)malloc(2*block_size_samples*sizeof(double));
-		memset(nok_tmp_DTimeSigBuf[chanNum],0,(2*block_size_samples)*sizeof(double));
-	}
-	for (chanNum=0;chanNum<MAX_TIME_CHANNELS+2;chanNum++) {
-		DTimeSigLookAheadBuf[chanNum]   = (double*)malloc((block_size_samples)*sizeof(double));
-		memset(DTimeSigLookAheadBuf[chanNum],0,(block_size_samples)*sizeof(double));
-	}
+  /* initialize psychoacoustic module */
+  EncTf_psycho_acoustic_init();
 
-	/* initialize psychoacoustic module */
-	EncTf_psycho_acoustic_init();
+  /* initialize spectrum processing */
+  /* initialize quantization and coding */
+  tf_init_encode_spectrum_aac(0);
 
+  /* Init TNS */
+  for (chanNum=0;chanNum<MAX_TIME_CHANNELS;chanNum++) {
+    TnsInit(sampling_rate,profile,&tnsInfo[chanNum]);
+    quantInfo[chanNum].tnsInfo = &tnsInfo[chanNum];         /* Set pointer to TNS data */
+  }
 
-	/* initialize spectrum processing */
-	/* initialize quantization and coding */
-	tf_init_encode_spectrum_aac(0);
+  /* Init LTP predictor */
+  for (chanNum=0;chanNum<MAX_TIME_CHANNELS;chanNum++) {
+    nok_init_lt_pred (&nok_lt_status[chanNum]);
+    quantInfo[chanNum].ltpInfo = &nok_lt_status[chanNum];  /* Set pointer to LTP data */
+    quantInfo[chanNum].prev_window_shape = WS_SIN;
+  }
 
-	/* Init TNS */
-	for (chanNum=0;chanNum<MAX_TIME_CHANNELS;chanNum++) {
-		TnsInit(sampling_rate,profile,&tnsInfo[chanNum]);
-		quantInfo[chanNum].tnsInfo = &tnsInfo[chanNum];         /* Set pointer to TNS data */
-	}
+  for (chanNum=0;chanNum<MAX_TIME_CHANNELS;chanNum++) {
+    quantInfo[chanNum].srate_idx = srate_idx;
+    quantInfo[chanNum].profile = as->profile;
+  }
 
-	/* Init LTP predictor */
-	for (chanNum=0;chanNum<MAX_TIME_CHANNELS;chanNum++) {
-		nok_init_lt_pred (&nok_lt_status[chanNum]);
-		quantInfo[chanNum].ltpInfo = &nok_lt_status[chanNum];  /* Set pointer to LTP data */
-		quantInfo[chanNum].prev_window_shape = WS_SIN;
-	}
-
-	for (chanNum=0;chanNum<MAX_TIME_CHANNELS;chanNum++) {
-		quantInfo[chanNum].srate_idx = srate_idx;
-		quantInfo[chanNum].profile = as->profile;
-	}
-
-	make_MDCT_windows();
-	make_FFT_order();
-	initrft();
+  /* Initialisation for FFT & MDCT stuff */
+  make_MDCT_windows();
+  make_FFT_order();
+  initrft();
 }
 
-/*****************************************************************************************
+/*******************************************************************************
  ***
  *** Function:    EncTfFrame
  ***
  *** Purpose:     processes a block of time signal input samples into a bitstream
- ***              based on T/F encoding 
+ ***              based on T/F encoding
  ***
  *** Description:
- *** 
  ***
+ ***
  *** Parameters:
  ***
  ***
@@ -215,595 +210,600 @@
  ***
  *** **** MPEG-4 VM ****
  ***
- ****************************************************************************************/
+ ******************************************************************************/
 
 int EncTfFrame (faacAACStream *as, BsBitStream  *fixed_stream)
 {
-	int used_bits;
-	int error;
+  int used_bits;
+  int error;
 
-	/* Energy array (computed before prediction for long windows) */
-	double energy[MAX_TIME_CHANNELS][MAX_SCFAC_BANDS];
-
-	/* determine the function parameters used earlier:   HP 21-aug-96 */
-	int          average_bits = as->frame_bits;
-	int          available_bitreservoir_bits = as->available_bits-as->frame_bits;
+  /* Energy array (computed before prediction for long windows) */
+  double energy[MAX_TIME_CHANNELS][MAX_SCFAC_BANDS];
 
-	/* actual amount of bits currently in the bit reservoir */
-	/* it is the job of this module to determine 
-	the no of bits to use in addition to average_block_bits
-	max. available: average_block_bits + available_bitreservoir_bits */
+  /* determine the function parameters used earlier:   HP 21-aug-96 */
+  int          average_bits = as->frame_bits;
+  int          available_bitreservoir_bits = as->available_bits-as->frame_bits;
+
+  /* actual amount of bits currently in the bit reservoir */
+  /* it is the job of this module to determine
+  the no of bits to use in addition to average_block_bits
+  max. available: average_block_bits + available_bitreservoir_bits */
 //	int max_bitreservoir_bits = 8184;
 
-	/* max. allowed amount of bits in the reservoir  (used to avoid padding bits) */
-	long num_bits_available;
+  /* max. allowed amount of bits in the reservoir  (used to avoid padding bits) */
+  long num_bits_available;
 
-	double *p_ratio[MAX_TIME_CHANNELS], allowed_distortion[MAX_TIME_CHANNELS][MAX_SCFAC_BANDS];
-	static double p_ratio_long[2][MAX_TIME_CHANNELS][MAX_SCFAC_BANDS];
-	static double p_ratio_short[2][MAX_TIME_CHANNELS][MAX_SCFAC_BANDS];
-	int    nr_of_sfb[MAX_TIME_CHANNELS], sfb_width_table[MAX_TIME_CHANNELS][MAX_SCFAC_BANDS];
-	int sfb_offset_table[MAX_TIME_CHANNELS][MAX_SCFAC_BANDS+1];
+  double *p_ratio[MAX_TIME_CHANNELS], allowed_distortion[MAX_TIME_CHANNELS][MAX_SCFAC_BANDS];
+  static double p_ratio_long[2][MAX_TIME_CHANNELS][MAX_SCFAC_BANDS];
+  static double p_ratio_short[2][MAX_TIME_CHANNELS][MAX_SCFAC_BANDS];
+  int    nr_of_sfb[MAX_TIME_CHANNELS], sfb_width_table[MAX_TIME_CHANNELS][MAX_SCFAC_BANDS];
+  int sfb_offset_table[MAX_TIME_CHANNELS][MAX_SCFAC_BANDS+1];
 
 //	int no_sub_win, sub_win_size;
 
-	/* structures holding the output of the psychoacoustic model */
-	CH_PSYCH_OUTPUT_LONG chpo_long[MAX_TIME_CHANNELS+2];
-	CH_PSYCH_OUTPUT_SHORT chpo_short[MAX_TIME_CHANNELS+2][MAX_SHORT_WINDOWS];
-	static int ps = 1;
-	ps = !ps;
+  /* structures holding the output of the psychoacoustic model */
+  CH_PSYCH_OUTPUT_LONG chpo_long[MAX_TIME_CHANNELS+2];
+  CH_PSYCH_OUTPUT_SHORT chpo_short[MAX_TIME_CHANNELS+2][MAX_SHORT_WINDOWS];
+  static int ps = 1;
+  ps = !ps;
 
-	if (as->header_type==ADTS_HEADER)
-		available_bitreservoir_bits += 58;
+  if (as->header_type==ADTS_HEADER)
+    available_bitreservoir_bits += 58;
 
-	{
-		/* store input data in look ahead buffer which may be necessary for the window switching decision */
-		int i;
-		int chanNum;
+  {
+    /* store input data in look ahead buffer which may be necessary for the window switching decision */
+    int i;
+    int chanNum;
 
-		for (chanNum=0;chanNum<max_ch;chanNum++) {
-			if(as->use_LTP)
-				for( i=0; i<block_size_samples; i++ ) {
-					/* temporary fix: a linear buffer for LTP containing the whole time frame */
-					nok_tmp_DTimeSigBuf[chanNum][i] = DTimeSigBuf[chanNum][i];
-					nok_tmp_DTimeSigBuf[chanNum][block_size_samples + i] = DTimeSigLookAheadBuf[chanNum][i];
-				}
-			for( i=0; i<block_size_samples; i++ ) {
-				/* last frame input data are encoded now */
-				DTimeSigBuf[chanNum][i] = DTimeSigLookAheadBuf[chanNum][i];
-				DTimeSigLookAheadBuf[chanNum][i] = as->inputBuffer[chanNum][i];
-			} /* end for(i ..) */
-		} /* end for(chanNum ... ) */
+    for (chanNum=0;chanNum<max_ch;chanNum++) {
+      if(as->use_LTP)
+	for( i=0; i<block_size_samples; i++ ) {
+	  /* temporary fix: a linear buffer for LTP containing the whole time frame */
+	  nok_tmp_DTimeSigBuf[chanNum][i] = DTimeSigBuf[chanNum][i];
+	  nok_tmp_DTimeSigBuf[chanNum][block_size_samples + i] = DTimeSigLookAheadBuf[chanNum][i];
+        }
+	for( i=0; i<block_size_samples; i++ ) {
+	  /* last frame input data are encoded now */
+	  DTimeSigBuf[chanNum][i] = DTimeSigLookAheadBuf[chanNum][i];
+	  DTimeSigLookAheadBuf[chanNum][i] = as->inputBuffer[chanNum][i];
+        } /* end for(i ..) */
+    } /* end for(chanNum ... ) */
 
-		if (as->use_MS == 1) {
-			for (chanNum=0;chanNum<2;chanNum++) {
-				if (chanNum == 0) {
-					for(i = 0; i < block_size_samples; i++){
-						DTimeSigLookAheadBuf[chanNum][i] = (as->inputBuffer[0][i]+as->inputBuffer[1][i])*0.5;
-					}
-				} else {
-					for(i = 0; i < block_size_samples; i++){
-						DTimeSigLookAheadBuf[chanNum][i] = (as->inputBuffer[0][i]-as->inputBuffer[1][i])*0.5;
-					}
-				}
-			}
-		}
+    if (as->use_MS == 1) {
+      for (chanNum=0;chanNum<2;chanNum++) {
+	if (chanNum == 0) {
+	  for(i = 0; i < block_size_samples; i++){
+	    DTimeSigLookAheadBuf[chanNum][i] = (as->inputBuffer[0][i]+as->inputBuffer[1][i])*0.5;
+          }
+        }
+        else {
+	  for(i = 0; i < block_size_samples; i++){
+	    DTimeSigLookAheadBuf[chanNum][i] = (as->inputBuffer[0][i]-as->inputBuffer[1][i])*0.5;
+          }
+        }
+      }
+    }
+  }
 
-	}
+  if (fixed_stream == NULL) {
+    psy_fill_lookahead(DTimeSigLookAheadBuf, max_ch);
+    return FNO_ERROR; /* quick'n'dirty fix for encoder startup    HP 21-aug-96 */
+  }
 
-	if (fixed_stream == NULL) {
-		psy_fill_lookahead(DTimeSigLookAheadBuf, max_ch);
+  /* Keep track of number of bits used */
+  used_bits = 0;
 
-		return FNO_ERROR; /* quick'n'dirty fix for encoder startup    HP 21-aug-96 */
-	}
+  /***********************************************************************/
+  /* Determine channel elements      */
+  /***********************************************************************/
+  DetermineChInfo(channelInfo,max_ch);
 
-	/* Keep track of number of bits used */
-	used_bits = 0;
+  /*****************************************************************************
+  *
+  * psychoacoustic
+  *
+  *****************************************************************************/
+  {
+    int chanNum, channels;
 
-	/***********************************************************************/
-	/* Determine channel elements      */
-	/***********************************************************************/
-	DetermineChInfo(channelInfo,max_ch);
+    if (as->use_MS == 0)
+      channels = max_ch+2;
+    else
+      channels = max_ch;
 
-	/******************************************************************************************************************************
-	*
-	* psychoacoustic
-	*
-	******************************************************************************************************************************/
-	{
-		int chanNum, channels;
+    for (chanNum = 0; chanNum < channels; chanNum++) {
 
-		if (as->use_MS == 0)
-			channels = max_ch+2;
-		else
-			channels = max_ch;
+    EncTf_psycho_acoustic(
+			  sampling_rate,
+			  chanNum,
+			  &DTimeSigLookAheadBuf[chanNum],
+			  &next_desired_block_type[chanNum],
+//			  (int)qc_select,
+//			  block_size_samples,
+			  chpo_long,
+			  chpo_short
+			  );
+    }
+  }
 
-		for (chanNum = 0; chanNum < channels; chanNum++) {
+  /*****************************************************************************
+  *
+  * block_switch processing
+  *
+  *****************************************************************************/
+  {
+    int chanNum;
+    for (chanNum=0;chanNum<max_ch;chanNum++) {
+    /* A few definitions:                                                      */
+    /*   block_type:  Initially, the block_type used in the previous frame.    */
+    /*                Will be set to the block_type to use this frame.         */
+    /*                A block type will be selected to ensure a meaningful     */
+    /*                window transition.                                       */
+    /*   next_desired_block_type:  Block_type (LONG or SHORT) which the psycho */
+    /*                model wants to use next frame.  The psycho model is      */
+    /*                using a look-ahead buffer.                               */
+    /*   desired_block_type:  Block_type (LONG or SHORT) which the psycho      */
+    /*                previously wanted to use.  It is the desired block_type  */
+    /*                for this frame.                                          */
+    if ( (block_type[chanNum]==ONLY_SHORT_WINDOW)||(block_type[chanNum]==LONG_SHORT_WINDOW) ) {
+      if ( (desired_block_type[chanNum]==ONLY_LONG_WINDOW)&&(next_desired_block_type[chanNum]==ONLY_LONG_WINDOW) ) {
+	block_type[chanNum]=SHORT_LONG_WINDOW;
+      }
+      else {
+	block_type[chanNum]=ONLY_SHORT_WINDOW;
+      }
+    }
+    else if (next_desired_block_type[chanNum]==ONLY_SHORT_WINDOW) {
+      block_type[chanNum]=LONG_SHORT_WINDOW;
+      }
+      else {
+	block_type[chanNum]=ONLY_LONG_WINDOW;
+      }
+      desired_block_type[chanNum]=next_desired_block_type[chanNum];
+    }
+  }
 
-			EncTf_psycho_acoustic(
-				sampling_rate,
-				chanNum,
-				&DTimeSigLookAheadBuf[chanNum],
-				&next_desired_block_type[chanNum],
-//				(int)qc_select,
-//				block_size_samples,
-				chpo_long,
-				chpo_short
-				);
-		}
-	}
-
-	/******************************************************************************************************************************
-	*
-	* block_switch processing
-	*
-	******************************************************************************************************************************/
-	{
-		int chanNum;
-		for (chanNum=0;chanNum<max_ch;chanNum++) {
-			/* A few definitions:                                                      */
-			/*   block_type:  Initially, the block_type used in the previous frame.    */
-			/*                Will be set to the block_type to use this frame.         */
-			/*                A block type will be selected to ensure a meaningful     */
-			/*                window transition.                                       */
-			/*   next_desired_block_type:  Block_type (LONG or SHORT) which the psycho */
-			/*                model wants to use next frame.  The psycho model is      */
-			/*                using a look-ahead buffer.                               */
-			/*   desired_block_type:  Block_type (LONG or SHORT) which the psycho      */
-			/*                previously wanted to use.  It is the desired block_type  */
-			/*                for this frame.                                          */
-			if ( (block_type[chanNum]==ONLY_SHORT_WINDOW)||(block_type[chanNum]==LONG_SHORT_WINDOW) ) {
-				if ( (desired_block_type[chanNum]==ONLY_LONG_WINDOW)&&(next_desired_block_type[chanNum]==ONLY_LONG_WINDOW) ) {
-					block_type[chanNum]=SHORT_LONG_WINDOW;
-				} else {
-					block_type[chanNum]=ONLY_SHORT_WINDOW;
-				}
-			} else if (next_desired_block_type[chanNum]==ONLY_SHORT_WINDOW) {
-				block_type[chanNum]=LONG_SHORT_WINDOW;
-			} else {
-				block_type[chanNum]=ONLY_LONG_WINDOW;
-			}
-			desired_block_type[chanNum]=next_desired_block_type[chanNum];
-		}
-	}
-
 //	printf("%d\t\n", block_type[0]);
 //	block_type[0] = ONLY_LONG_WINDOW;
 //	block_type[1] = ONLY_LONG_WINDOW;
 //	block_type[0] = ONLY_SHORT_WINDOW;
 //	block_type[1] = ONLY_SHORT_WINDOW;
-	block_type[1] = block_type[0];
+  block_type[1] = block_type[0];
 
-	{
-		int chanNum;
+  {
+    int chanNum;
 
-		for (chanNum=0;chanNum<max_ch;chanNum++) {
+    for (chanNum=0;chanNum<max_ch;chanNum++) {
+      /* Set window shape paremeter in quantInfo */
+      quantInfo[chanNum].prev_window_shape = quantInfo[chanNum].window_shape;
+//    quantInfo[chanNum].window_shape = WS_KBD;
+      quantInfo[chanNum].window_shape = WS_SIN;
 
-			/* Set window shape paremeter in quantInfo */
-			quantInfo[chanNum].prev_window_shape = quantInfo[chanNum].window_shape;
-//			quantInfo[chanNum].window_shape = WS_KBD;
-			quantInfo[chanNum].window_shape = WS_SIN;
-
-			switch( block_type[chanNum] ) {
-			case ONLY_SHORT_WINDOW  :
-//				no_sub_win   = short_win_in_long;
-//				sub_win_size = block_size_samples/short_win_in_long;
-				quantInfo[chanNum].max_sfb = max_sfb_s[srate_idx];
+      switch( block_type[chanNum] ) {
+        case ONLY_SHORT_WINDOW  :
+//      no_sub_win   = short_win_in_long;
+//      sub_win_size = block_size_samples/short_win_in_long;
+        quantInfo[chanNum].max_sfb = max_sfb_s[srate_idx];
 #if 0
-				quantInfo[chanNum].num_window_groups = 4;
-				quantInfo[chanNum].window_group_length[0] = 1;
-				quantInfo[chanNum].window_group_length[1] = 2;
-				quantInfo[chanNum].window_group_length[2] = 3;
-				quantInfo[chanNum].window_group_length[3] = 2;
+        quantInfo[chanNum].num_window_groups = 4;
+        quantInfo[chanNum].window_group_length[0] = 1;
+        quantInfo[chanNum].window_group_length[1] = 2;
+        quantInfo[chanNum].window_group_length[2] = 3;
+        quantInfo[chanNum].window_group_length[3] = 2;
 #else
-				quantInfo[chanNum].num_window_groups = 1;
-				quantInfo[chanNum].window_group_length[0] = 8;
-				quantInfo[chanNum].window_group_length[1] = 0;
-				quantInfo[chanNum].window_group_length[2] = 0;
-				quantInfo[chanNum].window_group_length[3] = 0;
-				quantInfo[chanNum].window_group_length[4] = 0;
-				quantInfo[chanNum].window_group_length[5] = 0;
-				quantInfo[chanNum].window_group_length[6] = 0;
-				quantInfo[chanNum].window_group_length[7] = 0;
+        quantInfo[chanNum].num_window_groups = 1;
+        quantInfo[chanNum].window_group_length[0] = 8;
+        quantInfo[chanNum].window_group_length[1] = 0;
+        quantInfo[chanNum].window_group_length[2] = 0;
+        quantInfo[chanNum].window_group_length[3] = 0;
+        quantInfo[chanNum].window_group_length[4] = 0;
+        quantInfo[chanNum].window_group_length[5] = 0;
+        quantInfo[chanNum].window_group_length[6] = 0;
+        quantInfo[chanNum].window_group_length[7] = 0;
 #endif
-				break;
+        break;
 
-			default:
-//				no_sub_win   = 1;
-//				sub_win_size = block_size_samples;
-				quantInfo[chanNum].max_sfb = max_sfb_l[srate_idx];
-				quantInfo[chanNum].num_window_groups = 1;
-				quantInfo[chanNum].window_group_length[0]=1;
-				break;
-			}
-		}
-	}
+        default:
+//      no_sub_win   = 1;
+//      sub_win_size = block_size_samples;
+        quantInfo[chanNum].max_sfb = max_sfb_l[srate_idx];
+        quantInfo[chanNum].num_window_groups = 1;
+        quantInfo[chanNum].window_group_length[0]=1;
+        break;
+      }
+    }
+  }
 
-	/******************************************************************************************************************************
-	*
-	* T/F mapping
-	*
-	******************************************************************************************************************************/
+  {
+    int chanNum;
+    for (chanNum=0;chanNum<max_ch;chanNum++) {
+      /* Count number of bits used for gain_control_data */
+      used_bits += WriteGainControlData(NULL,0); /* Zero write flag means don't write */
+    }
+  }
 
-	{
-		int chanNum, k;
-		for (chanNum=0;chanNum<max_ch;chanNum++) {
-			buffer2freq(
-				DTimeSigBuf[chanNum],
-				spectral_line_vector[chanNum],
-				overlap_buffer[chanNum],
-				block_type[chanNum],
-				quantInfo[chanNum].window_shape,
-				quantInfo[chanNum].prev_window_shape,
-				MOVERLAPPED
-				);
 
-			if (block_type[chanNum] == ONLY_SHORT_WINDOW) {  
-				for (k = 0; k < 8; k++) {
-					specFilter(spectral_line_vector[chanNum]+k*BLOCK_LEN_SHORT, spectral_line_vector[chanNum]+k*BLOCK_LEN_SHORT, as->out_sampling_rate, as->cut_off, BLOCK_LEN_SHORT); 
-				}
-			} else {
-				specFilter(spectral_line_vector[chanNum], spectral_line_vector[chanNum], as->out_sampling_rate, as->cut_off, BLOCK_LEN_LONG);
-			}
-		}
-	}
+  /*****************************************************************************
+  *
+  * T/F mapping
+  *
+  *****************************************************************************/
+  {
+    int chanNum, k;
+    for (chanNum=0;chanNum<max_ch;chanNum++) {
+      buffer2freq(
+		  DTimeSigBuf[chanNum],
+		  spectral_line_vector[chanNum],
+		  overlap_buffer[chanNum],
+		  block_type[chanNum],
+		  quantInfo[chanNum].window_shape,
+		  quantInfo[chanNum].prev_window_shape,
+		  MOVERLAPPED
+		  );
 
-	/******************************************************************************************************************************
-	*
-	* adapt ratios of psychoacoustic module to codec scale factor bands
-	*
-	******************************************************************************************************************************/
+      if (block_type[chanNum] == ONLY_SHORT_WINDOW) {
+	for (k = 0; k < 8; k++) {
+	  specFilter(spectral_line_vector[chanNum]+k*BLOCK_LEN_SHORT, spectral_line_vector[chanNum]+k*BLOCK_LEN_SHORT, as->out_sampling_rate, as->cut_off, BLOCK_LEN_SHORT);
+        }
+      }
+      else {
+	specFilter(spectral_line_vector[chanNum], spectral_line_vector[chanNum], as->out_sampling_rate, as->cut_off, BLOCK_LEN_LONG);
+      }
+    }
+  }
 
-	{
-		int chanNum;
-		for (chanNum=0;chanNum<max_ch;chanNum++) {
-			switch( block_type[chanNum] ) {
-			case ONLY_LONG_WINDOW:
-				memcpy( (char*)sfb_width_table[chanNum], (char*)chpo_long[chanNum].cb_width, (NSFB_LONG+1)*sizeof(int) );
-				nr_of_sfb[chanNum] = chpo_long[chanNum].no_of_cb;
-				p_ratio[chanNum]   = p_ratio_long[ps][chanNum];
-				break;
-			case LONG_SHORT_WINDOW:
-				memcpy( (char*)sfb_width_table[chanNum], (char*)chpo_long[chanNum].cb_width, (NSFB_LONG+1)*sizeof(int) );
-				nr_of_sfb[chanNum] = chpo_long[chanNum].no_of_cb;
-				p_ratio[chanNum]   = p_ratio_long[ps][chanNum];
-				break;
-			case ONLY_SHORT_WINDOW:
-				memcpy( (char*)sfb_width_table[chanNum], (char*)chpo_short[chanNum][0].cb_width, (NSFB_SHORT+1)*sizeof(int) );
-				nr_of_sfb[chanNum] = chpo_short[chanNum][0].no_of_cb;
-				p_ratio[chanNum]   = p_ratio_short[ps][chanNum];
-				break;
-			case SHORT_LONG_WINDOW:
-				memcpy( (char*)sfb_width_table[chanNum], (char*)chpo_long[chanNum].cb_width, (NSFB_LONG+1)*sizeof(int) );
-				nr_of_sfb[chanNum] = chpo_long[chanNum].no_of_cb;
-				p_ratio[chanNum]   = p_ratio_long[ps][chanNum];
-				break;
-			}
-		}
-	}
+  /*****************************************************************************
+  *
+  * adapt ratios of psychoacoustic module to codec scale factor bands
+  *
+  *****************************************************************************/
 
-	MSPreprocess(p_ratio_long[!ps], p_ratio_short[!ps], chpo_long, chpo_short,
+  {
+    int chanNum;
+    for (chanNum=0;chanNum<max_ch;chanNum++) {
+      switch( block_type[chanNum] ) {
+	case ONLY_LONG_WINDOW:
+	  memcpy( (char*)sfb_width_table[chanNum], (char*)chpo_long[chanNum].cb_width, (NSFB_LONG+1)*sizeof(int) );
+	  nr_of_sfb[chanNum] = chpo_long[chanNum].no_of_cb;
+	  p_ratio[chanNum]   = p_ratio_long[ps][chanNum];
+	  break;
+        case LONG_SHORT_WINDOW:
+	  memcpy( (char*)sfb_width_table[chanNum], (char*)chpo_long[chanNum].cb_width, (NSFB_LONG+1)*sizeof(int) );
+	  nr_of_sfb[chanNum] = chpo_long[chanNum].no_of_cb;
+	  p_ratio[chanNum]   = p_ratio_long[ps][chanNum];
+	  break;
+        case ONLY_SHORT_WINDOW:
+	  memcpy( (char*)sfb_width_table[chanNum], (char*)chpo_short[chanNum][0].cb_width, (NSFB_SHORT+1)*sizeof(int) );
+	  nr_of_sfb[chanNum] = chpo_short[chanNum][0].no_of_cb;
+	  p_ratio[chanNum]   = p_ratio_short[ps][chanNum];
+          break;
+        case SHORT_LONG_WINDOW:
+	  memcpy( (char*)sfb_width_table[chanNum], (char*)chpo_long[chanNum].cb_width, (NSFB_LONG+1)*sizeof(int) );
+	  nr_of_sfb[chanNum] = chpo_long[chanNum].no_of_cb;
+	  p_ratio[chanNum]   = p_ratio_long[ps][chanNum];
+          break;
+      }
+    }
+  }
+
+  MSPreprocess(p_ratio_long[!ps], p_ratio_short[!ps], chpo_long, chpo_short,
 		channelInfo, block_type, quantInfo, as->use_MS, as->use_IS, max_ch);
 
-	MSEnergy(spectral_line_vector, energy, chpo_long, chpo_short, sfb_width_table,
+  MSEnergy(spectral_line_vector, energy, chpo_long, chpo_short, sfb_width_table,
 //		channelInfo, block_type, quantInfo, as->use_MS, max_ch);
 		block_type, quantInfo, as->use_MS, max_ch);
 
-	{
-		int chanNum;   
-		for (chanNum=0;chanNum<max_ch;chanNum++) {
-			/* Construct sf band offset table */
-			int offset=0;
-			int sfb;
-			for (sfb=0;sfb<nr_of_sfb[chanNum];sfb++) {
-				sfb_offset_table[chanNum][sfb] = offset;
-				offset+=sfb_width_table[chanNum][sfb];
-			}
-			sfb_offset_table[chanNum][nr_of_sfb[chanNum]]=offset;
-		}
-	}
+  {
+    int chanNum;
+    for (chanNum=0;chanNum<max_ch;chanNum++) {
+      /* Construct sf band offset table */
+      int offset=0;
+      int sfb;
+      for (sfb=0;sfb<nr_of_sfb[chanNum];sfb++) {
+	sfb_offset_table[chanNum][sfb] = offset;
+	offset+=sfb_width_table[chanNum][sfb];
+      }
+      sfb_offset_table[chanNum][nr_of_sfb[chanNum]]=offset;
+    }
+  }
 
+  /*****************************************************************************
+  *
+  * quantization and coding
+  *
+  *****************************************************************************/
+  {
+//  int padding_limit = max_bitreservoir_bits;
+    int maxNumBitsByteAligned;
+    int chanNum;
+    int numFillBits;
+    int bitsLeftAfterFill;
+    int orig_used_bits;
 
-	/******************************************************************************************************************************
-	*
-	* quantization and coding
-	*
-	******************************************************************************************************************************/
-	{ 
-//		int padding_limit = max_bitreservoir_bits;
-		int maxNumBitsByteAligned;
-		int chanNum;   
-		int numFillBits;
-		int bitsLeftAfterFill;
-		int orig_used_bits;
+    /* bit budget */
+    num_bits_available = (long)(average_bits + available_bitreservoir_bits - used_bits);
 
-		/* bit budget */
-		num_bits_available = (long)(average_bits + available_bitreservoir_bits - used_bits);
-		
-		/* find the largest byte-aligned section with fewer bits than num_bits_available */
-		maxNumBitsByteAligned = ((num_bits_available >> 3) << 3);
+    /* find the largest byte-aligned section with fewer bits than num_bits_available */
+    maxNumBitsByteAligned = ((num_bits_available >> 3) << 3);
 
-		/* Compute how many reservoir bits can be used and still be able to byte */
-		/* align without exceeding num_bits_available, and have room for an ID_END marker   */
-		available_bitreservoir_bits = maxNumBitsByteAligned - LEN_SE_ID - average_bits;
+    /* Compute how many reservoir bits can be used and still be able to byte */
+    /* align without exceeding num_bits_available, and have room for an ID_END marker   */
+    available_bitreservoir_bits = maxNumBitsByteAligned - LEN_SE_ID - average_bits;
 
-		/******************************************/
-		/* Perform TNS analysis and filtering     */
-		/******************************************/
-		for (chanNum=0;chanNum<max_ch;chanNum++) {
-			error = TnsEncode(nr_of_sfb[chanNum],            /* Number of bands per window */
-				quantInfo[chanNum].max_sfb,              /* max_sfb */
-				block_type[chanNum],
-				sfb_offset_table[chanNum],
-				spectral_line_vector[chanNum],
-				&tnsInfo[chanNum],
-				as->use_TNS);
-			if (error == FERROR)
-				return FERROR;
-		}
+    /******************************************/
+    /* Perform TNS analysis and filtering     */
+    /******************************************/
+    for (chanNum=0;chanNum<max_ch;chanNum++) {
+      error = TnsEncode(nr_of_sfb[chanNum],            /* Number of bands per window */
+      		        quantInfo[chanNum].max_sfb,              /* max_sfb */
+          	        block_type[chanNum],
+	  	        sfb_offset_table[chanNum],
+		        spectral_line_vector[chanNum],
+		        &tnsInfo[chanNum],
+		        as->use_TNS);
+      if (error == FERROR)
+      return FERROR;
+    }
 
-		/******************************************/
-		/* Apply Intensity Stereo                 */
-		/******************************************/
-		if (as->use_IS && (as->use_MS != 1)) {
-			ISEncode(spectral_line_vector,
-				channelInfo,
-				sfb_offset_table,
-				block_type,
-				quantInfo,
-				max_ch);
-		}
+    /******************************************/
+    /* Apply Intensity Stereo                 */
+    /******************************************/
+    if (as->use_IS && (as->use_MS != 1)) {
+      ISEncode(spectral_line_vector,
+  	       channelInfo,
+	       sfb_offset_table,
+	       block_type,
+	       quantInfo,
+	       max_ch);
+    }
 
-		/*******************************************************************************/
-		/* If LTP prediction is used, compute LTP predictor info and residual spectrum */
-		/*******************************************************************************/
-		for(chanNum=0;chanNum<max_ch;chanNum++) 
-		{
-			if(as->use_LTP && (block_type[chanNum] != ONLY_SHORT_WINDOW)) 
-			{
-				if(channelInfo[chanNum].cpe)
-				{
-					if(channelInfo[chanNum].ch_is_left) 
-					{
-						int i;
-						int leftChan=chanNum;
-						int rightChan=channelInfo[chanNum].paired_ch;
-                   
-						nok_ltp_enc(spectral_line_vector[leftChan], 
-							nok_tmp_DTimeSigBuf[leftChan], 
-							block_type[leftChan], 
-							WS_SIN,
-							&sfb_offset_table[leftChan][0], 
-							nr_of_sfb[leftChan],
-							&nok_lt_status[leftChan]);
+    /*******************************************************************************/
+    /* If LTP prediction is used, compute LTP predictor info and residual spectrum */
+    /*******************************************************************************/
+    for(chanNum=0;chanNum<max_ch;chanNum++) {
+      if(as->use_LTP && (block_type[chanNum] != ONLY_SHORT_WINDOW)) {
+        if(channelInfo[chanNum].cpe) {
+    	  if(channelInfo[chanNum].ch_is_left) {
+	    int i;
+	    int leftChan=chanNum;
+	    int rightChan=channelInfo[chanNum].paired_ch;
 
-						nok_lt_status[rightChan].global_pred_flag = 
-							nok_lt_status[leftChan].global_pred_flag;
-						for(i = 0; i < BLOCK_LEN_LONG; i++)
-							nok_lt_status[rightChan].pred_mdct[i] = 
-							nok_lt_status[leftChan].pred_mdct[i];
-						for(i = 0; i < MAX_SCFAC_BANDS; i++)
-							nok_lt_status[rightChan].sfb_prediction_used[i] = 
-							nok_lt_status[leftChan].sfb_prediction_used[i];
-						nok_lt_status[rightChan].weight = nok_lt_status[leftChan].weight;
-						nok_lt_status[rightChan].delay[0] = nok_lt_status[leftChan].delay[0];
+  	    nok_ltp_enc(spectral_line_vector[leftChan],
+		        nok_tmp_DTimeSigBuf[leftChan],
+		        block_type[leftChan],
+		        WS_SIN,
+		        &sfb_offset_table[leftChan][0],
+		        nr_of_sfb[leftChan],
+		        &nok_lt_status[leftChan]);
 
-						if (!channelInfo[leftChan].common_window) {
-							nok_ltp_enc(spectral_line_vector[rightChan],
-							nok_tmp_DTimeSigBuf[rightChan], 
-							block_type[rightChan], 
-							WS_SIN,
-							&sfb_offset_table[rightChan][0], 
-							nr_of_sfb[rightChan],
-							&nok_lt_status[rightChan]);
-						}
-					} /* if(channelInfo[chanNum].ch_is_left) */
-				} /* if(channelInfo[chanNum].cpe) */
-				else
-					nok_ltp_enc(spectral_line_vector[chanNum], 
-					nok_tmp_DTimeSigBuf[chanNum], 
-					block_type[chanNum], 
-					WS_SIN,
-					&sfb_offset_table[chanNum][0], 
-					nr_of_sfb[chanNum],
-					&nok_lt_status[chanNum]);
+            nok_lt_status[rightChan].global_pred_flag = nok_lt_status[leftChan].global_pred_flag;
+  	    for(i = 0; i < BLOCK_LEN_LONG; i++)
+    	      nok_lt_status[rightChan].pred_mdct[i] = nok_lt_status[leftChan].pred_mdct[i];
+  	    for(i = 0; i < MAX_SCFAC_BANDS; i++)
+  	      nok_lt_status[rightChan].sfb_prediction_used[i] = nok_lt_status[leftChan].sfb_prediction_used[i];
+  	    nok_lt_status[rightChan].weight = nok_lt_status[leftChan].weight;
+	    nok_lt_status[rightChan].delay[0] = nok_lt_status[leftChan].delay[0];
 
-			} /* if(channelInfo[chanNum].present... */
-			else
-				quantInfo[chanNum].ltpInfo->global_pred_flag = 0;
-		} /* for(chanNum... */
+	    if (!channelInfo[leftChan].common_window) {
+	      nok_ltp_enc(spectral_line_vector[rightChan],
+			  nok_tmp_DTimeSigBuf[rightChan],
+			  block_type[rightChan],
+			  WS_SIN,
+			  &sfb_offset_table[rightChan][0],
+			  nr_of_sfb[rightChan],
+			  &nok_lt_status[rightChan]);
+            }
+          } /* if(channelInfo[chanNum].ch_is_left) */
+        } /* if(channelInfo[chanNum].cpe) */
+        else
+	  nok_ltp_enc(spectral_line_vector[chanNum],
+		      nok_tmp_DTimeSigBuf[chanNum],
+		      block_type[chanNum],
+		      WS_SIN,
+		      &sfb_offset_table[chanNum][0],
+		      nr_of_sfb[chanNum],
+		      &nok_lt_status[chanNum]);
+      } /* if(channelInfo[chanNum].present... */
+      else
+        quantInfo[chanNum].ltpInfo->global_pred_flag = 0;
+    } /* for(chanNum... */
 
-		/******************************************/
-		/* Apply MS stereo                        */
-		/******************************************/
-		if (as->use_MS == 1) {
-			MSEncode(spectral_line_vector,
-				channelInfo,
-				sfb_offset_table,
-				block_type,
-				quantInfo,
-				max_ch);
-		} else if (as->use_MS == 0) {
-			MSEncodeSwitch(spectral_line_vector,
-				channelInfo,
-				sfb_offset_table,
-//				block_type,
-				quantInfo
-//				,max_ch
-                                );
-		}
+    /******************************************/
+    /* Apply MS stereo                        */
+    /******************************************/
+    if (as->use_MS == 1) {
+      MSEncode(spectral_line_vector,
+	       channelInfo,
+	       sfb_offset_table,
+	       block_type,
+	       quantInfo,
+	       max_ch);
+    }
+    else if (as->use_MS == 0) {
+      MSEncodeSwitch(spectral_line_vector,
+		     channelInfo,
+		     sfb_offset_table,
+//		     block_type,
+		     quantInfo
+//		     ,max_ch
+                     );
+    }
 
-		/************************************************/
-		/* Call the AAC quantization and coding module. */
-		/************************************************/
-		for (chanNum = 0; chanNum < max_ch; chanNum++) {
+    /************************************************/
+    /* Call the AAC quantization and coding module. */
+    /************************************************/
+    for (chanNum = 0; chanNum < max_ch; chanNum++) {
+      int bitsToUse;
+      bitsToUse = (int)((average_bits - used_bits)/max_ch);
+      bitsToUse += (int)(0.2*available_bitreservoir_bits/max_ch);
 
-			int bitsToUse;
-			bitsToUse = (int)((average_bits - used_bits)/max_ch);
-			bitsToUse += (int)(0.2*available_bitreservoir_bits/max_ch);
+      error = tf_encode_spectrum_aac(&spectral_line_vector[chanNum],
+                                     &p_ratio[chanNum],
+                                     &allowed_distortion[chanNum],
+                                     &energy[chanNum],
+                                     &block_type[chanNum],
+                                     &sfb_width_table[chanNum],
+//				     &nr_of_sfb[chanNum],
+                                     bitsToUse,
+//				     available_bitreservoir_bits,
+//				     padding_limit,
+				     fixed_stream,
+//				     NULL,
+//				     1,             /* nr of audio channels */
+				     &reconstructed_spectrum[chanNum],
+//				     useShortWindows,
+//				     aacAllowScalefacs,
+				     &quantInfo[chanNum],
+				     &(channelInfo[chanNum])
+//				     ,0/*no vbr*/,
+//				     ,bit_rate
+                                     );
+      if (error == FERROR)
+        return error;
+    }
 
-			error = tf_encode_spectrum_aac( &spectral_line_vector[chanNum],
-				&p_ratio[chanNum],
-				&allowed_distortion[chanNum],
-				&energy[chanNum],
-				&block_type[chanNum],
-				&sfb_width_table[chanNum],
-//				&nr_of_sfb[chanNum],
-				bitsToUse,
-//				available_bitreservoir_bits,
-//				padding_limit,
-				fixed_stream,
-//				NULL,
-//				1,                        /* nr of audio channels */
-				&reconstructed_spectrum[chanNum],
-//				useShortWindows,
-//				aacAllowScalefacs,
-				&quantInfo[chanNum],
-				&(channelInfo[chanNum])
-//				,0/*no vbr*/,
-//				,bit_rate
-                                );
-			if (error == FERROR)
-				return error;
-		}
+    /**********************************************************/
+    /* Reconstruct MS Stereo bands for prediction            */
+    /**********************************************************/
+    if (as->use_MS != -1) {
+      MSReconstruct(reconstructed_spectrum,
+		    channelInfo,
+		    sfb_offset_table,
+//		    block_type,
+		    quantInfo,
+		    max_ch);
+    }
 
-		/**********************************************************/
-		/* Reconstruct MS Stereo bands for prediction            */
-		/**********************************************************/
-		if (as->use_MS != -1) {
-			MSReconstruct(reconstructed_spectrum,
-				channelInfo,
-				sfb_offset_table,
-//				block_type,
-				quantInfo,
-				max_ch);
-		}
+    /**********************************************************/
+    /* Reconstruct Intensity Stereo bands for prediction     */
+    /**********************************************************/
+    if ((as->use_IS)&& (pns_sfb_start > 51)) {  /* do intensity only if pns is off  */
+      ISReconstruct(reconstructed_spectrum,
+		    channelInfo,
+		    sfb_offset_table,
+//		    block_type,
+		    quantInfo,
+		    max_ch);
+    }
 
-		/**********************************************************/
-		/* Reconstruct Intensity Stereo bands for prediction     */
-		/**********************************************************/
-		if ((as->use_IS)&& (pns_sfb_start > 51)) {  /* do intensity only if pns is off  */
-			ISReconstruct(reconstructed_spectrum,
-				channelInfo,
-				sfb_offset_table,
-//				block_type,
-				quantInfo,
-				max_ch);
-		}
+    /**********************************************************/
+    /* Update LTP history buffer                              */
+    /**********************************************************/
+    if(as->use_LTP)
+      for (chanNum=0;chanNum<max_ch;chanNum++) {
+        nok_ltp_reconstruct(reconstructed_spectrum[chanNum],
+			    block_type[chanNum],
+			    WS_SIN,
+			    &sfb_offset_table[chanNum][0],
+			    nr_of_sfb[chanNum],
+			    &nok_lt_status[chanNum]);
+      }
 
-		/**********************************************************/
-		/* Update LTP history buffer                              */
-		/**********************************************************/
-		if(as->use_LTP)
-			for (chanNum=0;chanNum<max_ch;chanNum++) {
-				nok_ltp_reconstruct(reconstructed_spectrum[chanNum],
-					block_type[chanNum],
-					WS_SIN,
-					&sfb_offset_table[chanNum][0],
-					nr_of_sfb[chanNum],
-					&nok_lt_status[chanNum]);
-			}
 
+    /**********************************/
+    /* Write out all encoded channels */
+    /**********************************/
+    used_bits = 0;
+    if (as->header_type==ADTS_HEADER)
+      used_bits += WriteADTSHeader(&quantInfo[0], fixed_stream, used_bits, 0);
 
-		/**********************************/
-		/* Write out all encoded channels */
-		/**********************************/
-		used_bits = 0;
-		if (as->header_type==ADTS_HEADER)
-			used_bits += WriteADTSHeader(&quantInfo[0], fixed_stream, used_bits, 0);
+    for (chanNum=0;chanNum<max_ch;chanNum++) {
+      if (channelInfo[chanNum].present) {
+        /* Write out a single_channel_element */
+        if (!channelInfo[chanNum].cpe) {
+	  /* Write out sce */ /* BugFix by YT  '+=' sould be '=' */
+          used_bits += WriteSCE(&quantInfo[chanNum],   /* Quantization information */
+			        channelInfo[chanNum].tag,
+			        fixed_stream,           /* Bitstream */
+			        0);                     /* Write flag, 1 means write */
+        }
+        else {
+	  if (channelInfo[chanNum].ch_is_left) {
+	    /* Write out cpe */
+	    used_bits += WriteCPE(&quantInfo[chanNum],   /* Quantization information,left */
+				  &quantInfo[channelInfo[chanNum].paired_ch],   /* Right */
+				  channelInfo[chanNum].tag,
+				  channelInfo[chanNum].common_window,    /* common window */
+				  &(channelInfo[chanNum].ms_info),
+				  fixed_stream,           /* Bitstream */
+				  0);                     /* Write flag, 1 means write */
+          }
+        }  /* if (!channelInfo[chanNum].cpe)  else */
+      } /* if (chann...*/
+    } /* for (chanNum...*/
 
-		for (chanNum=0;chanNum<max_ch;chanNum++) {
-			if (channelInfo[chanNum].present) {
-				/* Write out a single_channel_element */
-				if (!channelInfo[chanNum].cpe) {
-					/* Write out sce */ /* BugFix by YT  '+=' sould be '=' */
-					used_bits += WriteSCE(&quantInfo[chanNum],   /* Quantization information */
-						channelInfo[chanNum].tag,
-						fixed_stream,           /* Bitstream */
-						0);                     /* Write flag, 1 means write */
-				} else {
-					if (channelInfo[chanNum].ch_is_left) {
-						/* Write out cpe */
-						used_bits += WriteCPE(&quantInfo[chanNum],   /* Quantization information,left */
-							&quantInfo[channelInfo[chanNum].paired_ch],   /* Right */
-							channelInfo[chanNum].tag,
-							channelInfo[chanNum].common_window,    /* common window */
-							&(channelInfo[chanNum].ms_info),
-							fixed_stream,           /* Bitstream */
-							0);                     /* Write flag, 1 means write */
-					}
-				}  /* if (!channelInfo[chanNum].cpe)  else */
-			} /* if (chann...*/
-		} /* for (chanNum...*/
+    orig_used_bits = used_bits;
 
-		orig_used_bits = used_bits;
+    /* Compute how many fill bits are needed to avoid overflowing bit reservoir */
+    /* Save room for ID_END terminator */
+    if (used_bits < (8 - LEN_SE_ID) ) {
+      numFillBits = 8 - LEN_SE_ID - used_bits;
+    }
+    else {
+      numFillBits = 0;
+    }
 
-		/* Compute how many fill bits are needed to avoid overflowing bit reservoir */
-		/* Save room for ID_END terminator */
-		if (used_bits < (8 - LEN_SE_ID) ) {
-			numFillBits = 8 - LEN_SE_ID - used_bits;
-		} else {
-			numFillBits = 0;
-		}
+    /* Write AAC fill_elements, smallest fill element is 7 bits. */
+    /* Function may leave up to 6 bits left after fill, so tell it to fill a few extra */
+    numFillBits += 6;
+    bitsLeftAfterFill=WriteAACFillBits(fixed_stream,numFillBits, 0);
+    used_bits += (numFillBits - bitsLeftAfterFill);
 
-		/* Write AAC fill_elements, smallest fill element is 7 bits. */
-		/* Function may leave up to 6 bits left after fill, so tell it to fill a few extra */
-		numFillBits += 6;
-		bitsLeftAfterFill=WriteAACFillBits(fixed_stream,numFillBits, 0);
-		used_bits += (numFillBits - bitsLeftAfterFill);
+    /* Write ID_END terminator */
+    used_bits += LEN_SE_ID;
 
-		/* Write ID_END terminator */
-		used_bits += LEN_SE_ID;
-		
-		/* Now byte align the bitstream */
-		used_bits += ByteAlign(fixed_stream, 0);
+    /* Now byte align the bitstream */
+    used_bits += ByteAlign(fixed_stream, 0);
 
-		if (as->header_type==ADTS_HEADER)
-			WriteADTSHeader(&quantInfo[0], fixed_stream, used_bits, 1);
+    if (as->header_type==ADTS_HEADER)
+      WriteADTSHeader(&quantInfo[0], fixed_stream, used_bits, 1);
 
-		for (chanNum=0;chanNum<max_ch;chanNum++) {
-			if (channelInfo[chanNum].present) {
-				/* Write out a single_channel_element */
-				if (!channelInfo[chanNum].cpe) {
-					/* Write out sce */ /* BugFix by YT  '+=' sould be '=' */
-					WriteSCE(&quantInfo[chanNum],   /* Quantization information */
-						channelInfo[chanNum].tag,
-						fixed_stream,           /* Bitstream */
-						1);                     /* Write flag, 1 means write */
-				} else {
-					if (channelInfo[chanNum].ch_is_left) {
-						/* Write out cpe */
-						WriteCPE(&quantInfo[chanNum],   /* Quantization information,left */
-							&quantInfo[channelInfo[chanNum].paired_ch],   /* Right */
-							channelInfo[chanNum].tag,
-							channelInfo[chanNum].common_window,    /* common window */
-							&(channelInfo[chanNum].ms_info),
-							fixed_stream,           /* Bitstream */
-							1);                     /* Write flag, 1 means write */
-					}
-				}  /* if (!channelInfo[chanNum].cpe)  else */
-			} /* if (chann...*/
-		} /* for (chanNum...*/
+    for (chanNum=0;chanNum<max_ch;chanNum++) {
+      if (channelInfo[chanNum].present) {
+        /* Write out a single_channel_element */
+        if (!channelInfo[chanNum].cpe) {
+          /* Write out sce */ /* BugFix by YT  '+=' sould be '=' */
+	  WriteSCE(&quantInfo[chanNum],   /* Quantization information */
+		   channelInfo[chanNum].tag,
+		   fixed_stream,           /* Bitstream */
+		   1);                     /* Write flag, 1 means write */
+        }
+        else {
+       	  if (channelInfo[chanNum].ch_is_left) {
+	    /* Write out cpe */
+	    WriteCPE(&quantInfo[chanNum],   /* Quantization information,left */
+		     &quantInfo[channelInfo[chanNum].paired_ch],   /* Right */
+		     channelInfo[chanNum].tag,
+		     channelInfo[chanNum].common_window,    /* common window */
+		     &(channelInfo[chanNum].ms_info),
+		     fixed_stream,           /* Bitstream */
+		     1);                     /* Write flag, 1 means write */
+          }
+        }  /* if (!channelInfo[chanNum].cpe)  else */
+      } /* if (chann...*/
+    } /* for (chanNum...*/
 
-		/* Compute how many fill bits are needed to avoid overflowing bit reservoir */
-		/* Save room for ID_END terminator */
-		if (orig_used_bits < (8 - LEN_SE_ID) ) {
-			numFillBits = 8 - LEN_SE_ID - used_bits;
-		} else {
-			numFillBits = 0;
-		}
+    /* Compute how many fill bits are needed to avoid overflowing bit reservoir */
+    /* Save room for ID_END terminator */
+    if (orig_used_bits < (8 - LEN_SE_ID) ) {
+    numFillBits = 8 - LEN_SE_ID - used_bits;
+    }
+    else {
+      numFillBits = 0;
+    }
 
-		/* Write AAC fill_elements, smallest fill element is 7 bits. */
-		/* Function may leave up to 6 bits left after fill, so tell it to fill a few extra */
-		numFillBits += 6;
-		bitsLeftAfterFill=WriteAACFillBits(fixed_stream,numFillBits, 1);
+    /* Write AAC fill_elements, smallest fill element is 7 bits. */
+    /* Function may leave up to 6 bits left after fill, so tell it to fill a few extra */
+    numFillBits += 6;
+    bitsLeftAfterFill=WriteAACFillBits(fixed_stream,numFillBits, 1);
 
-		/* Write ID_END terminator */
-		BsPutBit(fixed_stream,ID_END,LEN_SE_ID);
-		
-		/* Now byte align the bitstream */
-		ByteAlign(fixed_stream, 1);
+    /* Write ID_END terminator */
+    BsPutBit(fixed_stream,ID_END,LEN_SE_ID);
 
-	} /* Quantization and coding block */
-	return FNO_ERROR;
+    /* Now byte align the bitstream */
+    ByteAlign(fixed_stream, 1);
+
+  } /* End of quantization and coding */
+  return FNO_ERROR;
 }