reservoir.c

Go to the documentation of this file.
00001 /*
00002  *      bit reservoir source file
00003  *
00004  *      Copyright (c) 1999-2000 Mark Taylor
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Library General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the
00018  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00019  * Boston, MA 02111-1307, USA.
00020  */
00021 
00022 /* $Id: reservoir.c,v 1.39 2007/07/24 17:46:10 bouvigne Exp $ */
00023 
00024 #ifdef HAVE_CONFIG_H
00025 # include <config.h>
00026 #endif
00027 
00028 
00029 #include "lame.h"
00030 #include "machine.h"
00031 #include "encoder.h"
00032 #include "util.h"
00033 #include "reservoir.h"
00034 
00035 #include "bitstream.h"
00036 #include "lame-analysis.h"
00037 #include "lame_global_flags.h"
00038 
00039 /*
00040   ResvFrameBegin:
00041   Called (repeatedly) at the beginning of a frame. Updates the maximum
00042   size of the reservoir, and checks to make sure main_data_begin
00043   was set properly by the formatter
00044 */
00045 
00046 /*
00047  *  Background information:
00048  *
00049  *  This is the original text from the ISO standard. Because of
00050  *  sooo many bugs and irritations correcting comments are added
00051  *  in brackets []. A '^W' means you should remove the last word.
00052  *
00053  *  1) The following rule can be used to calculate the maximum
00054  *     number of bits used for one granule [^W frame]:
00055  *     At the highest possible bitrate of Layer III (320 kbps
00056  *     per stereo signal [^W^W^W], 48 kHz) the frames must be of
00057  *     [^W^W^W are designed to have] constant length, i.e.
00058  *     one buffer [^W^W the frame] length is:
00059  *
00060  *         320 kbps * 1152/48 kHz = 7680 bit = 960 byte
00061  *
00062  *     This value is used as the maximum buffer per channel [^W^W] at
00063  *     lower bitrates [than 320 kbps]. At 64 kbps mono or 128 kbps
00064  *     stereo the main granule length is 64 kbps * 576/48 kHz = 768 bit
00065  *     [per granule and channel] at 48 kHz sampling frequency.
00066  *     This means that there is a maximum deviation (short time buffer
00067  *     [= reservoir]) of 7680 - 2*2*768 = 4608 bits is allowed at 64 kbps.
00068  *     The actual deviation is equal to the number of bytes [with the
00069  *     meaning of octets] denoted by the main_data_end offset pointer.
00070  *     The actual maximum deviation is (2^9-1)*8 bit = 4088 bits
00071  *     [for MPEG-1 and (2^8-1)*8 bit for MPEG-2, both are hard limits].
00072  *     ... The xchange of buffer bits between the left and right channel
00073  *     is allowed without restrictions [exception: dual channel].
00074  *     Because of the [constructed] constraint on the buffer size
00075  *     main_data_end is always set to 0 in the case of bit_rate_index==14,
00076  *     i.e. data rate 320 kbps per stereo signal [^W^W^W]. In this case
00077  *     all data are allocated between adjacent header [^W sync] words
00078  *     [, i.e. there is no buffering at all].
00079  */
00080 
00081 int
00082 ResvFrameBegin(lame_global_flags const *gfp, int *mean_bits)
00083 {
00084     lame_internal_flags *const gfc = gfp->internal_flags;
00085     int     fullFrameBits;
00086     int     resvLimit;
00087     int     maxmp3buf;
00088     III_side_info_t *const l3_side = &gfc->l3_side;
00089     int     frameLength;
00090 
00091     frameLength = getframebits(gfp);
00092     *mean_bits = (frameLength - gfc->sideinfo_len * 8) / gfc->mode_gr;
00093 
00094 /*
00095  *  Meaning of the variables:
00096  *      resvLimit: (0, 8, ..., 8*255 (MPEG-2), 8*511 (MPEG-1))
00097  *          Number of bits can be stored in previous frame(s) due to
00098  *          counter size constaints
00099  *      maxmp3buf: ( ??? ... 8*1951 (MPEG-1 and 2), 8*2047 (MPEG-2.5))
00100  *          Number of bits allowed to encode one frame (you can take 8*511 bit
00101  *          from the bit reservoir and at most 8*1440 bit from the current
00102  *          frame (320 kbps, 32 kHz), so 8*1951 bit is the largest possible
00103  *          value for MPEG-1 and -2)
00104  *
00105  *          maximum allowed granule/channel size times 4 = 8*2047 bits.,
00106  *          so this is the absolute maximum supported by the format.
00107  *
00108  *
00109  *      fullFrameBits:  maximum number of bits available for encoding
00110  *                      the current frame.
00111  *
00112  *      mean_bits:      target number of bits per granule.
00113  *
00114  *      frameLength:
00115  *
00116  *      gfc->ResvMax:   maximum allowed reservoir
00117  *
00118  *      gfc->ResvSize:  current reservoir size
00119  *
00120  *      l3_side->resvDrain_pre:
00121  *         ancillary data to be added to previous frame:
00122  *         (only usefull in VBR modes if it is possible to have
00123  *         maxmp3buf < fullFrameBits)).  Currently disabled,
00124  *         see #define NEW_DRAIN
00125  *
00126  *      l3_side->resvDrain_post:
00127  *         ancillary data to be added to this frame:
00128  *
00129  */
00130 
00131     /* main_data_begin has 9 bits in MPEG-1, 8 bits MPEG-2 */
00132     resvLimit = (8 * 256) * gfc->mode_gr - 8;
00133 
00134     /* maximum allowed frame size.  dont use more than this number of
00135        bits, even if the frame has the space for them: */
00136     if (gfp->brate > 320) {
00137         /* in freeformat the buffer is constant */
00138         maxmp3buf =
00139             8 * ((int) ((gfp->brate * 1000) / (gfp->out_samplerate / (FLOAT) 1152) / 8 + .5));
00140     }
00141     else {
00142         /*all mp3 decoders should have enough buffer to handle this value: size of a 320kbps 32kHz frame */
00143         maxmp3buf = 8 * 1440;
00144 
00145         /* Bouvigne suggests this more lax interpretation of the ISO doc
00146            instead of using 8*960. */
00147 
00148         /*
00149         if (gfp->strict_ISO == old_FhG_decoder)
00150          always enabled because of compatibility problems with some old FhG decoders
00151          which is distributed with almost every Windows Installation
00152         */
00153         {
00154             maxmp3buf = 8 * ((int) (320000 / (gfp->out_samplerate / (FLOAT) 1152) / 8 + .5));
00155             /* adding (almost all) bits used for sideinfo seems still to work with old FhG
00156                decoders, so in 320 kbps case the backpointer may point back some bytes too
00157              */
00158             maxmp3buf += (gfc->sideinfo_len-8)*8;
00159         }
00160         if (gfp->strict_ISO)
00161         {
00162             maxmp3buf = 8 * ((int) (320000 / (gfp->out_samplerate / (FLOAT) 1152) / 8 + .5));
00163         }
00164     }
00165 
00166     gfc->ResvMax = maxmp3buf - frameLength;
00167     if (gfc->ResvMax > resvLimit)
00168         gfc->ResvMax = resvLimit;
00169     if (gfc->ResvMax < 0 || gfp->disable_reservoir)
00170         gfc->ResvMax = 0;
00171 
00172     fullFrameBits = *mean_bits * gfc->mode_gr + Min(gfc->ResvSize, gfc->ResvMax);
00173 
00174     if (fullFrameBits > maxmp3buf)
00175         fullFrameBits = maxmp3buf;
00176 
00177     assert(0 == gfc->ResvMax % 8);
00178     assert(gfc->ResvMax >= 0);
00179 
00180     l3_side->resvDrain_pre = 0;
00181 
00182     if (gfc->pinfo != NULL) {
00183         gfc->pinfo->mean_bits = *mean_bits / 2; /* expected bits per channel per granule [is this also right for mono/stereo, MPEG-1/2 ?] */
00184         gfc->pinfo->resvsize = gfc->ResvSize;
00185     }
00186 
00187     return fullFrameBits;
00188 }
00189 
00190 
00191 /*
00192   ResvMaxBits
00193   returns targ_bits:  target number of bits to use for 1 granule
00194          extra_bits:  amount extra available from reservoir
00195   Mark Taylor 4/99
00196 */
00197 void
00198 ResvMaxBits(lame_global_flags const *gfp, int mean_bits, int *targ_bits, int *extra_bits, int cbr)
00199 {
00200     lame_internal_flags *const gfc = gfp->internal_flags;
00201     int     add_bits;
00202     int     ResvSize = gfc->ResvSize, ResvMax = gfc->ResvMax;
00203 
00204     /* conpensate the saved bits used in the 1st granule */
00205     if (cbr)
00206         ResvSize += mean_bits;
00207 
00208     if (gfc->substep_shaping & 1)
00209         ResvMax *= 0.9;
00210 
00211     *targ_bits = mean_bits;
00212 
00213     /* extra bits if the reservoir is almost full */
00214     if (ResvSize * 10 > ResvMax * 9) {
00215         add_bits = ResvSize - (ResvMax * 9) / 10;
00216         *targ_bits += add_bits;
00217         gfc->substep_shaping |= 0x80;
00218     }
00219     else {
00220         add_bits = 0;
00221         gfc->substep_shaping &= 0x7f;
00222         /* build up reservoir.  this builds the reservoir a little slower
00223          * than FhG.  It could simple be mean_bits/15, but this was rigged
00224          * to always produce 100 (the old value) at 128kbs */
00225         /*    *targ_bits -= (int) (mean_bits/15.2); */
00226         if (!gfp->disable_reservoir && !(gfc->substep_shaping & 1))
00227             *targ_bits -= .1 * mean_bits;
00228     }
00229 
00230 
00231     /* amount from the reservoir we are allowed to use. ISO says 6/10 */
00232     *extra_bits = (ResvSize < (gfc->ResvMax * 6) / 10 ? ResvSize : (gfc->ResvMax * 6) / 10);
00233     *extra_bits -= add_bits;
00234 
00235     if (*extra_bits < 0)
00236         *extra_bits = 0;
00237 
00238 
00239 }
00240 
00241 /*
00242   ResvAdjust:
00243   Called after a granule's bit allocation. Readjusts the size of
00244   the reservoir to reflect the granule's usage.
00245 */
00246 void
00247 ResvAdjust(lame_internal_flags * gfc, gr_info const *gi)
00248 {
00249     gfc->ResvSize -= gi->part2_3_length + gi->part2_length;
00250 }
00251 
00252 
00253 /*
00254   ResvFrameEnd:
00255   Called after all granules in a frame have been allocated. Makes sure
00256   that the reservoir size is within limits, possibly by adding stuffing
00257   bits.
00258 */
00259 void
00260 ResvFrameEnd(lame_internal_flags * gfc, int mean_bits)
00261 {
00262     int     stuffingBits;
00263     int     over_bits;
00264     III_side_info_t *const l3_side = &gfc->l3_side;
00265 
00266 
00267     gfc->ResvSize += mean_bits * gfc->mode_gr;
00268     stuffingBits = 0;
00269     l3_side->resvDrain_post = 0;
00270     l3_side->resvDrain_pre = 0;
00271 
00272     /* we must be byte aligned */
00273     if ((over_bits = gfc->ResvSize % 8) != 0)
00274         stuffingBits += over_bits;
00275 
00276 
00277     over_bits = (gfc->ResvSize - stuffingBits) - gfc->ResvMax;
00278     if (over_bits > 0) {
00279         assert(0 == over_bits % 8);
00280         assert(over_bits >= 0);
00281         stuffingBits += over_bits;
00282     }
00283 
00284 
00285 #undef NEW_DRAIN
00286 #ifdef NEW_DRAIN
00287     /* drain as many bits as possible into previous frame ancillary data
00288      * In particular, in VBR mode ResvMax may have changed, and we have
00289      * to make sure main_data_begin does not create a reservoir bigger
00290      * than ResvMax  mt 4/00*/
00291     {
00292         int     mdb_bytes = Min(l3_side->main_data_begin * 8, stuffingBits) / 8;
00293         l3_side->resvDrain_pre += 8 * mdb_bytes;
00294         stuffingBits -= 8 * mdb_bytes;
00295         gfc->ResvSize -= 8 * mdb_bytes;
00296         l3_side->main_data_begin -= mdb_bytes;
00297 
00298 
00299         /* drain just enough to be byte aligned.  The remaining bits will
00300          * be added to the reservoir, and we will deal with them next frame.
00301          * If the next frame is at a lower bitrate, it may have a larger ResvMax,
00302          * and we will not have to waste these bits!  mt 4/00 */
00303         assert(stuffingBits >= 0);
00304         l3_side->resvDrain_post += (stuffingBits % 8);
00305         gfc->ResvSize -= stuffingBits % 8;
00306     }
00307 #else
00308     /* drain the rest into this frames ancillary data */
00309     l3_side->resvDrain_post += stuffingBits;
00310     gfc->ResvSize -= stuffingBits;
00311 #endif
00312 
00313     return;
00314 }

Generated on Sun Dec 2 11:34:20 2007 for LAME by  doxygen 1.5.2