shithub: aacenc

Download patch

ref: 8c68332f1a22aae7acf97a9118181a780448d609
parent: bbaf76df3156fd997e654576dc37b925d27e4155
author: menno <menno>
date: Tue Feb 15 12:03:22 EST 2000

Added ADTS header writing

--- a/aac_qc.c
+++ b/aac_qc.c
@@ -609,7 +609,7 @@
 	/* initialize the scale_factors that aren't intensity stereo bands */
 	is_info=&(ch_info->is_info);
 	for(k=0; k< quantInfo -> nr_of_sfb ;k++) {
-		scale_factor[k]=((is_info->is_present)&&(is_info->is_used[k])) ? scale_factor[k] : 0/*min(15,(int)(1.0/SigMaskRatio[k]+0.5))*/;
+		scale_factor[k]=((is_info->is_present)&&(is_info->is_used[k])) ? scale_factor[k] : 0/*min(5,(int)(1.0/SigMaskRatio[k]+0.5))*/;
 	}
 
 	/* Mark IS bands by setting book_vector to INTENSITY_HCB */
@@ -756,9 +756,6 @@
 					notdone = 1;
 				}
 			}
-		}
-
-		if (notdone) {
 			for (sb = 0; sb < quantInfo->nr_of_sfb; sb++) {
 				if (scale_factor[sb] > 59)
 					notdone = 0;
--- a/aac_qc.h
+++ b/aac_qc.h
@@ -67,6 +67,8 @@
   NOK_LT_PRED_STATUS *ltpInfo;          /* Prt to LTP data */
   int pns_sfb_nrg[SFB_NUM_MAX];
   int pns_sfb_flag[SFB_NUM_MAX];
+  int profile;
+  int srate_idx;
 } AACQuantInfo;
 
 
--- a/aac_se_enc.c
+++ b/aac_se_enc.c
@@ -30,74 +30,114 @@
 /* Return number of bits left over (less than 7 ).                           */
 /*****************************************************************************/
 int WriteAACFillBits(BsBitStream* ptrBs,  /* Pointer to bitstream */
-		     int numBits)         /* Number of bits needed to fill */
+		     int numBits,         /* Number of bits needed to fill */
+			 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;
 
-    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;
-      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;
-      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;
-      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 */
+{
+	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;
+}
+
+
 /*****************************************************************************/
 /* 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;
 }
 
 /*****************************************************************************/
@@ -104,27 +144,27 @@
 /* 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;
+	
+	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;
+		/* 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;
 }
 
 
@@ -133,56 +173,56 @@
 /*****************************************************************************/
 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);
+	}
+	
+	bit_count += LEN_SE_ID;
+	bit_count += LEN_TAG;
+	bit_count += 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);
-	  }
+	/* 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;
+		bit_count += (ms_info->is_present==1)*numWindows*maxSfb*LEN_MASK;
 	}
-      }
-    }
-    bit_count += LEN_MASK_PRES;
-    bit_count += (ms_info->is_present==1)*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;
+	
+	/* Write individual_channel_stream elements */
+	bit_count += WriteICS(quantInfoL,commonWindow,fixedStream,writeFlag);
+	bit_count += WriteICS(quantInfoR,commonWindow,fixedStream,writeFlag);
+	
+	return bit_count;
 }
 
 
@@ -190,45 +230,45 @@
 /* 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);
+	/* Write section_data() information to the bitstream */
+	bit_count += sort_book_numbers(quantInfo,output_book_vector,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 TNS data */
+	bit_count += WriteTNSData(quantInfo,fixed_stream,writeFlag);
 	
-  /* Write gain control data */
-  bit_count += WriteGainControlData(quantInfo,fixed_stream,writeFlag);
+	/* Write gain control data */
+	bit_count += WriteGainControlData(quantInfo,fixed_stream,writeFlag);
 
-  /* Write out spectral_data() */
-  bit_count += WriteSpectralData(quantInfo,fixed_stream,writeFlag);
+	/* Write out spectral_data() */
+	bit_count += WriteSpectralData(quantInfo,fixed_stream,writeFlag);
 
-  /* Return number of bits */
-  return(bit_count);
+	/* Return number of bits */
+	return(bit_count);
 }
 
 
--- a/aac_se_enc.h
+++ b/aac_se_enc.h
@@ -54,7 +54,13 @@
 /* Write AAC fill bits to the bitStream                                      */
 /*****************************************************************************/
 int WriteAACFillBits(BsBitStream* ptrBs,  /* Pointer to bit stream */
-		     int numBits);        /* Number of bits neede to fill */
+		     int numBits,        /* Number of bits neede to fill */
+				int writeFlag);
+
+int WriteADTSHeader(AACQuantInfo* quantInfo,   /* AACQuantInfo structure */
+					BsBitStream* fixedStream,  /* Pointer to bitstream */
+					int used_bits,
+					int writeFlag);            /* 1 means write, 0 means count only */
 
 /*****************************************************************************/
 /* WriteSCE(...), write a single-channel element to the bitstream.           */
--- a/bitstream.c
+++ b/bitstream.c
@@ -8,12 +8,7 @@
 #include "bitstream.h"		/* bit stream module */
 
 
-#define BYTE_NUMBIT 8		/* bits in byte (char) */
-#define LONG_NUMBIT 32		/* bits in unsigned long */
-#define bit2byte(a) (((a)+BYTE_NUMBIT-1)/BYTE_NUMBIT)
-#define byte2bit(a) ((a)*BYTE_NUMBIT)
 
