seed random from ruby interface
[swftools.git] / lib / bladeenc / reservoir.c
1 /*
2                         (c) Copyright 1998-2000 - Tord Jansson
3                         ======================================
4
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.
9
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.
13
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.
18
19
20
21         ------------    Changes    ------------
22
23         2000-12-11  Andre Piotrowski
24
25         -       reformatted
26 */
27
28 #include        <stdio.h>
29 #include        <stdlib.h>
30 #include        <math.h>
31 #include        <assert.h>
32
33 #include        "common.h"
34 #include        "l3side.h"
35 #include        "loop.h"
36 #include        "huffman.h"
37 #include        "l3bitstream.h"
38 #include        "reservoir.h"
39
40
41
42
43
44 /*
45         Layer3 bit reservoir:
46         Described in C.1.5.4.2.2 of the IS
47 */
48
49 static  int                             ResvSize = 0;   /* in bits */
50 static  int                             ResvMax  = 0;   /* in bits */
51
52
53
54
55
56 void                                    fixStatic_reservoir (void)
57 {
58         ResvSize = 0;
59         ResvMax  = 0;
60 }
61
62
63
64
65
66 /*
67         ResvFrameBegin:
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
71 */
72 void                                    ResvFrameBegin
73 (
74         frame_params                    *fr_ps,
75         III_side_info_t                 *l3_side,
76         int                                             mean_bits,
77         int                                             frameLength
78 )
79 {
80         layer                                   *info;
81         int                                             fullFrameBits, mode_gr;
82         int                                             expectedResvSize, resvLimit;
83
84
85         info = fr_ps->header;
86         mode_gr = 2;
87         resvLimit = 4088; /* main_data_begin has 9 bits in MPEG 1 */
88
89
90         /*
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
94         */
95         expectedResvSize = l3_side->main_data_begin * 8;
96 /*      assert (expectedResvSize == ResvSize); */
97
98         fullFrameBits = mean_bits * mode_gr;
99
100         /*
101                 determine maximum size of reservoir:
102                 ResvMax + frameLength <= 7680;
103
104                 limit max size to resvLimit bits because
105                 main_data_begin cannot indicate a
106                 larger value
107         */
108         ResvMax = MIN(MAX (0, 7680-frameLength), resvLimit);
109 }
110
111
112
113
114
115 /*
116         ResvMaxBits:
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.
120 */
121 int                                             ResvMaxBits
122 (
123         frame_params                    *fr_ps,
124         III_side_info_t                 *l3_side,
125         double                                  *pe,
126         int                                             mean_bits
127 )
128 {
129         int                                             more_bits, max_bits, add_bits, over_bits;
130
131
132         mean_bits /= fr_ps->stereo;
133
134         max_bits = mean_bits;
135
136         if (ResvMax != 0)
137         {
138                 more_bits = (int) (*pe * 3.1 - mean_bits);
139
140                 if (more_bits > 100)
141                 {
142                         int             frac = (ResvSize * 6) / 10;
143
144                         add_bits = MIN(frac, more_bits);
145                 }
146                 else
147                         add_bits = 0;
148
149                 over_bits = ResvSize - ((ResvMax * 8) / 10) - add_bits;
150                 if (over_bits > 0)
151                         add_bits += over_bits;
152
153                 max_bits += add_bits;
154         }
155
156         if (max_bits > 4095)
157                 max_bits = 4095;
158
159         return max_bits;
160 }
161
162
163
164
165
166 /*
167         ResvAdjust:
168         Called after a granule's bit allocation. Readjusts the size of
169         the reservoir to reflect the granule's usage.
170 */
171 void                                    ResvAdjust
172 (
173         frame_params                    *fr_ps,
174         gr_info                                 *cod_info,
175         III_side_info_t                 *l3_side,
176         int                                             mean_bits
177 )
178 {
179         ResvSize += (mean_bits / fr_ps->stereo) - cod_info->part2_3_length;
180 }
181
182
183
184
185
186 /*
187         ResvFrameEnd:
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.
193 */
194 void                                    ResvFrameEnd
195 (
196         frame_params                    *fr_ps,
197         III_side_info_t                 *l3_side,
198         int                                             mean_bits
199 )
200 {
201         layer                                   *info;
202         gr_info                                 *cod_info;
203         int                                             mode_gr, gr, ch, stereo, ancillary_pad, stuffingBits;
204         int                                             over_bits;
205
206         info    = fr_ps->header;
207         stereo  = fr_ps->stereo;
208         mode_gr = 2;
209
210         ancillary_pad = 0;
211
212         /* just in case mean_bits is odd, this is necessary... */
213         if ((stereo == 2)  &&  (mean_bits & 1))
214                 ResvSize ++;
215
216         stuffingBits = ancillary_pad;
217         
218         if ((over_bits = ResvSize - ResvMax) > 0)
219         {
220                 stuffingBits += over_bits;
221                 ResvSize     -= over_bits;
222         }
223
224         /* we must be byte aligned */
225         if ((over_bits = ResvSize % 8) != 0)
226         {
227                 stuffingBits += over_bits;
228                 ResvSize     -= over_bits;
229         }
230
231         if (stuffingBits)
232         {
233                 /*
234                         plan a: put all into the first granule
235                         This was preferred by someone designing a
236                         real-time decoder...
237                 */
238                 cod_info = &l3_side->gr[0].ch[0].tt;
239
240                 if (cod_info->part2_3_length + stuffingBits < 4095)
241                         cod_info->part2_3_length += stuffingBits;
242                 else
243                 {
244                     /* plan b: distribute throughout the granules */
245                     for (gr = 0;  gr < mode_gr;  gr++)
246                     {
247                                 for (ch = 0;  ch < stereo;  ch++)
248                                 {
249                                         int                     extraBits, bitsThisGr;
250                                         gr_info         *cod_info = &l3_side->gr[gr].ch[ch].tt;
251
252                                         if (stuffingBits == 0)
253                                                 break;
254                                         extraBits = 4095 - cod_info->part2_3_length;
255                                         bitsThisGr = (extraBits < stuffingBits) ? extraBits : stuffingBits;
256                                         cod_info->part2_3_length += bitsThisGr;
257                                         stuffingBits -= bitsThisGr;
258                                 }
259                         }
260                     /*
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
264                     */
265                     l3_side->resvDrain = stuffingBits;
266                 }
267         }
268 }