Coverage Report

Created: 2023-03-26 06:17

/src/lame/libmp3lame/reservoir.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 *      bit reservoir source file
3
 *
4
 *      Copyright (c) 1999-2000 Mark Taylor
5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Library General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2 of the License, or (at your option) any later version.
10
 *
11
 * This library is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Library General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Library General Public
17
 * License along with this library; if not, write to the
18
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19
 * Boston, MA 02111-1307, USA.
20
 */
21
22
/* $Id$ */
23
24
#ifdef HAVE_CONFIG_H
25
# include <config.h>
26
#endif
27
28
29
#include "lame.h"
30
#include "machine.h"
31
#include "encoder.h"
32
#include "util.h"
33
#include "reservoir.h"
34
35
#include "bitstream.h"
36
#include "lame-analysis.h"
37
#include "lame_global_flags.h"
38
39
40
/*
41
  ResvFrameBegin:
42
  Called (repeatedly) at the beginning of a frame. Updates the maximum
43
  size of the reservoir, and checks to make sure main_data_begin
44
  was set properly by the formatter
45
*/
46
47
/*
48
 *  Background information:
49
 *
50
 *  This is the original text from the ISO standard. Because of
51
 *  sooo many bugs and irritations correcting comments are added
52
 *  in brackets []. A '^W' means you should remove the last word.
53
 *
54
 *  1) The following rule can be used to calculate the maximum
55
 *     number of bits used for one granule [^W frame]:
56
 *     At the highest possible bitrate of Layer III (320 kbps
57
 *     per stereo signal [^W^W^W], 48 kHz) the frames must be of
58
 *     [^W^W^W are designed to have] constant length, i.e.
59
 *     one buffer [^W^W the frame] length is:
60
 *
61
 *         320 kbps * 1152/48 kHz = 7680 bit = 960 byte
62
 *
63
 *     This value is used as the maximum buffer per channel [^W^W] at
64
 *     lower bitrates [than 320 kbps]. At 64 kbps mono or 128 kbps
65
 *     stereo the main granule length is 64 kbps * 576/48 kHz = 768 bit
66
 *     [per granule and channel] at 48 kHz sampling frequency.
67
 *     This means that there is a maximum deviation (short time buffer
68
 *     [= reservoir]) of 7680 - 2*2*768 = 4608 bits is allowed at 64 kbps.
69
 *     The actual deviation is equal to the number of bytes [with the
70
 *     meaning of octets] denoted by the main_data_end offset pointer.
71
 *     The actual maximum deviation is (2^9-1)*8 bit = 4088 bits
72
 *     [for MPEG-1 and (2^8-1)*8 bit for MPEG-2, both are hard limits].
73
 *     ... The xchange of buffer bits between the left and right channel
74
 *     is allowed without restrictions [exception: dual channel].
75
 *     Because of the [constructed] constraint on the buffer size
76
 *     main_data_end is always set to 0 in the case of bit_rate_index==14,
77
 *     i.e. data rate 320 kbps per stereo signal [^W^W^W]. In this case
78
 *     all data are allocated between adjacent header [^W sync] words
79
 *     [, i.e. there is no buffering at all].
80
 */