-
 BsBitStream *BsOpenWrite(int size)
 {
 	BsBitStream *bs;
@@ -83,7 +78,7 @@
 	return 0;
 }
 
-int ByteAlign(BsBitStream* ptrBs)
+int ByteAlign(BsBitStream* ptrBs, int writeFlag)
 {
 	int len, i,j;
 	len = BsBufferNumBit( ptrBs );
@@ -91,8 +86,10 @@
 	j = (8 - (len%8))%8;
 
 	if ((len % 8) == 0) j = 0;
-	for( i=0; i<j; i++ ) {
-		BsPutBit( ptrBs, 0, 1 ); 
+	if (writeFlag) {
+		for( i=0; i<j; i++ ) {
+			BsPutBit( ptrBs, 0, 1 ); 
+		}
 	}
 	return j;
 }
--- a/bitstream.h
+++ b/bitstream.h
@@ -9,6 +9,12 @@
 #define max(a,b) ( (a) > (b) ? (a) : (b) )
 #endif
 
+#define BYTE_NUMBIT 8		/* bits in byte (char) */
+#define LONG_NUMBIT 32		/* bits in unsigned long */
+#define bit2byte(a) (((a)+BYTE_NUMBIT-1)/BYTE_NUMBIT)
+#define byte2bit(a) ((a)*BYTE_NUMBIT)
+
+
 typedef struct _bitstream
 {
   unsigned char *data;		/* data bits */
@@ -34,7 +40,7 @@
 			  int numBit);
 
 /* ByteAlign(...), used to byte align bitstream */
-int ByteAlign(BsBitStream* ptrBs);
+int ByteAlign(BsBitStream* ptrBs, int writeFlag);
 
 #endif
 
--- a/enc_tf.c
+++ b/enc_tf.c
@@ -191,9 +191,14 @@
 		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();
+	initrft();
 }
 
 /*****************************************************************************************
@@ -554,6 +559,7 @@
 		int chanNum;   
 		int numFillBits;
 		int bitsLeftAfterFill;
+		int orig_used_bits;
 
 		/* bit budget */
 		num_bits_available = (long)(average_bits + available_bitreservoir_bits - used_bits);
@@ -754,30 +760,34 @@
 		/**********************************/
 		/* Write out all encoded channels */
 		/**********************************/
+		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 */
+					used_bits += WriteSCE(&quantInfo[chanNum],   /* Quantization information */
 						channelInfo[chanNum].tag,
 						fixed_stream,           /* Bitstream */
-						1);                     /* Write flag, 1 means write */
+						0);                     /* Write flag, 1 means write */
 				} else {
 					if (channelInfo[chanNum].ch_is_left) {
 						/* Write out cpe */
-						used_bits = WriteCPE(&quantInfo[chanNum],   /* Quantization information,left */
+						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 */
-							1);                     /* Write flag, 1 means write */
+							0);                     /* Write flag, 1 means write */
 					}
 				}  /* if (!channelInfo[chanNum].cpe)  else */
 			} /* if (chann...*/
 		} /* for (chanNum...*/
 
+		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) ) {
@@ -789,15 +799,59 @@
 		/* 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);
+		bitsLeftAfterFill=WriteAACFillBits(fixed_stream,numFillBits, 0);
 		used_bits += (numFillBits - bitsLeftAfterFill);
 
 		/* Write ID_END terminator */
-		BsPutBit(fixed_stream,ID_END,LEN_SE_ID);
 		used_bits += LEN_SE_ID;
 		
 		/* Now byte align the bitstream */
-		used_bits += ByteAlign(fixed_stream);
+		used_bits += ByteAlign(fixed_stream, 0);
+
+		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...*/
+
+		/* 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 ID_END terminator */
+		BsPutBit(fixed_stream,ID_END,LEN_SE_ID);
+		
+		/* Now byte align the bitstream */
+		ByteAlign(fixed_stream, 1);
 
 	} /* Quantization and coding block */
 	return FNO_ERROR;
--- a/encoder.c
+++ b/encoder.c
@@ -51,12 +51,15 @@
 		as->rc_needed = 1;
 	else
 		as->rc_needed = 0;
-
+#if 0
 	if (as->header_type==ADIF_HEADER) {
 		*headerSize = 17;
 	} else {
+#endif
 		*headerSize = 0;
+#if 0
 	}
+#endif
 
 	EncTfInit(as, 0);
 
@@ -248,6 +251,7 @@
 	if (as->rc_needed)
 		RateConvFree (as->rc_buf);
 
+#if 0
 	if (as->header_type==ADIF_HEADER)
 	{
 		int i;
@@ -299,7 +303,7 @@
 		BsPutBit(bitHeader,(as->channels == 2),1);
 		BsPutBit(bitHeader,0,4);
 
-		ByteAlign(bitHeader);
+		ByteAlign(bitHeader, 1);
 		// Comment
 		BsPutBit(bitHeader,0,8);
 
@@ -311,6 +315,7 @@
 			headerBuf[i] = bitHeader->data[i];
 		BsClose(bitHeader);
 	}
+#endif
 
 	for (ch=0; ch < as->channels; ch++)
 		if(as->inputBuffer[ch]) free(as->inputBuffer[ch]);