2 (c) Copyright 1998-2000 - 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, and might
7 contain smaller or larger sections that are directly taken
8 from ISO's reference code.
10 All changes to the ISO reference code herein are either
11 copyrighted by Tord Jansson (tord.jansson@swipnet.se)
12 or sublicensed to Tord Jansson by a third party.
14 BladeEnc is free software; you can redistribute this file
15 and/or modify it under the terms of the GNU Lesser General Public
16 License as published by the Free Software Foundation; either
17 version 2.1 of the License, or (at your option) any later version.
21 ------------ Changes ------------
23 2000-12-11 Andre Piotrowski
37 #include "l3bitstream.h"
38 #include "reservoir.h"
46 Described in C.1.5.4.2.2 of the IS
49 static int ResvSize = 0; /* in bits */
50 static int ResvMax = 0; /* in bits */
56 void fixStatic_reservoir (void)
68 Called at the beginning of a frame. Updates the maximum
69 size of the reservoir, and checks to make sure main_data_begin
70 was set properly by the formatter
75 III_side_info_t *l3_side,
81 int fullFrameBits, mode_gr;
82 int expectedResvSize, resvLimit;
87 resvLimit = 4088; /* main_data_begin has 9 bits in MPEG 1 */
91 main_data_begin was set by the formatter to the
92 expected value for the next call -- this should
93 agree with our reservoir size
95 expectedResvSize = l3_side->main_data_begin * 8;
96 /* assert (expectedResvSize == ResvSize); */
98 fullFrameBits = mean_bits * mode_gr;
101 determine maximum size of reservoir:
102 ResvMax + frameLength <= 7680;
104 limit max size to resvLimit bits because
105 main_data_begin cannot indicate a
108 ResvMax = MIN(MAX (0, 7680-frameLength), resvLimit);
117 Called at the beginning of each granule to get the max bit
118 allowance for the current granule based on reservoir size
119 and perceptual entropy.
124 III_side_info_t *l3_side,
129 int more_bits, max_bits, add_bits, over_bits;
132 mean_bits /= fr_ps->stereo;
134 max_bits = mean_bits;
138 more_bits = (int) (*pe * 3.1 - mean_bits);
142 int frac = (ResvSize * 6) / 10;
144 add_bits = MIN(frac, more_bits);
149 over_bits = ResvSize - ((ResvMax * 8) / 10) - add_bits;
151 add_bits += over_bits;
153 max_bits += add_bits;
168 Called after a granule's bit allocation. Readjusts the size of
169 the reservoir to reflect the granule's usage.
175 III_side_info_t *l3_side,
179 ResvSize += (mean_bits / fr_ps->stereo) - cod_info->part2_3_length;
188 Called after all granules in a frame have been allocated. Makes sure
189 that the reservoir size is within limits, possibly by adding stuffing
190 bits. Note that stuffing bits are added by increasing a granule's
191 part2_3_length. The bitstream formatter will detect this and write the
192 appropriate stuffing bits to the bitstream.
197 III_side_info_t *l3_side,
203 int mode_gr, gr, ch, stereo, ancillary_pad, stuffingBits;
206 info = fr_ps->header;
207 stereo = fr_ps->stereo;
212 /* just in case mean_bits is odd, this is necessary... */
213 if ((stereo == 2) && (mean_bits & 1))
216 stuffingBits = ancillary_pad;
218 if ((over_bits = ResvSize - ResvMax) > 0)
220 stuffingBits += over_bits;
221 ResvSize -= over_bits;
224 /* we must be byte aligned */
225 if ((over_bits = ResvSize % 8) != 0)
227 stuffingBits += over_bits;
228 ResvSize -= over_bits;
234 plan a: put all into the first granule
235 This was preferred by someone designing a
238 cod_info = &l3_side->gr[0].ch[0].tt;
240 if (cod_info->part2_3_length + stuffingBits < 4095)
241 cod_info->part2_3_length += stuffingBits;
244 /* plan b: distribute throughout the granules */
245 for (gr = 0; gr < mode_gr; gr++)
247 for (ch = 0; ch < stereo; ch++)
249 int extraBits, bitsThisGr;
250 gr_info *cod_info = &l3_side->gr[gr].ch[ch].tt;
252 if (stuffingBits == 0)
254 extraBits = 4095 - cod_info->part2_3_length;
255 bitsThisGr = (extraBits < stuffingBits) ? extraBits : stuffingBits;
256 cod_info->part2_3_length += bitsThisGr;
257 stuffingBits -= bitsThisGr;
261 If any stuffing bits remain, we elect to spill them
262 into ancillary data. The bitstream formatter will do this if
263 l3side->resvDrain is set
265 l3_side->resvDrain = stuffingBits;