81
82
int
83
ResvFrameBegin(lame_internal_flags * gfc, int *mean_bits)
84
556k
{
85
556k
    SessionConfig_t const *const cfg = &gfc->cfg;
86
556k
    EncStateVar_t *const esv = &gfc->sv_enc;
87
556k
    int     fullFrameBits;
88
556k
    int     resvLimit;
89
556k
    int     maxmp3buf;
90
556k
    III_side_info_t *const l3_side = &gfc->l3_side;
91
556k
    int     frameLength;
92
556k
    int     meanBits;
93
94
556k
    frameLength = getframebits(gfc);
95
556k
    meanBits = (frameLength - cfg->sideinfo_len * 8) / cfg->mode_gr;
96
97
/*
98
 *  Meaning of the variables:
99
 *      resvLimit: (0, 8, ..., 8*255 (MPEG-2), 8*511 (MPEG-1))
100
 *          Number of bits can be stored in previous frame(s) due to
101
 *          counter size constaints
102
 *      maxmp3buf: ( ??? ... 8*1951 (MPEG-1 and 2), 8*2047 (MPEG-2.5))
103
 *          Number of bits allowed to encode one frame (you can take 8*511 bit
104
 *          from the bit reservoir and at most 8*1440 bit from the current
105
 *          frame (320 kbps, 32 kHz), so 8*1951 bit is the largest possible
106
 *          value for MPEG-1 and -2)
107
 *
108
 *          maximum allowed granule/channel size times 4 = 8*2047 bits.,
109
 *          so this is the absolute maximum supported by the format.
110
 *
111
 *
112
 *      fullFrameBits:  maximum number of bits available for encoding
113
 *                      the current frame.
114
 *
115
 *      mean_bits:      target number of bits per granule.
116
 *
117
 *      frameLength:
118
 *
119
 *      gfc->ResvMax:   maximum allowed reservoir
120
 *
121
 *      gfc->ResvSize:  current reservoir size
122
 *
123
 *      l3_side->resvDrain_pre:
124
 *         ancillary data to be added to previous frame:
125
 *         (only usefull in VBR modes if it is possible to have
126
 *         maxmp3buf < fullFrameBits)).  Currently disabled,
127
 *         see #define NEW_DRAIN
128
 *         2010-02-13: RH now enabled, it seems to be needed for CBR too,
129
 *                     as there exists one example, where the FhG decoder
130
 *                     can't decode a -b320 CBR file anymore.
131
 *
132
 *      l3_side->resvDrain_post:
133
 *         ancillary data to be added to this frame:
134
 *
135
 */
136
137
    /* main_data_begin has 9 bits in MPEG-1, 8 bits MPEG-2 */
138
556k
    resvLimit = (8 * 256) * cfg->mode_gr - 8;
139
140
    /* maximum allowed frame size.  dont use more than this number of
141
       bits, even if the frame has the space for them: */
142
556k
    maxmp3buf = cfg->buffer_constraint;
143
556k
    esv->ResvMax = maxmp3buf - frameLength;
144
556k
    if (esv->ResvMax > resvLimit)
145
481k
        esv->ResvMax = resvLimit;
146
556k
    if (esv->ResvMax < 0 || cfg->disable_reservoir)
147
1.92k
        esv->ResvMax = 0;
148
    
149
556k
    fullFrameBits = meanBits * cfg->mode_gr + Min(esv->ResvSize, esv->ResvMax);
150
151
556k
    if (fullFrameBits > maxmp3buf)
152
0
        fullFrameBits = maxmp3buf;
153
154
556k
    assert(0 == esv->ResvMax % 8);
155
556k
    assert(esv->ResvMax >= 0);
156
157
556k
    l3_side->resvDrain_pre = 0;
158
159
556k
    if (gfc->pinfo != NULL) {
160
0
        gfc->pinfo->mean_bits = meanBits / 2; /* expected bits per channel per granule [is this also right for mono/stereo, MPEG-1/2 ?] */
161
0
        gfc->pinfo->resvsize = esv->ResvSize;
162
0
    }
163
556k
    *mean_bits = meanBits;
164
556k
    return fullFrameBits;
165
556k
}
166
167
168
/*
169
  ResvMaxBits
170
  returns targ_bits:  target number of bits to use for 1 granule
171
         extra_bits:  amount extra available from reservoir
172
  Mark Taylor 4/99
173
*/
174
void
175
ResvMaxBits(lame_internal_flags * gfc, int mean_bits, int *targ_bits, int *extra_bits, int cbr)
176
105k
{
177
105k
    SessionConfig_t const *const cfg = &gfc->cfg;
178
105k
    EncStateVar_t *const esv = &gfc->sv_enc;
179
105k
    int     add_bits, targBits, extraBits;
180
105k
    int     ResvSize = esv->ResvSize, ResvMax = esv->ResvMax;
181
182
    /* conpensate the saved bits used in the 1st granule */
183
105k
    if (cbr)
184
9.81k
        ResvSize += mean_bits;
185
186
105k
    if (gfc->sv_qnt.substep_shaping & 1)
187
0
        ResvMax *= 0.9;
188
189
105k
    targBits = mean_bits;
190
191
    /* extra bits if the reservoir is almost full */
192
105k
    if (ResvSize * 10 > ResvMax * 9) {
193
23.0k
        add_bits = ResvSize - (ResvMax * 9) / 10;
194
23.0k
        targBits += add_bits;
195
23.0k
        gfc->sv_qnt.substep_shaping |= 0x80;
196
23.0k
    }
197
82.1k
    else {
198
82.1k
        add_bits = 0;
199
82.1k
        gfc->sv_qnt.substep_shaping &= 0x7f;
200
        /* build up reservoir.  this builds the reservoir a little slower
201
         * than FhG.  It could simple be mean_bits/15, but this was rigged
202
         * to always produce 100 (the old value) at 128kbs */
203
        /*    *targ_bits -= (int) (mean_bits/15.2); */
204
82.1k
        if (!cfg->disable_reservoir && !(gfc->sv_qnt.substep_shaping & 1))
205
80.2k
            targBits -= .1 * mean_bits;
206
82.1k
    }
207
208
209
    /* amount from the reservoir we are allowed to use. ISO says 6/10 */
210
105k
    extraBits = (ResvSize < (esv->ResvMax * 6) / 10 ? ResvSize : (esv->ResvMax * 6) / 10);
211
105k
    extraBits -= add_bits;
212
213
105k
    if (extraBits < 0)
214
12.5k
        extraBits = 0;
215
216
105k
    *targ_bits = targBits;
217
105k
    *extra_bits = extraBits;
218
105k
}
219
220
/*
221
  ResvAdjust:
222
  Called after a granule's bit allocation. Readjusts the size of
223
  the reservoir to reflect the granule's usage.
224
*/
225
void
226
ResvAdjust(lame_internal_flags * gfc, gr_info const *gi)
227
183k
{
228
183k
    gfc->sv_enc.ResvSize -= gi->part2_3_length + gi->part2_length;
229
183k
}
230
231
232
/*
233
  ResvFrameEnd:
234
  Called after all granules in a frame have been allocated. Makes sure
235
  that the reservoir size is within limits, possibly by adding stuffing
236
  bits.
237
*/
238
void
239
ResvFrameEnd(lame_internal_flags * gfc, int mean_bits)
240
64.5k
{
241
64.5k
    SessionConfig_t const *const cfg = &gfc->cfg;
242
64.5k
    EncStateVar_t *const esv = &gfc->sv_enc;
243
64.5k
    III_side_info_t *const l3_side = &gfc->l3_side;
244
64.5k
    int     stuffingBits;
245
64.5k
    int     over_bits;
246
247
64.5k
    esv->ResvSize += mean_bits * cfg->mode_gr;
248
64.5k
    stuffingBits = 0;
249
64.5k
    l3_side->resvDrain_post = 0;
250
64.5k
    l3_side->resvDrain_pre = 0;
251
252
    /* we must be byte aligned */
253
64.5k
    if ((over_bits = esv->ResvSize % 8) != 0)
254
49.7k
        stuffingBits += over_bits;
255
256
257
64.5k
    over_bits = (esv->ResvSize - stuffingBits) - esv->ResvMax;
258
64.5k
    if (over_bits > 0) {
259
8.93k
        assert(0 == over_bits % 8);
260
8.93k
        assert(over_bits >= 0);
261
8.93k
        stuffingBits += over_bits;
262
8.93k
    }
263
264
265
    /* NOTE: enabling the NEW_DRAIN code fixes some problems with FhG decoder
266
             shipped with MS Windows operating systems. Using this, it is even
267
             possible to use Gabriel's lax buffer consideration again, which
268
             assumes, any decoder should have a buffer large enough
269
             for a 320 kbps frame at 32 kHz sample rate.
270
271
       old drain code:
272
             lame -b320 BlackBird.wav ---> does not play with GraphEdit.exe using FhG decoder V1.5 Build 50
273
274
       new drain code:
275
             lame -b320 BlackBird.wav ---> plays fine with GraphEdit.exe using FhG decoder V1.5 Build 50
276
277
             Robert Hegemann, 2010-02-13.
278
     */
279
    /* drain as many bits as possible into previous frame ancillary data
280
     * In particular, in VBR mode ResvMax may have changed, and we have
281
     * to make sure main_data_begin does not create a reservoir bigger
282
     * than ResvMax  mt 4/00*/
283
64.5k
    {
284
64.5k
        int     mdb_bytes = Min(l3_side->main_data_begin * 8, stuffingBits) / 8;
285
64.5k
        l3_side->resvDrain_pre += 8 * mdb_bytes;
286
64.5k
        stuffingBits -= 8 * mdb_bytes;
287
64.5k
        esv->ResvSize -= 8 * mdb_bytes;
288
64.5k
        l3_side->main_data_begin -= mdb_bytes;
289
64.5k
    }
290
    /* drain the rest into this frames ancillary data */
291
64.5k
    l3_side->resvDrain_post += stuffingBits;
292
64.5k
    esv->ResvSize -= stuffingBits;
293
64.5k
}