2 (c) Copyright 1998-2001 - Tord Jansson
3 ======================================
5 This file is part of the BladeEnc MP3 Encoder, based on
6 ISO's reference code for MPEG Layer 3 compression.
8 This file doesn't contain any of the ISO reference code and
9 is copyright Tord Jansson (tord.jansson@swipnet.se).
11 BladeEnc is free software; you can redistribute this file
12 and/or modify it under the terms of the GNU Lesser General Public
13 License as published by the Free Software Foundation; either
14 version 2.1 of the License, or (at your option) any later version.
18 ------------ Changes ------------
20 2000-12-05 Andre Piotrowski
22 - reformatted, slightly optimized
26 - use some explicit type casting to avoid compiler warnings
37 #include "formatbitstream2.h"
43 extern char *pEncodedOutput;
50 /*____ Structure Definitions _________________________________________________*/
52 typedef struct HeaderDef
57 struct HeaderDef *pNext;
64 /*____ Function Prototypes ___________________________________________________*/
66 static int writeMainDataBits (BF_FrameData *psFrame, BitHolder *psBH);
67 static void putbits (unsigned int val, int n);
68 static int generateHeader (BF_FrameData *psFrame);
74 /*____ Static Data ___________________________________________________________*/
76 static int BitsRemaining = 0;
77 static Header *pHeaderChain = NULL;
78 static Header *pFreeHeaderChain = NULL;
84 /*____ initFormatBitstream() ________________________________________________*/
86 void initFormatBitstream (void)
90 pFreeHeaderChain = NULL;
97 /*____ exitFormatBitstream() _________________________________________________*/
99 void exitFormatBitstream (void)
103 /* Fill out the last frame with 0xFF */
107 /* Dealocate Headers */
109 while (pHeaderChain != NULL)
111 psFree = pHeaderChain;
112 pHeaderChain = psFree->pNext;
116 while (pFreeHeaderChain != NULL)
118 psFree = pFreeHeaderChain;
119 pFreeHeaderChain = psFree->pNext;
128 /*____ flushFrame() _________________________________________________________*/
130 void flushFrame (void)
132 /* Fill out the last frame with 0xFF */
134 while (BitsRemaining > 32)
136 putbits (0xFFFFFFFF, 32);
140 if (BitsRemaining > 0)
141 putbits (0xFFFFFFFF, BitsRemaining);
150 /*____ initBitHolder() ______________________________________________________*/
152 BitHolder *initBitHolder
158 wp->element = (BitHolderElement *) malloc (sizeof(BitHolderElement) * elements);
159 wp->max_elements = elements;
169 /*____ exitBitHolder() ______________________________________________________*/
171 void exitBitHolder (BitHolder * wp)
173 mem_free ((void **)&wp->element);
175 if (wp->element != NULL)
187 /*____ addBits() ____________________________________________________________*/
198 if (wp->nrEntries == wp->max_elements)
200 printf ("ERROR: BitHolder overflow!\n");
204 value &= 0xFFFFFFFF >> (32-length);
206 wp->element[wp->nrEntries].value = value;
207 wp->element[wp->nrEntries].length = length;
216 /*____ writeFrame() _________________________________________________________*/
220 BF_FrameData *psFrame,
221 BF_FrameResults *results
227 int sizeRemainHeaders, sizeRemainFrames;
231 /* Generate and save header, return size of SideInfo.*/
233 results->SILength = generateHeader (psFrame);
236 /* Put the bits and compute size of mainData */
239 for (gr = 0; gr < psFrame->nGranules; gr++)
241 for (ch = 0; ch < psFrame->nChannels; ch++)
243 bits += writeMainDataBits (psFrame, &psFrame->scaleFactors[gr][ch]);
244 bits += writeMainDataBits (psFrame, &psFrame-> codedData[gr][ch]);
245 bits += writeMainDataBits (psFrame, &psFrame->userSpectrum[gr][ch]);
248 bits += writeMainDataBits (psFrame, &psFrame->userFrameData);
250 results->mainDataLength = bits;
253 /* calculate nextBackPointer */
255 sizeRemainHeaders = 0;
256 sizeRemainFrames = 0;
258 for (psHeader = pHeaderChain; psHeader != NULL; psHeader = psHeader->pNext)
260 sizeRemainHeaders += psHeader->size;
261 sizeRemainFrames += psHeader->frameSize;
265 results->nextBackPtr = (BitsRemaining / 8) + sizeRemainFrames - sizeRemainHeaders; /* BitsRemaining must be dividable by 8 */
272 /*=============================================================================
273 >>> Static Functions <<<
274 =============================================================================*/
280 /*____ writeBitHolder() _____________________________________________________*/
282 static void writeBitHolder (BitHolder *part)
284 BitHolderElement *ep;
288 for (i = 0; i < part->nrEntries; i++, ep++)
289 putbits (ep->value, ep->length);
296 /*____ calcCRC() ____________________________________________________________*/
310 for (i = 2; i < size; i++)
312 if (i != 4 && i != 5)
316 while (masking >>= 1)
318 carry = crc & 0x8000;
321 if ( (carry == 0) != ((data & masking) == 0) )
323 if (!carry ^ !(data & masking))
337 /*____ generateHeader() ____________________________________________________*/
339 static int generateHeader (BF_FrameData *psFrame)
345 char *pOldEncodedOutput;
348 /* Get a Free Header structure */
350 if( pFreeHeaderChain == NULL )
351 psHeader = (Header *) malloc( sizeof( Header ) );
354 psHeader = pFreeHeaderChain;
355 pFreeHeaderChain = psHeader->pNext;
357 psHeader->pNext = NULL;
358 for( wpLink = &pHeaderChain ; * wpLink != NULL ; wpLink = &((*wpLink)->pNext) )
359 {} /* avoid compiler warning */
363 /* Generate the Header */
365 pOldEncodedOutput = pEncodedOutput;
366 pEncodedOutput = psHeader->data;
367 pEncodedOutput[0] = 0; /* Need to be cleared since we OR in data... */
370 writeBitHolder (&psFrame->header);
371 writeBitHolder (&psFrame->frameSI);
373 for (ch = 0; ch < psFrame->nChannels; ch++)
374 writeBitHolder (&psFrame->channelSI[ch]);
376 for (gr = 0; gr < psFrame->nGranules; gr++)
377 for (ch = 0; ch < psFrame->nChannels; ch++)
378 writeBitHolder (&psFrame->spectrumSI[gr][ch]);
381 /* Checksum generation (if CRC enabled).*/
383 if (!(psHeader->data[1] & 0x1))
385 crc = calcCRC (psHeader->data, pEncodedOutput - psHeader->data);
386 psHeader->data[4] = (char) (crc >> 8);
387 psHeader->data[5] = (char) crc;
391 psHeader->size = pEncodedOutput - psHeader->data;
392 psHeader->frameSize = psFrame->frameLength / 8;
394 pEncodedOutput = pOldEncodedOutput;
397 return psHeader->size * 8;
404 /*____ writeHeader() _______________________________________________________*/
406 static int writeHeader (void)
410 psHeader = pHeaderChain;
411 memcpy (pEncodedOutput, psHeader->data, psHeader->size);
412 pEncodedOutput += psHeader->size;
415 pHeaderChain = psHeader->pNext;
416 psHeader->pNext = pFreeHeaderChain;
417 pFreeHeaderChain = psHeader;
419 return (psHeader->frameSize - psHeader->size) * 8;
426 /*____ writeMainDataBits() __________________________________________________*/
428 static int writeMainDataBits
430 BF_FrameData *psFrame, /* avoid compiler warning */
434 BitHolderElement *psElem = psBH->element;
439 for (i = 0; i < psBH->nrEntries; i++, psElem++)
442 nBits = psElem->length;
444 if (BitsRemaining == 0)
445 BitsRemaining = writeHeader ();
447 if (nBits > BitsRemaining)
449 nBits -= BitsRemaining;
450 putbits (val >> nBits, BitsRemaining);
451 BitsRemaining = writeHeader ();
454 putbits (val, nBits);
455 BitsRemaining -= nBits;
457 bits += psElem->length;
467 /*____ putbits() _____________________________________________________________*/
470 /*write n bits into the bit stream */
482 while (n >= outputBit)
486 *pEncodedOutput |= val >> n;
493 if (n > 0) /* n < outputBit */
497 *pEncodedOutput |= val << outputBit;