home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Sound / LAME / Source / reservoir.c < prev    next >
C/C++ Source or Header  |  1999-06-03  |  7KB  |  287 lines

  1. /**********************************************************************
  2.  * ISO MPEG Audio Subgroup Software Simulation Group (1996)
  3.  * ISO 13818-3 MPEG-2 Audio Encoder - Lower Sampling Frequency Extension
  4.  *
  5.  * $Id: reservoir.c,v 1.1 1996/02/14 04:04:23 rowlands Exp $
  6.  *
  7.  * $Log: reservoir.c,v $
  8.  * Revision 1.1  1996/02/14 04:04:23  rowlands
  9.  * Initial revision
  10.  *
  11.  * Received from Mike Coleman
  12.  **********************************************************************/
  13. /*
  14.   Revision History:
  15.  
  16.   Date        Programmer                Comment
  17.   ==========  ========================= ===============================
  18.   1995/09/06  mc@fivebats.com           created
  19.  
  20. */
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <math.h>
  24. #include <assert.h>
  25. #include "l3side.h"
  26. #include "loop.h"
  27. #include "huffman.h"
  28. #include "l3bitstream.h"
  29. #include "reservoir.h"
  30. #include "gtkanal.h"
  31.  
  32. /*
  33.   Layer3 bit reservoir:
  34.   Described in C.1.5.4.2.2 of the IS
  35. */
  36.  
  37. static int ResvSize = 0; /* in bits */
  38. static int ResvMax  = 0; /* in bits */
  39.  
  40. void InitReservoir(void)
  41. {
  42.     ResvSize = 0;
  43.     ResvMax  = 0;
  44. }
  45.  
  46. /*
  47.   ResvFrameBegin:
  48.   Called at the beginning of a frame. Updates the maximum
  49.   size of the reservoir, and checks to make sure main_data_begin
  50.   was set properly by the formatter
  51. */
  52. int
  53. ResvFrameBegin( frame_params *fr_ps, III_side_info_t *l3_side, int mean_bits, int frameLength )
  54. {
  55.     layer *info;
  56.     int fullFrameBits, mode_gr;
  57.     int expectedResvSize, resvLimit;
  58.  
  59.     info = fr_ps->header;
  60.     if ( info->version == 1 )
  61.     {
  62.     mode_gr = 2;
  63.     resvLimit = 4088; /* main_data_begin has 9 bits in MPEG 1 */
  64.     }
  65.     else
  66.     {
  67.     mode_gr = 1;
  68.     resvLimit = 2040; /* main_data_begin has 8 bits in MPEG 2 */
  69.     }
  70.  
  71.     /*
  72.       main_data_begin was set by the formatter to the
  73.       expected value for the next call -- this should
  74.       agree with our reservoir size
  75.     */
  76.     expectedResvSize = l3_side->main_data_begin * 8;
  77. #ifdef DEBUG
  78.     fprintf( stderr, ">>> ResvSize = %d\n", ResvSize );
  79. #endif
  80.     assert( expectedResvSize == ResvSize );
  81.  
  82.     fullFrameBits = mean_bits * mode_gr + ResvSize;
  83.  
  84.     /*
  85.       determine maximum size of reservoir:
  86.       ResvMax + frameLength <= 7680;
  87.     */
  88.     if ( frameLength > 7680 )
  89.     ResvMax = 0;
  90.     else
  91.     ResvMax = 7680 - frameLength;
  92.  
  93.     /*
  94.       limit max size to resvLimit bits because
  95.       main_data_begin cannot indicate a
  96.       larger value
  97.       */
  98.     if ( ResvMax > resvLimit )
  99.     ResvMax = resvLimit;
  100.  
  101. #ifdef HAVEGTK
  102.   if (gtkflag){
  103.     pinfo->mean_bits=mean_bits/2;  /* expected bits per channel per granule */
  104.     pinfo->resvsize=ResvSize;
  105.   }
  106. #endif
  107.  
  108.     return fullFrameBits;
  109. }
  110.  
  111. /*
  112.   ResvMaxBits:
  113.   Called at the beginning of each granule to get the max bit
  114.   allowance for the current granule based on reservoir size
  115.   and perceptual entropy.
  116. */
  117. int
  118. ResvMaxBits( frame_params *fr_ps, III_side_info_t *l3_side, double *pe, int mean_bits,int gr,int ch)
  119. {
  120.     int more_bits, max_bits, add_bits, over_bits;
  121.  
  122.     mean_bits /= fr_ps->stereo;
  123.     max_bits = mean_bits;
  124.  
  125.     if ( max_bits > 4095 )
  126.     max_bits = 4095;
  127.  
  128.     if ( ResvMax == 0 )
  129.     return max_bits;
  130.  
  131.     more_bits = *pe * 3.1 - mean_bits;
  132.  
  133.     add_bits = 0;
  134.     if ( more_bits > 100 )
  135.     {
  136.     int frac = (ResvSize * 6) / 10;
  137.  
  138.     if ( frac < more_bits )
  139.         add_bits = frac;
  140.     else
  141.         add_bits = more_bits;
  142.     }
  143.  
  144.  
  145.     over_bits = ResvSize - ((ResvMax * 8) / 10) - add_bits;
  146.     if ( over_bits > 0 )
  147.     add_bits += over_bits;
  148.  
  149.     max_bits += add_bits;
  150.     if ( max_bits > 4095 )
  151.     max_bits = 4095;
  152.  
  153.     return max_bits;
  154. }
  155.  
  156. /*
  157.   ResvMaxBits2:
  158.   As above, but now it *really* is bits per granule (both channels).  
  159.   Mark Taylor 4/99
  160. */
  161. void ResvMaxBits2(int mean_bits, int *targ_bits, int *extra_bits, int gr)
  162. {
  163.   int add_bits;
  164.  
  165.  
  166.   *targ_bits = mean_bits ;
  167.   /* extra bits if the reservoir is almost full */
  168.   if (ResvSize > ((ResvMax * 9) / 10)) {
  169.     add_bits= ResvSize-((ResvMax * 9) / 10);
  170.     *targ_bits += add_bits;
  171.   }else {
  172.     add_bits =0 ;
  173.     /* build up reservoir.  this builds the reservoir a little slower
  174.      * than FhG */
  175.     *targ_bits -= 100; 
  176.   }
  177.   
  178.   /* amount from the reservoir we are allowed to use */
  179.   *extra_bits =    
  180.     (ResvSize  < (ResvMax*6)/10  ? ResvSize : (ResvMax*6)/10);
  181.   *extra_bits -= add_bits;
  182.   
  183.   if (*extra_bits < 0) *extra_bits=0;
  184.  
  185.  
  186.   if ( *targ_bits > 4095 )
  187.     *targ_bits = 4095;
  188.   if ( *targ_bits + *extra_bits > 4095 )
  189.     *extra_bits = 4095 - *targ_bits;
  190.   
  191. }
  192.  
  193. /*
  194.   ResvAdjust:
  195.   Called after a granule's bit allocation. Readjusts the size of
  196.   the reservoir to reflect the granule's usage.
  197. */
  198. void
  199. ResvAdjust( frame_params *fr_ps, gr_info *gi, III_side_info_t *l3_side, int mean_bits )
  200. {
  201.     ResvSize += (mean_bits / fr_ps->stereo) - gi->part2_3_length;
  202. }
  203.  
  204.  
  205. /*
  206.   ResvFrameEnd:
  207.   Called after all granules in a frame have been allocated. Makes sure
  208.   that the reservoir size is within limits, possibly by adding stuffing
  209.   bits. Note that stuffing bits are added by increasing a granule's
  210.   part2_3_length. The bitstream formatter will detect this and write the
  211.   appropriate stuffing bits to the bitstream.
  212. */
  213. void
  214. ResvFrameEnd( frame_params *fr_ps, III_side_info_t *l3_side, int mean_bits )
  215. {
  216.     layer *info;
  217.     gr_info *gi;
  218.     int mode_gr, gr, ch, stereo, ancillary_pad, stuffingBits;
  219.     int over_bits;
  220.  
  221.     info   = fr_ps->header;
  222.     stereo = fr_ps->stereo;
  223.     mode_gr = (info->version == 1) ? 2 : 1;
  224.     ancillary_pad = 0;
  225.  
  226. #if 1
  227.     /* just in case mean_bits is odd, this is necessary... */
  228.     if ( (stereo == 2) && (mean_bits & 1) )
  229.     ResvSize += 1;
  230. #endif
  231.  
  232.     over_bits = ResvSize - ResvMax;
  233.     if ( over_bits < 0 )
  234.     over_bits = 0;
  235.     
  236.     ResvSize -= over_bits;
  237.     stuffingBits = over_bits + ancillary_pad;
  238.  
  239.     /* we must be byte aligned */
  240.     if ( (over_bits = ResvSize % 8) )
  241.     {
  242.     stuffingBits += over_bits;
  243.     ResvSize -= over_bits;
  244.     }
  245.  
  246.     if ( stuffingBits )
  247.     {
  248.     /*
  249.       plan a: put all into the first granule
  250.       This was preferred by someone designing a
  251.       real-time decoder...
  252.     */
  253.     gi = (gr_info *) &(l3_side->gr[0].ch[0]);    
  254.     
  255.     if ( gi->part2_3_length + stuffingBits < 4095 )
  256.         gi->part2_3_length += stuffingBits;
  257.     else
  258.     {
  259.         /* plan b: distribute throughout the granules */
  260.         for (gr = 0; gr < mode_gr; gr++ )
  261.         for (ch = 0; ch < stereo; ch++ )
  262.         {
  263.             int extraBits, bitsThisGr;
  264.             gr_info *gi = (gr_info *) &(l3_side->gr[gr].ch[ch]);
  265.             if ( stuffingBits == 0 )
  266.             break;
  267.             extraBits = 4095 - gi->part2_3_length;
  268.             bitsThisGr = extraBits < stuffingBits ? extraBits : stuffingBits;
  269.             gi->part2_3_length += bitsThisGr;
  270.             stuffingBits -= bitsThisGr;
  271.         }
  272.         /*
  273.           If any stuffing bits remain, we elect to spill them
  274.           into ancillary data. The bitstream formatter will do this if
  275.           l3side->resvDrain is set
  276.         */
  277. #ifdef DEBUG
  278.         if ( stuffingBits )
  279.         fprintf( stderr, "spilling %d stuffing bits into ancillary data\n", stuffingBits );
  280. #endif
  281.         l3_side->resvDrain = stuffingBits;
  282.     }
  283.     }
  284. }
  285.  
  286.  
  287.