home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Sound / LAME / Source / formatBitstream.c < prev    next >
C/C++ Source or Header  |  1999-05-15  |  15KB  |  558 lines

  1. /*********************************************************************
  2.   Copyright (c) 1995 ISO/IEC JTC1 SC29 WG1, All Rights Reserved
  3.   formatBitstream.c
  4. **********************************************************************/
  5. /*
  6.   Revision History:
  7.  
  8.   Date        Programmer                Comment
  9.   ==========  ========================= ===============================
  10.   1995/09/06  mc@fivebats.com           created
  11.   1995/09/18  mc@fivebats.com           bugfix: WriteMainDataBits
  12.   1995/09/20  mc@fivebats.com           bugfix: store_side_info
  13. */
  14.  
  15. #include "formatBitstream.h"
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <assert.h>
  19.  
  20. #ifndef EXIT_FAILURE
  21. #define EXIT_FAILURE 1
  22. #endif
  23.  
  24. /* globals */
  25. static int BitCount       = 0;
  26. static int ThisFrameSize  = 0;
  27. static int BitsRemaining  = 0;
  28. static BitsFcnPtr PutBits = NULL;
  29.  
  30. void InitFormatBitStream(void)
  31. {
  32.     BitCount        = 0;
  33.     ThisFrameSize    = 0;
  34.     BitsRemaining    = 0;
  35.     PutBits            = NULL;
  36. }
  37.  
  38. /* forward declarations */
  39. static int store_side_info( BF_FrameData *frameInfo );
  40. static int main_data( BF_FrameData *frameInfo, BF_FrameResults *results );
  41. static int side_queue_elements( int *forwardFrameLength, int *forwardSILength );
  42. static void free_side_queues();
  43. static void WriteMainDataBits( unsigned val,
  44.                                unsigned nbits,
  45.                    BF_FrameResults *results );
  46. /*
  47.   BitStreamFrame is the public interface to the bitstream
  48.   formatting package. It writes one frame of main data per call.
  49.  
  50.   Assumptions:
  51.   - The back pointer is zero on the first call
  52.   - An integral number of bytes is written each frame
  53.  
  54.   You should be able to change the frame length, side info
  55.   length, #channels, #granules on a frame-by-frame basis.
  56.  
  57.   See formatBitstream.h for more information about the data
  58.   structures and the bitstream syntax.
  59. */
  60. static int elements, forwardFrameLength, forwardSILength; 
  61. void
  62. BF_BitstreamFrame( BF_FrameData *frameInfo, BF_FrameResults *results )
  63. {
  64.   /*    int elements, forwardFrameLength, forwardSILength; */
  65.  
  66.     assert( frameInfo->nGranules <= MAX_GRANULES );
  67.     assert( frameInfo->nChannels <= MAX_CHANNELS );
  68.  
  69.     /* get ptr to bit writing function */
  70.     PutBits = frameInfo->putbits;
  71.     assert( PutBits );
  72.     /* save SI and compute its length */
  73.     results->SILength = store_side_info( frameInfo );
  74.  
  75.     /* write the main data, inserting SI to maintain framing */
  76.     results->mainDataLength = main_data( frameInfo, results );
  77.  
  78.     /*
  79.       Caller must ensure that back SI and main data are
  80.       an integral number of bytes, since the back pointer
  81.       can only point to a byte boundary and this code
  82.       does not add stuffing bits
  83.     */
  84.     assert( (BitsRemaining % 8) == 0 );
  85.  
  86.     /* calculate nextBackPointer */
  87.     elements = side_queue_elements( &forwardFrameLength, &forwardSILength );
  88.     results->nextBackPtr = (BitsRemaining / 8) + (forwardFrameLength / 8) - (forwardSILength / 8);
  89. }
  90.  
  91. /*
  92.   FlushBitstream writes zeros into main data
  93.   until all queued headers are written. The
  94.   queue data buffers are also freed.
  95. */
  96. void
  97. BF_FlushBitstream( BF_FrameData *frameInfo, BF_FrameResults *results )
  98. {
  99.   /*    int elements, forwardFrameLength, forwardSILength; */
  100.  
  101.     /* get ptr to bit writing function */
  102.     PutBits = frameInfo->putbits;
  103.     assert( PutBits );
  104.  
  105.     if ( elements )
  106.     {
  107.       int bitsRemaining = forwardFrameLength - forwardSILength;
  108.       int wordsRemaining = bitsRemaining / 32;
  109.       while ( wordsRemaining-- ) {
  110.     WriteMainDataBits( 0, 32, results );
  111.       }
  112.       WriteMainDataBits( 0, (bitsRemaining % 32), results );    
  113.     }
  114.     
  115.  
  116.     results->mainDataLength = forwardFrameLength - forwardSILength;
  117.     results->SILength       = forwardSILength;
  118.     results->nextBackPtr    = 0;
  119.  
  120.     /* reclaim queue space */
  121.     free_side_queues();
  122.  
  123.     /* reinitialize globals */
  124.     BitCount       = 0;
  125.     ThisFrameSize  = 0;
  126.     BitsRemaining  = 0;    
  127.     PutBits        = NULL;
  128.     return;
  129. }
  130.  
  131. int
  132. BF_PartLength( BF_BitstreamPart *part )
  133. {
  134.     BF_BitstreamElement *ep = part->element;
  135.     int i, bits;
  136.  
  137.     bits = 0;
  138.     for ( i = 0; i < part->nrEntries; i++, ep++ )
  139.     bits += ep->length;
  140.     return bits;
  141. }
  142.  
  143.  
  144. /*
  145.   The following is all private to this file
  146. */
  147.  
  148. typedef struct
  149. {
  150.     int frameLength;
  151.     int SILength;
  152.     int nGranules;
  153.     int nChannels;
  154.     BF_PartHolder *headerPH;
  155.     BF_PartHolder *frameSIPH;
  156.     BF_PartHolder *channelSIPH[MAX_CHANNELS];
  157.     BF_PartHolder *spectrumSIPH[MAX_GRANULES][MAX_CHANNELS];
  158. } MYSideInfo;
  159.  
  160. static MYSideInfo *get_side_info();
  161. static int write_side_info();
  162. typedef int (*PartWriteFcnPtr)( BF_BitstreamPart *part, BF_FrameResults *results );
  163.  
  164.  
  165. static int
  166. writePartMainData( BF_BitstreamPart *part, BF_FrameResults *results )
  167. {
  168.     BF_BitstreamElement *ep;
  169.     int i, bits;
  170.  
  171.     assert( results );
  172.     assert( part );
  173.  
  174.     bits = 0;
  175.     ep = part->element;
  176.     for ( i = 0; i < part->nrEntries; i++, ep++ )
  177.     {
  178.     WriteMainDataBits( ep->value, ep->length, results );
  179.     bits += ep->length;
  180.     }
  181.     return bits;
  182. }
  183.  
  184. static int
  185. writePartSideInfo( BF_BitstreamPart *part, BF_FrameResults *results )
  186. {
  187.     BF_BitstreamElement *ep;
  188.     int i, bits;
  189.  
  190.     assert( part );
  191.  
  192.     bits = 0;
  193.     ep = part->element;
  194.     for ( i = 0; i < part->nrEntries; i++, ep++ )
  195.     {
  196.     (*PutBits)( ep->value, ep->length );
  197.     bits += ep->length;
  198.     }
  199.     return bits;
  200. }
  201.  
  202. static int
  203. main_data( BF_FrameData *fi, BF_FrameResults *results )
  204. {
  205.     int gr, ch, bits;
  206.     PartWriteFcnPtr wp = writePartMainData;
  207.     bits = 0;
  208.     results->mainDataLength = 0;
  209.  
  210.     for ( gr = 0; gr < fi->nGranules; gr++ )
  211.     for ( ch = 0; ch < fi->nChannels; ch++ )
  212.     {
  213.         bits += (*wp)( fi->scaleFactors[gr][ch], results );
  214.         bits += (*wp)( fi->codedData[gr][ch],    results );
  215.         bits += (*wp)( fi->userSpectrum[gr][ch], results );
  216.     }
  217.     bits += (*wp)( fi->userFrameData, results );
  218.     return bits;
  219. }
  220.  
  221. /*
  222.   This is a wrapper around PutBits() that makes sure that the
  223.   framing header and side info are inserted at the proper
  224.   locations
  225. */
  226.  
  227. static void
  228. WriteMainDataBits( unsigned val,
  229.            unsigned nbits,
  230.            BF_FrameResults *results )
  231. {
  232.     assert( nbits <= 32 );
  233.     if ( BitCount == ThisFrameSize )
  234.     {
  235.     BitCount = write_side_info();
  236.     BitsRemaining = ThisFrameSize - BitCount;
  237.     }
  238.     if ( nbits == 0 )
  239.     return;
  240.     if ( nbits > BitsRemaining )
  241.     {
  242.     unsigned extra = val >> (nbits - BitsRemaining);
  243.     nbits -= BitsRemaining;
  244.     (*PutBits)( extra, BitsRemaining );
  245.     BitCount = write_side_info();
  246.     BitsRemaining = ThisFrameSize - BitCount;
  247.     (*PutBits)( val, nbits );
  248.     }
  249.     else
  250.     (*PutBits)( val, nbits );
  251.     BitCount += nbits;
  252.     BitsRemaining -= nbits;
  253.     assert( BitCount <= ThisFrameSize );
  254.     assert( BitsRemaining >= 0 );
  255.     assert( (BitCount + BitsRemaining) == ThisFrameSize );
  256. }
  257.  
  258.  
  259. static int
  260. write_side_info()
  261. {
  262.     MYSideInfo *si;
  263.     int bits, ch, gr;
  264.     PartWriteFcnPtr wp = writePartSideInfo;
  265.  
  266.     bits = 0;
  267.     si = get_side_info();
  268.     ThisFrameSize = si->frameLength;
  269.     bits += (*wp)( si->headerPH->part,  NULL );
  270.     bits += (*wp)( si->frameSIPH->part, NULL );
  271.  
  272.     for ( ch = 0; ch < si->nChannels; ch++ )
  273.     bits += (*wp)( si->channelSIPH[ch]->part, NULL );
  274.  
  275.     for ( gr = 0; gr < si->nGranules; gr++ )
  276.     for ( ch = 0; ch < si->nChannels; ch++ )
  277.         bits += (*wp)( si->spectrumSIPH[gr][ch]->part, NULL );
  278.     return bits;
  279. }
  280.  
  281. typedef struct side_info_link
  282. {
  283.     struct side_info_link *next;
  284.     MYSideInfo           side_info;
  285. } side_info_link;
  286.  
  287. static struct side_info_link *side_queue_head   = NULL;
  288. static struct side_info_link *side_queue_free   = NULL;
  289.  
  290. static void free_side_info_link( side_info_link *l );
  291.  
  292. static int
  293. side_queue_elements( int *frameLength, int *SILength )
  294. {
  295.     int elements = 0;
  296.     side_info_link *l = side_queue_head;
  297.  
  298.     *frameLength = 0;
  299.     *SILength    = 0;
  300.  
  301.     for ( l = side_queue_head; l; l = l->next )
  302.     {
  303.     elements++;
  304.     *frameLength += l->side_info.frameLength;
  305.     *SILength    += l->side_info.SILength;
  306.     }
  307.     return elements;
  308. }
  309.  
  310. static int
  311. store_side_info( BF_FrameData *info )
  312. {
  313.     int ch, gr;
  314.     side_info_link *l = NULL;
  315.     /* obtain a side_info_link to store info */
  316.     side_info_link *f = side_queue_free;
  317.     int bits = 0;
  318.  
  319.     if ( f == NULL )
  320.     { /* must allocate another */
  321. #ifdef DEBUG
  322.     static int n_si = 0;
  323.     n_si += 1;
  324.     fprintf( stderr, "allocating side_info_link number %d\n", n_si );
  325. #endif
  326.     l = (side_info_link *) calloc( 1, sizeof(side_info_link) );
  327.     if ( l == NULL )
  328.     {
  329.         fprintf( stderr, "cannot allocate side_info_link" );
  330.         exit( EXIT_FAILURE );
  331.     }
  332.     l->next = NULL;
  333.     l->side_info.headerPH  = BF_newPartHolder( info->header->nrEntries );
  334.     l->side_info.frameSIPH = BF_newPartHolder( info->frameSI->nrEntries );
  335.     for ( ch = 0; ch < info->nChannels; ch++ )
  336.         l->side_info.channelSIPH[ch] = BF_newPartHolder( info->channelSI[ch]->nrEntries );
  337.     for ( gr = 0; gr < info->nGranules; gr++ )
  338.         for ( ch = 0; ch < info->nChannels; ch++ )
  339.         l->side_info.spectrumSIPH[gr][ch] = BF_newPartHolder( info->spectrumSI[gr][ch]->nrEntries );
  340.     
  341.     }
  342.     else
  343.     { /* remove from the free list */
  344.     side_queue_free = f->next;
  345.     f->next = NULL;
  346.     l = f;
  347.     }
  348.     /* copy data */
  349.     l->side_info.frameLength = info->frameLength;
  350.     l->side_info.nGranules   = info->nGranules;
  351.     l->side_info.nChannels   = info->nChannels;
  352.     l->side_info.headerPH    = BF_LoadHolderFromBitstreamPart( l->side_info.headerPH,  info->header );
  353.     l->side_info.frameSIPH   = BF_LoadHolderFromBitstreamPart( l->side_info.frameSIPH, info->frameSI );
  354.  
  355.     bits += BF_PartLength( info->header );
  356.     bits += BF_PartLength( info->frameSI );
  357.  
  358.     for ( ch = 0; ch < info->nChannels; ch++ )
  359.     {
  360.     l->side_info.channelSIPH[ch] = BF_LoadHolderFromBitstreamPart( l->side_info.channelSIPH[ch],
  361.                                        info->channelSI[ch] );
  362.     bits += BF_PartLength( info->channelSI[ch] );
  363.     }
  364.  
  365.     for ( gr = 0; gr < info->nGranules; gr++ )
  366.     for ( ch = 0; ch < info->nChannels; ch++ )
  367.     {
  368.         l->side_info.spectrumSIPH[gr][ch] = BF_LoadHolderFromBitstreamPart( l->side_info.spectrumSIPH[gr][ch],
  369.                                         info->spectrumSI[gr][ch] );
  370.         bits += BF_PartLength( info->spectrumSI[gr][ch] );
  371.     }
  372.     l->side_info.SILength = bits;
  373.     /* place at end of queue */
  374.     f = side_queue_head;
  375.     if ( f == NULL )
  376.     {  /* empty queue */
  377.     side_queue_head = l;
  378.     }
  379.     else
  380.     { /* find last element */
  381.     while ( f->next )
  382.         f = f->next;
  383.     f->next = l;
  384.     }
  385.     return bits;
  386. }
  387.  
  388. static MYSideInfo*
  389. get_side_info()
  390. {
  391.     side_info_link *f = side_queue_free;
  392.     side_info_link *l = side_queue_head;
  393.     
  394.     /*
  395.       If we stop here it means you didn't provide enough
  396.       headers to support the amount of main data that was
  397.       written.
  398.     */
  399.     assert( l );
  400.     
  401.     /* update queue head */
  402.     side_queue_head = l->next;
  403.  
  404.     /*
  405.       Append l to the free list. You can continue
  406.       to use it until store_side_info is called
  407.       again, which will not happen again for this
  408.       frame.
  409.     */
  410.     side_queue_free = l;
  411.     l->next = f;
  412.     return &l->side_info;
  413. }
  414.  
  415. static void
  416. free_side_queues()
  417. {
  418.     side_info_link *l, *next;
  419.     
  420.     for ( l = side_queue_head; l; l = next )
  421.     {
  422.     next = l->next;
  423.     free_side_info_link( l );
  424.     }
  425.     side_queue_head = NULL;
  426.  
  427.     for ( l = side_queue_free; l; l = next )
  428.     {
  429.     next = l->next;
  430.     free_side_info_link( l );
  431.     }
  432.     side_queue_free = NULL;
  433. }
  434.  
  435. static void
  436. free_side_info_link( side_info_link *l )
  437. {
  438.     int gr, ch;
  439.  
  440.     l->side_info.headerPH  = BF_freePartHolder( l->side_info.headerPH );
  441.     l->side_info.frameSIPH = BF_freePartHolder( l->side_info.frameSIPH );
  442.  
  443.     for ( ch = 0; ch < l->side_info.nChannels; ch++ )
  444.     l->side_info.channelSIPH[ch] = BF_freePartHolder( l->side_info.channelSIPH[ch] );
  445.  
  446.     for ( gr = 0; gr < l->side_info.nGranules; gr++ )
  447.     for ( ch = 0; ch < l->side_info.nChannels; ch++ )
  448.         l->side_info.spectrumSIPH[gr][ch] = BF_freePartHolder( l->side_info.spectrumSIPH[gr][ch] );
  449.  
  450.     free( l );
  451. }
  452. /*
  453.   Allocate a new holder of a given size
  454. */
  455. BF_PartHolder *BF_newPartHolder( int max_elements )
  456. {
  457.     BF_PartHolder *newPH    = calloc( 1, sizeof(BF_PartHolder) );
  458.     assert( newPH );
  459.     newPH->max_elements  = max_elements;
  460.     newPH->part          = calloc( 1, sizeof(BF_BitstreamPart) );
  461.     assert( newPH->part );
  462.     newPH->part->element = calloc( max_elements, sizeof(BF_BitstreamElement) );
  463.     assert( newPH->part->element );
  464.     newPH->part->nrEntries = 0;
  465.     return newPH;
  466. }
  467.  
  468. BF_PartHolder *BF_NewHolderFromBitstreamPart( BF_BitstreamPart *thePart )
  469. {
  470.     BF_PartHolder *newHolder = BF_newPartHolder( thePart->nrEntries );
  471.     return BF_LoadHolderFromBitstreamPart( newHolder, thePart );
  472. }
  473.  
  474. BF_PartHolder *BF_LoadHolderFromBitstreamPart( BF_PartHolder *theHolder, BF_BitstreamPart *thePart )
  475. {
  476.     BF_BitstreamElement *pElem;
  477.     int i;
  478.  
  479.     theHolder->part->nrEntries = 0;
  480.     for ( i = 0; i < thePart->nrEntries; i++ )
  481.     {
  482.     pElem = &(thePart->element[i]);
  483.     theHolder = BF_addElement( theHolder, pElem );
  484.     }
  485.     return theHolder;
  486. }
  487.  
  488. /*
  489.   Grow or shrink a part holder. Always creates a new
  490.   one of the right length and frees the old one after
  491.   copying the data.
  492. */
  493. BF_PartHolder *BF_resizePartHolder( BF_PartHolder *oldPH, int max_elements )
  494. {
  495.     int elems, i;
  496.     BF_PartHolder *newPH;
  497.  
  498. #ifdef DEBUG
  499.     fprintf( stderr, "Resizing part holder from %d to %d\n",
  500.          oldPH->max_elements, max_elements );
  501. #endif
  502.     /* create new holder of the right length */
  503.     newPH = BF_newPartHolder( max_elements );
  504.  
  505.     /* copy values from old to new */
  506.     elems = (oldPH->max_elements > max_elements) ? max_elements : oldPH->max_elements;
  507.     newPH->part->nrEntries = elems;
  508.     for ( i = 0; i < elems; i++ )
  509.     newPH->part->element[i] = oldPH->part->element[i];
  510.  
  511.     /* free old holder */
  512.     BF_freePartHolder( oldPH );
  513.     
  514.     return newPH;
  515. }
  516.  
  517. BF_PartHolder *BF_freePartHolder( BF_PartHolder *thePH )
  518. {
  519.     free( thePH->part->element );
  520.     free( thePH->part );
  521.     free( thePH );
  522.     return NULL;
  523. }
  524.  
  525. /*
  526.   Add theElement to thePH, growing the holder if
  527.   necessary. Returns ptr to the holder, which may
  528.   not be the one you called it with!
  529. */
  530. BF_PartHolder *BF_addElement( BF_PartHolder *thePH, BF_BitstreamElement *theElement )
  531. {
  532.     BF_PartHolder *retPH = thePH;
  533.     int needed_entries = thePH->part->nrEntries + 1;
  534.     int extraPad = 8;  /* add this many more if we need to resize */
  535.  
  536.     /* grow if necessary */
  537.     if ( needed_entries > thePH->max_elements )
  538.     retPH = BF_resizePartHolder( thePH, needed_entries + extraPad );
  539.  
  540.     /* copy the data */
  541.     retPH->part->element[retPH->part->nrEntries++] = *theElement;
  542.     return retPH;
  543. }
  544.  
  545. /*
  546.   Add a bit value and length to the element list in thePH
  547. */
  548. BF_PartHolder *BF_addEntry( BF_PartHolder *thePH, uint32 value, uint16 length )
  549. {
  550.     BF_BitstreamElement myElement;
  551.     myElement.value  = value;
  552.     myElement.length = length;
  553.     if ( length )
  554.     return BF_addElement( thePH, &myElement );
  555.     else
  556.     return thePH;
  557. }
  558.