]> git.sesse.net Git - ffmpeg/blob - libavcodec/vorbis_enc.c
c7dd885f7843cccdd031ba9d4b9da496753adf4e
[ffmpeg] / libavcodec / vorbis_enc.c
1 /*
2  * copyright (c) 2006 Oded Shimon <ods15@ods15.dyndns.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18
19 /**
20  * @file vorbis_enc.c
21  * Native Vorbis encoder.
22  * @author Oded Shimon <ods15@ods15.dyndns.org>
23  */
24
25 #include "avcodec.h"
26 #include "dsputil.h"
27 #include "vorbis.h"
28
29 #undef NDEBUG
30 #include <assert.h>
31
32 typedef struct {
33     int nentries;
34     uint8_t * lens;
35     uint32_t * codewords;
36     int ndimentions;
37     float min;
38     float delta;
39     int seq_p;
40     int lookup;
41     int * quantlist;
42     float * dimentions;
43 } codebook_t;
44
45 typedef struct {
46     int dim;
47     int subclass;
48     int masterbook;
49     int * books;
50 } floor_class_t;
51
52 typedef struct {
53     int x;
54     int low;
55     int high;
56     int sort;
57 } floor_entry_t;
58
59 typedef struct {
60     int partitions;
61     int * partition_to_class;
62     int nclasses;
63     floor_class_t * classes;
64     int multiplier;
65     int rangebits;
66     int values;
67     floor_entry_t * list;
68 } floor_t;
69
70 typedef struct {
71     int type;
72     int begin;
73     int end;
74     int partition_size;
75     int classifications;
76     int classbook;
77     int8_t (*books)[8];
78     float (*maxes)[2];
79 } residue_t;
80
81 typedef struct {
82     int submaps;
83     int * mux;
84     int * floor;
85     int * residue;
86     int coupling_steps;
87     int * magnitude;
88     int * angle;
89 } mapping_t;
90
91 typedef struct {
92     int blockflag;
93     int mapping;
94 } vorbis_mode_t;
95
96 typedef struct {
97     int channels;
98     int sample_rate;
99     int blocksize[2]; // in (1<<n) format
100     MDCTContext mdct[2];
101     const float * win[2];
102     int have_saved;
103     float * saved;
104     float * samples;
105     float * floor; // also used for tmp values for mdct
106     float * coeffs; // also used for residue after floor
107     float quality;
108
109     int ncodebooks;
110     codebook_t * codebooks;
111
112     int nfloors;
113     floor_t * floors;
114
115     int nresidues;
116     residue_t * residues;
117
118     int nmappings;
119     mapping_t * mappings;
120
121     int nmodes;
122     vorbis_mode_t * modes;
123 } venc_context_t;
124
125 typedef struct {
126     int total;
127     int total_pos;
128     int pos;
129     uint8_t * buf_ptr;
130 } PutBitContext;
131
132 static const uint8_t codebook0[] = {
133    2, 10,  8, 14,  7, 12, 11, 14,  1,  5,  3,  7,  4,  9,  7,
134   13,
135 };
136
137 static const uint8_t codebook1[] = {
138    1,  4,  2,  6,  3,  7,  5,  7,
139 };
140
141 static const uint8_t codebook2[] = {
142    1,  5,  7, 21,  5,  8,  9, 21, 10,  9, 12, 20, 20, 16, 20,
143   20,  4,  8,  9, 20,  6,  8,  9, 20, 11, 11, 13, 20, 20, 15,
144   17, 20,  9, 11, 14, 20,  8, 10, 15, 20, 11, 13, 15, 20, 20,
145   20, 20, 20, 20, 20, 20, 20, 13, 20, 20, 20, 18, 18, 20, 20,
146   20, 20, 20, 20,  3,  6,  8, 20,  6,  7,  9, 20, 10,  9, 12,
147   20, 20, 20, 20, 20,  5,  7,  9, 20,  6,  6,  9, 20, 10,  9,
148   12, 20, 20, 20, 20, 20,  8, 10, 13, 20,  8,  9, 12, 20, 11,
149   10, 12, 20, 20, 20, 20, 20, 18, 20, 20, 20, 15, 17, 18, 20,
150   18, 17, 18, 20, 20, 20, 20, 20,  7, 10, 12, 20,  8,  9, 11,
151   20, 14, 13, 14, 20, 20, 20, 20, 20,  6,  9, 12, 20,  7,  8,
152   11, 20, 12, 11, 13, 20, 20, 20, 20, 20,  9, 11, 15, 20,  8,
153   10, 14, 20, 12, 11, 14, 20, 20, 20, 20, 20, 20, 20, 20, 20,
154   20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 11, 16, 18,
155   20, 15, 15, 17, 20, 20, 17, 20, 20, 20, 20, 20, 20,  9, 14,
156   16, 20, 12, 12, 15, 20, 17, 15, 18, 20, 20, 20, 20, 20, 16,
157   19, 18, 20, 15, 16, 20, 20, 17, 17, 20, 20, 20, 20, 20, 20,
158   20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
159   20,
160 };
161
162 static const uint8_t codebook3[] = {
163    2,  3,  7, 13,  4,  4,  7, 15,  8,  6,  9, 17, 21, 16, 15,
164   21,  2,  5,  7, 11,  5,  5,  7, 14,  9,  7, 10, 16, 17, 15,
165   16, 21,  4,  7, 10, 17,  7,  7,  9, 15, 11,  9, 11, 16, 21,
166   18, 15, 21, 18, 21, 21, 21, 15, 17, 17, 19, 21, 19, 18, 20,
167   21, 21, 21, 20,
168 };
169
170 static const uint8_t codebook4[] = {
171    5,  5,  5,  5,  6,  5,  6,  5,  6,  5,  6,  5,  6,  5,  6,
172    5,  6,  5,  6,  5,  6,  5,  6,  5,  7,  5,  7,  5,  7,  5,
173    7,  5,  8,  6,  8,  6,  8,  6,  9,  6,  9,  6, 10,  6, 10,
174    6, 11,  6, 11,  7, 11,  7, 12,  7, 12,  7, 12,  7, 12,  7,
175   12,  7, 12,  7, 12,  7, 12,  8, 13,  8, 12,  8, 12,  8, 13,
176    8, 13,  9, 13,  9, 13,  9, 13,  9, 12, 10, 12, 10, 13, 10,
177   14, 11, 14, 12, 14, 13, 14, 13, 14, 14, 15, 16, 15, 15, 15,
178   14, 15, 17, 21, 22, 22, 21, 22, 22, 22, 22, 22, 22, 21, 21,
179   21, 21, 21, 21, 21, 21, 21, 21,
180 };
181
182 static const uint8_t codebook5[] = {
183    2,  5,  5,  4,  5,  4,  5,  4,  5,  4,  6,  5,  6,  5,  6,
184    5,  6,  5,  7,  5,  7,  6,  8,  6,  8,  6,  8,  6,  9,  6,
185    9,  6,
186 };
187
188 static const uint8_t codebook6[] = {
189    8,  5,  8,  4,  9,  4,  9,  4,  9,  4,  9,  4,  9,  4,  9,
190    4,  9,  4,  9,  4,  9,  4,  8,  4,  8,  4,  9,  5,  9,  5,
191    9,  5,  9,  5,  9,  6, 10,  6, 10,  7, 10,  8, 11,  9, 11,
192   11, 12, 13, 12, 14, 13, 15, 13, 15, 14, 16, 14, 17, 15, 17,
193   15, 15, 16, 16, 15, 16, 16, 16, 15, 18, 16, 15, 17, 17, 19,
194   19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
195   19, 19, 19, 19, 19, 19,
196 };
197
198 static const uint8_t codebook7[] = {
199    1,  5,  5,  5,  5,  5,  5,  5,  6,  5,  6,  5,  6,  5,  6,
200    5,  6,  6,  7,  7,  7,  7,  8,  7,  8,  8,  9,  8, 10,  9,
201   10,  9,
202 };
203
204 static const uint8_t codebook8[] = {
205    4,  3,  4,  3,  4,  4,  5,  4,  5,  4,  5,  5,  6,  5,  6,
206    5,  7,  5,  7,  6,  7,  6,  8,  7,  8,  7,  8,  7,  9,  8,
207    9,  9,  9,  9, 10, 10, 10, 11,  9, 12,  9, 12,  9, 15, 10,
208   14,  9, 13, 10, 13, 10, 12, 10, 12, 10, 13, 10, 12, 11, 13,
209   11, 14, 12, 13, 13, 14, 14, 13, 14, 15, 14, 16, 13, 13, 14,
210   16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
211   16, 16, 16, 16, 15, 15,
212 };
213
214 static const uint8_t codebook9[] = {
215    4,  5,  4,  5,  3,  5,  3,  5,  3,  5,  4,  4,  4,  4,  5,
216    5,  5,
217 };
218
219 static const uint8_t codebook10[] = {
220    3,  3,  4,  3,  4,  4,  4,  4,  5,  5,  5,  5,  5,  6,  5,
221    7,  5,  8,  6,  8,  6,  9,  7, 10,  7, 10,  8, 10,  8, 11,
222    9, 11,
223 };
224
225 static const uint8_t codebook11[] = {
226    3,  7,  3,  8,  3, 10,  3,  8,  3,  9,  3,  8,  4,  9,  4,
227    9,  5,  9,  6, 10,  6,  9,  7, 11,  7, 12,  9, 13, 10, 13,
228   12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
229   12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
230   12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
231   12, 12, 12,
232 };
233
234 static const uint8_t codebook12[] = {
235    4,  5,  4,  5,  4,  5,  4,  5,  3,  5,  3,  5,  3,  5,  4,
236    5,  4,
237 };
238
239 static const uint8_t codebook13[] = {
240    4,  2,  4,  2,  5,  3,  5,  4,  6,  6,  6,  7,  7,  8,  7,
241    8,  7,  8,  7,  9,  8,  9,  8,  9,  8, 10,  8, 11,  9, 12,
242    9, 12,
243 };
244
245 static const uint8_t codebook14[] = {
246    2,  5,  2,  6,  3,  6,  4,  7,  4,  7,  5,  9,  5, 11,  6,
247   11,  6, 11,  7, 11,  6, 11,  6, 11,  9, 11,  8, 11, 11, 11,
248   11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
249   11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
250   11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 10, 10, 10,
251   10, 10, 10,
252 };
253
254 static const uint8_t codebook15[] = {
255    5,  6, 11, 11, 11, 11, 10, 10, 12, 11,  5,  2, 11,  5,  6,
256    6,  7,  9, 11, 13, 13, 10,  7, 11,  6,  7,  8,  9, 10, 12,
257   11,  5, 11,  6,  8,  7,  9, 11, 14, 15, 11,  6,  6,  8,  4,
258    5,  7,  8, 10, 13, 10,  5,  7,  7,  5,  5,  6,  8, 10, 11,
259   10,  7,  7,  8,  6,  5,  5,  7,  9,  9, 11,  8,  8, 11,  8,
260    7,  6,  6,  7,  9, 12, 11, 10, 13,  9,  9,  7,  7,  7,  9,
261   11, 13, 12, 15, 12, 11,  9,  8,  8,  8,
262 };
263
264 static const uint8_t codebook16[] = {
265    2,  4,  4,  0,  0,  0,  0,  0,  0,  5,  6,  6,  0,  0,  0,
266    0,  0,  0,  5,  6,  6,  0,  0,  0,  0,  0,  0,  0,  0,  0,
267    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
268    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
269    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
270    0,  0,  0,  0,  0,  0,  5,  7,  7,  0,  0,  0,  0,  0,  0,
271    7,  8,  8,  0,  0,  0,  0,  0,  0,  6,  7,  8,  0,  0,  0,
272    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
273    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
274    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
275    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  5,  7,  7,
276    0,  0,  0,  0,  0,  0,  6,  8,  7,  0,  0,  0,  0,  0,  0,
277    7,  8,  8,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
278    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
279    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
280    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
281    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
282    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
283    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
284    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
285    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
286    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
287    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
288    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
289    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
290    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
291    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
292    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
293    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
294    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
295    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
296    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
297    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
298    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
299    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
300    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
301    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
302    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
303    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
304    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
305    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
306    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
307    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
308    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
309    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
310    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
311    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
312    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
313    0,  0,  0,  0,  0,  0,  0,  0,  0,  5,  7,  7,  0,  0,  0,
314    0,  0,  0,  7,  8,  8,  0,  0,  0,  0,  0,  0,  7,  8,  8,
315    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
316    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
317    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
318    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
319    7,  8,  8,  0,  0,  0,  0,  0,  0,  8,  8,  9,  0,  0,  0,
320    0,  0,  0,  8,  9,  9,  0,  0,  0,  0,  0,  0,  0,  0,  0,
321    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
322    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
323    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
324    0,  0,  0,  0,  0,  0,  6,  8,  8,  0,  0,  0,  0,  0,  0,
325    7,  9,  8,  0,  0,  0,  0,  0,  0,  8,  9,  9,  0,  0,  0,
326    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
327    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
328    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
329    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
330    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
331    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
332    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
333    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
334    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
335    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
336    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
337    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
338    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
339    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
340    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
341    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
342    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
343    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
344    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
345    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
346    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
347    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
348    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
349    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
350    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
351    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
352    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
353    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
354    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
355    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
356    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
357    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
358    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
359    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
360    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
361    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
362    0,  0,  0,  5,  7,  7,  0,  0,  0,  0,  0,  0,  7,  8,  8,
363    0,  0,  0,  0,  0,  0,  7,  8,  8,  0,  0,  0,  0,  0,  0,
364    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
365    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
366    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
367    0,  0,  0,  0,  0,  0,  0,  0,  0,  6,  8,  8,  0,  0,  0,
368    0,  0,  0,  8,  9,  9,  0,  0,  0,  0,  0,  0,  7,  8,  9,
369    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
370    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
371    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
372    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
373    6,  8,  8,  0,  0,  0,  0,  0,  0,  8,  9,  9,  0,  0,  0,
374    0,  0,  0,  8,  9,  8,
375 };
376
377 static const uint8_t codebook17[] = {
378    2,  5,  5,  0,  0,  0,  5,  5,  0,  0,  0,  5,  5,  0,  0,
379    0,  7,  8,  0,  0,  0,  0,  0,  0,  0,  5,  6,  6,  0,  0,
380    0,  7,  7,  0,  0,  0,  7,  7,  0,  0,  0, 10, 10,  0,  0,
381    0,  0,  0,  0,  0,  5,  6,  6,  0,  0,  0,  7,  7,  0,  0,
382    0,  7,  7,  0,  0,  0, 10, 10,  0,  0,  0,  0,  0,  0,  0,
383    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
384    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
385    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
386    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
387    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
388    5,  7,  7,  0,  0,  0,  7,  7,  0,  0,  0,  7,  7,  0,  0,
389    0,  9,  9,  0,  0,  0,  0,  0,  0,  0,  5,  7,  7,  0,  0,
390    0,  7,  7,  0,  0,  0,  7,  7,  0,  0,  0,  9,  9,  0,  0,
391    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
392    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
393    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
394    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
395    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
396    0,  0,  0,  0,  0,  5,  7,  7,  0,  0,  0,  7,  7,  0,  0,
397    0,  7,  7,  0,  0,  0,  9,  9,  0,  0,  0,  0,  0,  0,  0,
398    5,  7,  7,  0,  0,  0,  7,  7,  0,  0,  0,  7,  7,  0,  0,
399    0,  9,  9,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
400    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
401    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
402    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
403    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
404    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  8, 10, 10,  0,  0,
405    0,  9,  9,  0,  0,  0,  9,  9,  0,  0,  0, 10, 10,  0,  0,
406    0,  0,  0,  0,  0,  8, 10, 10,  0,  0,  0,  9,  9,  0,  0,
407    0,  9,  9,  0,  0,  0, 10, 10,
408 };
409
410 static const uint8_t codebook18[] = {
411    2,  4,  3,  6,  6,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
412    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  4,  4,  4,  6,  6,
413    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
414    0,  0,  0,  0,  0,  4,  4,  4,  6,  6,  0,  0,  0,  0,  0,
415    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
416    6,  6,  6,  9,  9,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
417    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  6,  6,  7,  9,  9,
418 };
419
420 static const uint8_t codebook19[] = {
421    2,  3,  3,  6,  6,  0,  0,  0,  0,  0,  4,  4,  6,  6,  0,
422    0,  0,  0,  0,  4,  4,  6,  6,  0,  0,  0,  0,  0,  5,  5,
423    6,  6,  0,  0,  0,  0,  0,  0,  0,  6,  6,  0,  0,  0,  0,
424    0,  0,  0,  7,  8,  0,  0,  0,  0,  0,  0,  0,  7,  7,  0,
425    0,  0,  0,  0,  0,  0,  9,  9,
426 };
427
428 static const uint8_t codebook20[] = {
429    1,  3,  4,  6,  6,  7,  7,  9,  9,  0,  5,  5,  7,  7,  7,
430    8,  9,  9,  0,  5,  5,  7,  7,  8,  8,  9,  9,  0,  7,  7,
431    8,  8,  8,  8, 10, 10,  0,  0,  0,  8,  8,  8,  8, 10, 10,
432    0,  0,  0,  9,  9,  9,  9, 10, 10,  0,  0,  0,  9,  9,  9,
433    9, 10, 10,  0,  0,  0, 10, 10, 10, 10, 11, 11,  0,  0,  0,
434    0,  0, 10, 10, 11, 11,
435 };
436
437 static const uint8_t codebook21[] = {
438    2,  3,  3,  6,  6,  7,  7,  8,  8,  8,  8,  9,  9, 10, 10,
439   11, 10,  0,  5,  5,  7,  7,  8,  8,  9,  9,  9,  9, 10, 10,
440   10, 10, 11, 11,  0,  5,  5,  7,  7,  8,  8,  9,  9,  9,  9,
441   10, 10, 10, 10, 11, 11,  0,  6,  6,  7,  7,  8,  8,  9,  9,
442    9,  9, 10, 10, 11, 11, 11, 11,  0,  0,  0,  7,  7,  8,  8,
443    9,  9,  9,  9, 10, 10, 11, 11, 11, 12,  0,  0,  0,  8,  8,
444    8,  8,  9,  9,  9,  9, 10, 10, 11, 11, 12, 12,  0,  0,  0,
445    8,  8,  8,  8,  9,  9,  9,  9, 10, 10, 11, 11, 12, 12,  0,
446    0,  0,  9,  9,  9,  9, 10, 10, 10, 10, 11, 10, 11, 11, 12,
447   12,  0,  0,  0,  0,  0,  9,  9, 10, 10, 10, 10, 11, 11, 11,
448   11, 12, 12,  0,  0,  0,  0,  0,  9,  8,  9,  9, 10, 10, 11,
449   11, 12, 12, 12, 12,  0,  0,  0,  0,  0,  8,  8,  9,  9, 10,
450   10, 11, 11, 12, 11, 12, 12,  0,  0,  0,  0,  0,  9, 10, 10,
451   10, 11, 11, 11, 11, 12, 12, 13, 13,  0,  0,  0,  0,  0,  0,
452    0, 10, 10, 10, 10, 11, 11, 12, 12, 13, 13,  0,  0,  0,  0,
453    0,  0,  0, 11, 11, 11, 11, 12, 12, 12, 12, 13, 13,  0,  0,
454    0,  0,  0,  0,  0, 11, 11, 11, 11, 12, 12, 12, 12, 13, 13,
455    0,  0,  0,  0,  0,  0,  0, 11, 11, 12, 12, 12, 12, 13, 13,
456   13, 13,  0,  0,  0,  0,  0,  0,  0,  0,  0, 12, 12, 12, 12,
457   13, 13, 13, 13,
458 };
459
460 static const uint8_t codebook22[] = {
461    1,  4,  4,  7,  6,  6,  7,  6,  6,  4,  7,  7, 10,  9,  9,
462   11,  9,  9,  4,  7,  7, 10,  9,  9, 11,  9,  9,  7, 10, 10,
463   11, 11, 10, 12, 11, 11,  6,  9,  9, 11, 10, 10, 11, 10, 10,
464    6,  9,  9, 11, 10, 10, 11, 10, 10,  7, 11, 11, 11, 11, 11,
465   12, 11, 11,  6,  9,  9, 11, 10, 10, 11, 10, 10,  6,  9,  9,
466   11, 10, 10, 11, 10, 10,
467 };
468
469 static const uint8_t codebook23[] = {
470    2,  4,  4,  6,  6,  7,  7,  7,  7,  8,  8, 10,  5,  5,  6,
471    6,  7,  7,  8,  8,  8,  8, 10,  5,  5,  6,  6,  7,  7,  8,
472    8,  8,  8, 10,  6,  6,  7,  7,  8,  8,  8,  8,  8,  8, 10,
473   10, 10,  7,  7,  8,  7,  8,  8,  8,  8, 10, 10, 10,  8,  8,
474    8,  8,  8,  8,  8,  8, 10, 10, 10,  7,  8,  8,  8,  8,  8,
475    8,  8, 10, 10, 10,  8,  8,  8,  8,  8,  8,  8,  8, 10, 10,
476   10, 10, 10,  8,  8,  8,  8,  8,  8, 10, 10, 10, 10, 10,  9,
477    9,  8,  8,  9,  8, 10, 10, 10, 10, 10,  8,  8,  8,  8,  8,
478    8,
479 };
480
481 static const uint8_t codebook24[] = {
482    1,  4,  4,  6,  6,  7,  7,  8,  8,  9,  9, 10, 10,  6,  5,
483    5,  7,  7,  8,  8,  8,  8,  9,  9, 10, 10,  7,  5,  5,  7,
484    7,  8,  8,  8,  8,  9,  9, 11, 10,  0,  8,  8,  8,  8,  9,
485    9,  9,  9, 10, 10, 11, 11,  0,  8,  8,  8,  8,  9,  9,  9,
486    9, 10, 10, 11, 11,  0, 12, 12,  9,  9, 10, 10, 10, 10, 11,
487   11, 11, 12,  0, 13, 13,  9,  9, 10, 10, 10, 10, 11, 11, 12,
488   12,  0,  0,  0, 10, 10, 10, 10, 11, 11, 12, 12, 12, 12,  0,
489    0,  0, 10, 10, 10, 10, 11, 11, 12, 12, 12, 12,  0,  0,  0,
490   14, 14, 11, 11, 11, 11, 12, 12, 13, 13,  0,  0,  0, 14, 14,
491   11, 11, 11, 11, 12, 12, 13, 13,  0,  0,  0,  0,  0, 12, 12,
492   12, 12, 13, 13, 14, 13,  0,  0,  0,  0,  0, 13, 13, 12, 12,
493   13, 12, 14, 13,
494 };
495
496 static const uint8_t codebook25[] = {
497    2,  4,  4,  5,  5,  6,  5,  5,  5,  5,  6,  4,  5,  5,  5,
498    6,  5,  5,  5,  5,  6,  6,  6,  5,  5,
499 };
500
501 static const uint8_t codebook26[] = {
502    1,  4,  4, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,  4,  9,
503    8, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,  2,  9,  7, 12,
504   12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
505   12, 12, 12, 12, 12, 12, 12, 12, 12, 11, 12, 12, 12, 12, 12,
506   12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
507   12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
508   12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
509   12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
510   12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
511   12, 12, 12, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
512   11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
513   11, 11, 11, 11,
514 };
515
516 static const uint8_t codebook27[] = {
517    1,  4,  4,  6,  6,  7,  7,  8,  7,  9,  9, 10, 10, 10, 10,
518    6,  5,  5,  7,  7,  8,  8, 10,  8, 11, 10, 12, 12, 13, 13,
519    6,  5,  5,  7,  7,  8,  8, 10,  9, 11, 11, 12, 12, 13, 12,
520   18,  8,  8,  8,  8,  9,  9, 10,  9, 11, 10, 12, 12, 13, 13,
521   18,  8,  8,  8,  8,  9,  9, 10, 10, 11, 11, 13, 12, 14, 13,
522   18, 11, 11,  9,  9, 10, 10, 11, 11, 11, 12, 13, 12, 13, 14,
523   18, 11, 11,  9,  8, 11, 10, 11, 11, 11, 11, 12, 12, 14, 13,
524   18, 18, 18, 10, 11, 10, 11, 12, 12, 12, 12, 13, 12, 14, 13,
525   18, 18, 18, 10, 11, 11,  9, 12, 11, 12, 12, 12, 13, 13, 13,
526   18, 18, 17, 14, 14, 11, 11, 12, 12, 13, 12, 14, 12, 14, 13,
527   18, 18, 18, 14, 14, 11, 10, 12,  9, 12, 13, 13, 13, 13, 13,
528   18, 18, 17, 16, 18, 13, 13, 12, 12, 13, 11, 14, 12, 14, 14,
529   17, 18, 18, 17, 18, 13, 12, 13, 10, 12, 11, 14, 14, 14, 14,
530   17, 18, 18, 18, 18, 15, 16, 12, 12, 13, 10, 14, 12, 14, 15,
531   18, 18, 18, 16, 17, 16, 14, 12, 11, 13, 10, 13, 13, 14, 15,
532 };
533
534 static const uint8_t codebook28[] = {
535    2,  5,  5,  6,  6,  7,  7,  7,  7,  7,  7,  8,  8,  8,  8,
536    8,  8, 10,  6,  6,  7,  7,  8,  7,  8,  8,  8,  8,  8,  9,
537    9,  9,  9,  9, 10,  6,  6,  7,  7,  7,  7,  8,  8,  8,  8,
538    9,  9,  9,  9,  9,  9, 10,  7,  7,  7,  7,  8,  8,  8,  8,
539    9,  9,  9,  9,  9,  9,  9,  9, 10, 10, 10,  7,  7,  8,  8,
540    8,  9,  9,  9,  9,  9,  9,  9,  9,  9, 11, 11, 11,  8,  8,
541    8,  8,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9, 10, 10, 10,
542    8,  8,  8,  8,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9, 10,
543   10, 10,  8,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9, 10,
544    9, 10, 10, 10, 11, 11,  9,  9,  9,  9,  9,  9,  9,  9,  9,
545    9,  9,  9, 11, 10, 11, 11, 11,  9,  9,  9,  9,  9,  9, 10,
546   10,  9,  9, 10,  9, 11, 10, 11, 11, 11,  9,  9,  9,  9,  9,
547    9,  9,  9, 10, 10, 10,  9, 11, 11, 11, 11, 11,  9,  9,  9,
548    9, 10, 10,  9,  9,  9,  9, 10,  9, 11, 11, 11, 11, 11, 11,
549   11,  9,  9,  9,  9,  9,  9, 10, 10, 10, 10, 11, 11, 11, 11,
550   11, 11, 11, 10,  9, 10, 10,  9, 10,  9,  9, 10,  9, 11, 10,
551   10, 11, 11, 11, 11,  9, 10,  9,  9,  9,  9, 10, 10, 10, 10,
552   11, 11, 11, 11, 11, 11, 10, 10, 10,  9,  9, 10,  9, 10,  9,
553   10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11,  9,  9,  9,  9,
554    9, 10, 10, 10,
555 };
556
557 static const struct {
558     int dim;
559     int len;
560     int real_len;
561     const uint8_t * clens;
562     int lookup;
563     float min;
564     float delta;
565     const uint8_t * quant;
566 } cvectors[] = {
567     { 2,   16,   16, codebook0,  0 },
568     { 2,    8,    8, codebook1,  0 },
569     { 2,  256,  256, codebook2,  0 },
570     { 2,   64,   64, codebook3,  0 },
571     { 2,  128,  128, codebook4,  0 },
572     { 2,   32,   32, codebook5,  0 },
573     { 2,   96,   96, codebook6,  0 },
574     { 2,   32,   32, codebook7,  0 },
575     { 2,   96,   96, codebook8,  0 },
576     { 2,   17,   17, codebook9,  0 },
577     { 2,   32,   32, codebook10, 0 },
578     { 2,   78,   78, codebook11, 0 },
579     { 2,   17,   17, codebook12, 0 },
580     { 2,   32,   32, codebook13, 0 },
581     { 2,   78,   78, codebook14, 0 },
582     { 2,  100,  100, codebook15, 0 },
583     { 8, 1641, 6561, codebook16, 1,    -1.0,   1.0, (const uint8_t[]){ 1, 0, 2, } },
584     { 4,  443,  625, codebook17, 1,    -2.0,   1.0, (const uint8_t[]){ 2, 1, 3, 0, 4, } },
585     { 4,  105,  625, codebook18, 1,    -2.0,   1.0, (const uint8_t[]){ 2, 1, 3, 0, 4, } },
586     { 2,   68,   81, codebook19, 1,    -4.0,   1.0, (const uint8_t[]){ 4, 3, 5, 2, 6, 1, 7, 0, 8, } },
587     { 2,   81,   81, codebook20, 1,    -4.0,   1.0, (const uint8_t[]){ 4, 3, 5, 2, 6, 1, 7, 0, 8, } },
588     { 2,  289,  289, codebook21, 1,    -8.0,   1.0, (const uint8_t[]){ 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15, 0, 16, } },
589     { 4,   81,   81, codebook22, 1,   -11.0,  11.0, (const uint8_t[]){ 1, 0, 2, } },
590     { 2,  121,  121, codebook23, 1,    -5.0,   1.0, (const uint8_t[]){ 5, 4, 6, 3, 7, 2, 8, 1, 9, 0, 10, } },
591     { 2,  169,  169, codebook24, 1,   -30.0,   5.0, (const uint8_t[]){ 6, 5, 7, 4, 8, 3, 9, 2, 10, 1, 11, 0, 12, } },
592     { 2,   25,   25, codebook25, 1,    -2.0,   1.0, (const uint8_t[]){ 2, 1, 3, 0, 4, } },
593     { 2,  169,  169, codebook26, 1, -1530.0, 255.0, (const uint8_t[]){ 6, 5, 7, 4, 8, 3, 9, 2, 10, 1, 11, 0, 12, } },
594     { 2,  225,  225, codebook27, 1,  -119.0,  17.0, (const uint8_t[]){ 7, 6, 8, 5, 9, 4, 10, 3, 11, 2, 12, 1, 13, 0, 14, } },
595     { 2,  289,  289, codebook28, 1,    -8.0,   1.0, (const uint8_t[]){ 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15, 0, 16, } },
596 };
597
598 static inline void init_put_bits(PutBitContext * pb, uint8_t * buf, int buffer_len) {
599     pb->total = buffer_len * 8;
600     pb->total_pos = 0;
601     pb->pos = 0;
602     pb->buf_ptr = buf;
603 }
604
605 static void put_bits(PutBitContext * pb, int bits, uint64_t val) {
606     if ((pb->total_pos += bits) >= pb->total) return;
607     if (!bits) return;
608     if (pb->pos) {
609         if (pb->pos > bits) {
610             *pb->buf_ptr |= val << (8 - pb->pos);
611             pb->pos -= bits;
612             bits = 0;
613         } else {
614             *pb->buf_ptr++ |= (val << (8 - pb->pos)) & 0xFF;
615             val >>= pb->pos;
616             bits -= pb->pos;
617             pb->pos = 0;
618         }
619     }
620     for (; bits >= 8; bits -= 8) {
621         *pb->buf_ptr++ = val & 0xFF;
622         val >>= 8;
623     }
624     if (bits) {
625         *pb->buf_ptr = val;
626         pb->pos = 8 - bits;
627     }
628 }
629
630 static inline void flush_put_bits(PutBitContext * pb) {
631 }
632
633 static inline int put_bits_count(PutBitContext * pb) {
634     return pb->total_pos;
635 }
636
637 static inline void put_codeword(PutBitContext * pb, codebook_t * cb, int entry) {
638     assert(entry >= 0);
639     assert(entry < cb->nentries);
640     assert(cb->lens[entry]);
641     put_bits(pb, cb->lens[entry], cb->codewords[entry]);
642 }
643
644 static int cb_lookup_vals(int lookup, int dimentions, int entries) {
645     if (lookup == 1) return ff_vorbis_nth_root(entries, dimentions);
646     else if (lookup == 2) return dimentions * entries;
647     return 0;
648 }
649
650 static void ready_codebook(codebook_t * cb) {
651     int i;
652
653     ff_vorbis_len2vlc(cb->lens, cb->codewords, cb->nentries);
654
655     if (!cb->lookup) cb->dimentions = NULL;
656     else {
657         int vals = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries);
658         cb->dimentions = av_malloc(sizeof(float) * cb->nentries * cb->ndimentions);
659         for (i = 0; i < cb->nentries; i++) {
660             float last = 0;
661             int j;
662             int div = 1;
663             for (j = 0; j < cb->ndimentions; j++) {
664                 int off;
665                 if (cb->lookup == 1) off = (i / div) % vals; // lookup type 1
666                 else off = i * cb->ndimentions + j; // lookup type 2
667
668                 cb->dimentions[i * cb->ndimentions + j] = last + cb->min + cb->quantlist[off] * cb->delta;
669                 if (cb->seq_p) last = cb->dimentions[i * cb->ndimentions + j];
670                 div *= vals;
671             }
672         }
673     }
674
675 }
676
677 static void ready_floor(floor_t * fc) {
678     int i;
679     fc->list[0].sort = 0;
680     fc->list[1].sort = 1;
681     for (i = 2; i < fc->values; i++) {
682         int j;
683         fc->list[i].low = 0;
684         fc->list[i].high = 1;
685         fc->list[i].sort = i;
686         for (j = 2; j < i; j++) {
687             int tmp = fc->list[j].x;
688             if (tmp < fc->list[i].x) {
689                 if (tmp > fc->list[fc->list[i].low].x) fc->list[i].low = j;
690             } else {
691                 if (tmp < fc->list[fc->list[i].high].x) fc->list[i].high = j;
692             }
693         }
694     }
695     for (i = 0; i < fc->values - 1; i++) {
696         int j;
697         for (j = i + 1; j < fc->values; j++) {
698             if (fc->list[fc->list[i].sort].x > fc->list[fc->list[j].sort].x) {
699                 int tmp = fc->list[i].sort;
700                 fc->list[i].sort = fc->list[j].sort;
701                 fc->list[j].sort = tmp;
702             }
703         }
704     }
705 }
706
707 static void ready_residue(residue_t * rc, venc_context_t * venc) {
708     int i;
709     assert(rc->type == 2);
710     rc->maxes = av_mallocz(sizeof(float[2]) * rc->classifications);
711     for (i = 0; i < rc->classifications; i++) {
712         int j;
713         codebook_t * cb;
714         for (j = 0; j < 8; j++) if (rc->books[i][j] != -1) break;
715         if (j == 8) continue; // zero
716         cb = &venc->codebooks[rc->books[i][j]];
717         assert(cb->ndimentions >= 2);
718         assert(cb->lookup);
719
720         for (j = 0; j < cb->nentries; j++) {
721             float a;
722             if (!cb->lens[j]) continue;
723             a = fabs(cb->dimentions[j * cb->ndimentions]);
724             if (a > rc->maxes[i][0]) rc->maxes[i][0] = a;
725             a = fabs(cb->dimentions[j * cb->ndimentions + 1]);
726             if (a > rc->maxes[i][1]) rc->maxes[i][1] = a;
727         }
728     }
729     // small bias
730     for (i = 0; i < rc->classifications; i++) {
731         rc->maxes[i][0] += 0.8;
732         rc->maxes[i][1] += 0.8;
733     }
734 }
735
736 static void create_vorbis_context(venc_context_t * venc, AVCodecContext * avccontext) {
737     floor_t * fc;
738     residue_t * rc;
739     mapping_t * mc;
740     int i, book;
741
742     venc->channels = avccontext->channels;
743     venc->sample_rate = avccontext->sample_rate;
744     venc->blocksize[0] = venc->blocksize[1] = 11;
745
746     venc->ncodebooks = sizeof(cvectors)/sizeof(cvectors[0]);
747     venc->codebooks = av_malloc(sizeof(codebook_t) * venc->ncodebooks);
748
749     // codebook 0..14 - floor1 book, values 0..255
750     // codebook 15 residue masterbook
751     // codebook 16..29 residue
752     for (book = 0; book < venc->ncodebooks; book++) {
753         codebook_t * cb = &venc->codebooks[book];
754         int vals;
755         cb->ndimentions = cvectors[book].dim;
756         cb->nentries = cvectors[book].real_len;
757         cb->min = cvectors[book].min;
758         cb->delta = cvectors[book].delta;
759         cb->lookup = cvectors[book].lookup;
760         cb->seq_p = 0;
761
762         cb->lens = av_malloc(sizeof(uint8_t) * cb->nentries);
763         cb->codewords = av_malloc(sizeof(uint32_t) * cb->nentries);
764         memcpy(cb->lens, cvectors[book].clens, cvectors[book].len);
765         memset(cb->lens + cvectors[book].len, 0, cb->nentries - cvectors[book].len);
766
767         if (cb->lookup) {
768             vals = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries);
769             cb->quantlist = av_malloc(sizeof(int) * vals);
770             for (i = 0; i < vals; i++) cb->quantlist[i] = cvectors[book].quant[i];
771         } else {
772             cb->quantlist = NULL;
773         }
774         ready_codebook(cb);
775     }
776
777     venc->nfloors = 1;
778     venc->floors = av_malloc(sizeof(floor_t) * venc->nfloors);
779
780     // just 1 floor
781     fc = &venc->floors[0];
782     fc->partitions = 8;
783     fc->partition_to_class = av_malloc(sizeof(int) * fc->partitions);
784     fc->nclasses = 0;
785     for (i = 0; i < fc->partitions; i++) {
786         int a[] = {0,1,2,2,3,3,4,4};
787         fc->partition_to_class[i] = a[i];
788         fc->nclasses = FFMAX(fc->nclasses, fc->partition_to_class[i]);
789     }
790     fc->nclasses++;
791     fc->classes = av_malloc(sizeof(floor_class_t) * fc->nclasses);
792     for (i = 0; i < fc->nclasses; i++) {
793         floor_class_t * c = &fc->classes[i];
794         int j, books;
795         int dim[] = {3,4,3,4,3};
796         int subclass[] = {0,1,1,2,2};
797         int masterbook[] = {0/*none*/,0,1,2,3};
798         int * nbooks[] = {
799             (int[]){ 4 },
800             (int[]){ 5, 6 },
801             (int[]){ 7, 8 },
802             (int[]){ -1, 9, 10, 11 },
803             (int[]){ -1, 12, 13, 14 },
804         };
805         c->dim = dim[i];
806         c->subclass = subclass[i];
807         c->masterbook = masterbook[i];
808         books = (1 << c->subclass);
809         c->books = av_malloc(sizeof(int) * books);
810         for (j = 0; j < books; j++) c->books[j] = nbooks[i][j];
811     }
812     fc->multiplier = 2;
813     fc->rangebits = venc->blocksize[0] - 1;
814
815     fc->values = 2;
816     for (i = 0; i < fc->partitions; i++)
817         fc->values += fc->classes[fc->partition_to_class[i]].dim;
818
819     fc->list = av_malloc(sizeof(floor_entry_t) * fc->values);
820     fc->list[0].x = 0;
821     fc->list[1].x = 1 << fc->rangebits;
822     for (i = 2; i < fc->values; i++) {
823         static const int a[] = {
824              93, 23,372,  6, 46,186,750, 14, 33, 65,
825             130,260,556,  3, 10, 18, 28, 39, 55, 79,
826             111,158,220,312,464,650,850
827         };
828         fc->list[i].x = a[i - 2];
829     }
830     ready_floor(fc);
831
832     venc->nresidues = 1;
833     venc->residues = av_malloc(sizeof(residue_t) * venc->nresidues);
834
835     // single residue
836     rc = &venc->residues[0];
837     rc->type = 2;
838     rc->begin = 0;
839     rc->end = 1600;
840     rc->partition_size = 32;
841     rc->classifications = 10;
842     rc->classbook = 15;
843     rc->books = av_malloc(sizeof(*rc->books) * rc->classifications);
844     {
845         static const int8_t a[10][8] = {
846             { -1, -1, -1, -1, -1, -1, -1, -1, },
847             { -1, -1, 16, -1, -1, -1, -1, -1, },
848             { -1, -1, 17, -1, -1, -1, -1, -1, },
849             { -1, -1, 18, -1, -1, -1, -1, -1, },
850             { -1, -1, 19, -1, -1, -1, -1, -1, },
851             { -1, -1, 20, -1, -1, -1, -1, -1, },
852             { -1, -1, 21, -1, -1, -1, -1, -1, },
853             { 22, 23, -1, -1, -1, -1, -1, -1, },
854             { 24, 25, -1, -1, -1, -1, -1, -1, },
855             { 26, 27, 28, -1, -1, -1, -1, -1, },
856         };
857         memcpy(rc->books, a, sizeof a);
858     }
859     ready_residue(rc, venc);
860
861     venc->nmappings = 1;
862     venc->mappings = av_malloc(sizeof(mapping_t) * venc->nmappings);
863
864     // single mapping
865     mc = &venc->mappings[0];
866     mc->submaps = 1;
867     mc->mux = av_malloc(sizeof(int) * venc->channels);
868     for (i = 0; i < venc->channels; i++) mc->mux[i] = 0;
869     mc->floor = av_malloc(sizeof(int) * mc->submaps);
870     mc->residue = av_malloc(sizeof(int) * mc->submaps);
871     for (i = 0; i < mc->submaps; i++) {
872         mc->floor[i] = 0;
873         mc->residue[i] = 0;
874     }
875     mc->coupling_steps = venc->channels == 2 ? 1 : 0;
876     mc->magnitude = av_malloc(sizeof(int) * mc->coupling_steps);
877     mc->angle = av_malloc(sizeof(int) * mc->coupling_steps);
878     if (mc->coupling_steps) {
879         mc->magnitude[0] = 0;
880         mc->angle[0] = 1;
881     }
882
883     venc->nmodes = 1;
884     venc->modes = av_malloc(sizeof(vorbis_mode_t) * venc->nmodes);
885
886     // single mode
887     venc->modes[0].blockflag = 0;
888     venc->modes[0].mapping = 0;
889
890     venc->have_saved = 0;
891     venc->saved = av_malloc(sizeof(float) * venc->channels * (1 << venc->blocksize[1]) / 2);
892     venc->samples = av_malloc(sizeof(float) * venc->channels * (1 << venc->blocksize[1]));
893     venc->floor = av_malloc(sizeof(float) * venc->channels * (1 << venc->blocksize[1]) / 2);
894     venc->coeffs = av_malloc(sizeof(float) * venc->channels * (1 << venc->blocksize[1]) / 2);
895
896     venc->win[0] = ff_vorbis_vwin[venc->blocksize[0] - 6];
897     venc->win[1] = ff_vorbis_vwin[venc->blocksize[1] - 6];
898
899     ff_mdct_init(&venc->mdct[0], venc->blocksize[0], 0);
900     ff_mdct_init(&venc->mdct[1], venc->blocksize[1], 0);
901 }
902
903 static void put_float(PutBitContext * pb, float f) {
904     int exp, mant;
905     uint32_t res = 0;
906     mant = (int)ldexp(frexp(f, &exp), 20);
907     exp += 788 - 20;
908     if (mant < 0) { res |= (1 << 31); mant = -mant; }
909     res |= mant | (exp << 21);
910     put_bits(pb, 32, res);
911 }
912
913 static void put_codebook_header(PutBitContext * pb, codebook_t * cb) {
914     int i;
915     int ordered = 0;
916
917     put_bits(pb, 24, 0x564342); //magic
918     put_bits(pb, 16, cb->ndimentions);
919     put_bits(pb, 24, cb->nentries);
920
921     for (i = 1; i < cb->nentries; i++) if (cb->lens[i] < cb->lens[i-1]) break;
922     if (i == cb->nentries) ordered = 1;
923
924     put_bits(pb, 1, ordered);
925     if (ordered) {
926         int len = cb->lens[0];
927         put_bits(pb, 5, len - 1);
928         i = 0;
929         while (i < cb->nentries) {
930             int j;
931             for (j = 0; j+i < cb->nentries; j++) if (cb->lens[j+i] != len) break;
932             put_bits(pb, ilog(cb->nentries - i), j);
933             i += j;
934             len++;
935         }
936     } else {
937         int sparse = 0;
938         for (i = 0; i < cb->nentries; i++) if (!cb->lens[i]) break;
939         if (i != cb->nentries) sparse = 1;
940         put_bits(pb, 1, sparse);
941
942         for (i = 0; i < cb->nentries; i++) {
943             if (sparse) put_bits(pb, 1, !!cb->lens[i]);
944             if (cb->lens[i]) put_bits(pb, 5, cb->lens[i] - 1);
945         }
946     }
947
948     put_bits(pb, 4, cb->lookup);
949     if (cb->lookup) {
950         int tmp = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries);
951         int bits = ilog(cb->quantlist[0]);
952
953         for (i = 1; i < tmp; i++) bits = FFMAX(bits, ilog(cb->quantlist[i]));
954
955         put_float(pb, cb->min);
956         put_float(pb, cb->delta);
957
958         put_bits(pb, 4, bits - 1);
959         put_bits(pb, 1, cb->seq_p);
960
961         for (i = 0; i < tmp; i++) put_bits(pb, bits, cb->quantlist[i]);
962     }
963 }
964
965 static void put_floor_header(PutBitContext * pb, floor_t * fc) {
966     int i;
967
968     put_bits(pb, 16, 1); // type, only floor1 is supported
969
970     put_bits(pb, 5, fc->partitions);
971
972     for (i = 0; i < fc->partitions; i++) put_bits(pb, 4, fc->partition_to_class[i]);
973
974     for (i = 0; i < fc->nclasses; i++) {
975         int j, books;
976
977         put_bits(pb, 3, fc->classes[i].dim - 1);
978         put_bits(pb, 2, fc->classes[i].subclass);
979
980         if (fc->classes[i].subclass) put_bits(pb, 8, fc->classes[i].masterbook);
981
982         books = (1 << fc->classes[i].subclass);
983
984         for (j = 0; j < books; j++) put_bits(pb, 8, fc->classes[i].books[j] + 1);
985     }
986
987     put_bits(pb, 2, fc->multiplier - 1);
988     put_bits(pb, 4, fc->rangebits);
989
990     for (i = 2; i < fc->values; i++) put_bits(pb, fc->rangebits, fc->list[i].x);
991 }
992
993 static void put_residue_header(PutBitContext * pb, residue_t * rc) {
994     int i;
995
996     put_bits(pb, 16, rc->type);
997
998     put_bits(pb, 24, rc->begin);
999     put_bits(pb, 24, rc->end);
1000     put_bits(pb, 24, rc->partition_size - 1);
1001     put_bits(pb, 6, rc->classifications - 1);
1002     put_bits(pb, 8, rc->classbook);
1003
1004     for (i = 0; i < rc->classifications; i++) {
1005         int j, tmp = 0;
1006         for (j = 0; j < 8; j++) tmp |= (rc->books[i][j] != -1) << j;
1007
1008         put_bits(pb, 3, tmp & 7);
1009         put_bits(pb, 1, tmp > 7);
1010
1011         if (tmp > 7) put_bits(pb, 5, tmp >> 3);
1012     }
1013
1014     for (i = 0; i < rc->classifications; i++) {
1015         int j;
1016         for (j = 0; j < 8; j++)
1017             if (rc->books[i][j] != -1)
1018                 put_bits(pb, 8, rc->books[i][j]);
1019     }
1020 }
1021
1022 static int put_main_header(venc_context_t * venc, uint8_t ** out) {
1023     int i;
1024     PutBitContext pb;
1025     uint8_t buffer[50000] = {0}, * p = buffer;
1026     int buffer_len = sizeof buffer;
1027     int len, hlens[3];
1028
1029     // identification header
1030     init_put_bits(&pb, p, buffer_len);
1031     put_bits(&pb, 8, 1); //magic
1032     for (i = 0; "vorbis"[i]; i++) put_bits(&pb, 8, "vorbis"[i]);
1033     put_bits(&pb, 32, 0); // version
1034     put_bits(&pb, 8, venc->channels);
1035     put_bits(&pb, 32, venc->sample_rate);
1036     put_bits(&pb, 32, 0); // bitrate
1037     put_bits(&pb, 32, 0); // bitrate
1038     put_bits(&pb, 32, 0); // bitrate
1039     put_bits(&pb, 4, venc->blocksize[0]);
1040     put_bits(&pb, 4, venc->blocksize[1]);
1041     put_bits(&pb, 1, 1); // framing
1042
1043     flush_put_bits(&pb);
1044     hlens[0] = (put_bits_count(&pb) + 7) / 8;
1045     buffer_len -= hlens[0];
1046     p += hlens[0];
1047
1048     // comment header
1049     init_put_bits(&pb, p, buffer_len);
1050     put_bits(&pb, 8, 3); //magic
1051     for (i = 0; "vorbis"[i]; i++) put_bits(&pb, 8, "vorbis"[i]);
1052     put_bits(&pb, 32, 0); // vendor length TODO
1053     put_bits(&pb, 32, 0); // amount of comments
1054     put_bits(&pb, 1, 1); // framing
1055
1056     flush_put_bits(&pb);
1057     hlens[1] = (put_bits_count(&pb) + 7) / 8;
1058     buffer_len -= hlens[1];
1059     p += hlens[1];
1060
1061     // setup header
1062     init_put_bits(&pb, p, buffer_len);
1063     put_bits(&pb, 8, 5); //magic
1064     for (i = 0; "vorbis"[i]; i++) put_bits(&pb, 8, "vorbis"[i]);
1065
1066     // codebooks
1067     put_bits(&pb, 8, venc->ncodebooks - 1);
1068     for (i = 0; i < venc->ncodebooks; i++) put_codebook_header(&pb, &venc->codebooks[i]);
1069
1070     // time domain, reserved, zero
1071     put_bits(&pb, 6, 0);
1072     put_bits(&pb, 16, 0);
1073
1074     // floors
1075     put_bits(&pb, 6, venc->nfloors - 1);
1076     for (i = 0; i < venc->nfloors; i++) put_floor_header(&pb, &venc->floors[i]);
1077
1078     // residues
1079     put_bits(&pb, 6, venc->nresidues - 1);
1080     for (i = 0; i < venc->nresidues; i++) put_residue_header(&pb, &venc->residues[i]);
1081
1082     // mappings
1083     put_bits(&pb, 6, venc->nmappings - 1);
1084     for (i = 0; i < venc->nmappings; i++) {
1085         mapping_t * mc = &venc->mappings[i];
1086         int j;
1087         put_bits(&pb, 16, 0); // mapping type
1088
1089         put_bits(&pb, 1, mc->submaps > 1);
1090         if (mc->submaps > 1) put_bits(&pb, 4, mc->submaps - 1);
1091
1092         put_bits(&pb, 1, !!mc->coupling_steps);
1093         if (mc->coupling_steps) {
1094             put_bits(&pb, 8, mc->coupling_steps - 1);
1095             for (j = 0; j < mc->coupling_steps; j++) {
1096                 put_bits(&pb, ilog(venc->channels - 1), mc->magnitude[j]);
1097                 put_bits(&pb, ilog(venc->channels - 1), mc->angle[j]);
1098             }
1099         }
1100
1101         put_bits(&pb, 2, 0); // reserved
1102
1103         if (mc->submaps > 1) for (j = 0; j < venc->channels; j++) put_bits(&pb, 4, mc->mux[j]);
1104
1105         for (j = 0; j < mc->submaps; j++) {
1106             put_bits(&pb, 8, 0); // reserved time configuration
1107             put_bits(&pb, 8, mc->floor[j]);
1108             put_bits(&pb, 8, mc->residue[j]);
1109         }
1110     }
1111
1112     // modes
1113     put_bits(&pb, 6, venc->nmodes - 1);
1114     for (i = 0; i < venc->nmodes; i++) {
1115         put_bits(&pb, 1, venc->modes[i].blockflag);
1116         put_bits(&pb, 16, 0); // reserved window type
1117         put_bits(&pb, 16, 0); // reserved transform type
1118         put_bits(&pb, 8, venc->modes[i].mapping);
1119     }
1120
1121     put_bits(&pb, 1, 1); // framing
1122
1123     flush_put_bits(&pb);
1124     hlens[2] = (put_bits_count(&pb) + 7) / 8;
1125
1126     len = hlens[0] + hlens[1] + hlens[2];
1127     p = *out = av_mallocz(64 + len + len/255);
1128
1129     *p++ = 2;
1130     p += av_xiphlacing(p, hlens[0]);
1131     p += av_xiphlacing(p, hlens[1]);
1132     buffer_len = 0;
1133     for (i = 0; i < 3; i++) {
1134         memcpy(p, buffer + buffer_len, hlens[i]);
1135         p += hlens[i];
1136         buffer_len += hlens[i];
1137     }
1138
1139     return p - *out;
1140 }
1141
1142 static float get_floor_average(floor_t * fc, float * coeffs, int i) {
1143     int begin = fc->list[fc->list[FFMAX(i-1, 0)].sort].x;
1144     int end   = fc->list[fc->list[FFMIN(i+1, fc->values - 1)].sort].x;
1145     int j;
1146     float average = 0;
1147
1148     for (j = begin; j < end; j++) average += fabs(coeffs[j]);
1149     return average / (end - begin);
1150 }
1151
1152 static void floor_fit(venc_context_t * venc, floor_t * fc, float * coeffs, int * posts, int samples) {
1153     int range = 255 / fc->multiplier + 1;
1154     int i;
1155     float tot_average = 0.;
1156     for (i = 0; i < fc->values; i++) tot_average += get_floor_average(fc, coeffs, i);
1157     tot_average /= fc->values;
1158     tot_average /= venc->quality;
1159
1160     for (i = 0; i < fc->values; i++) {
1161         int position = fc->list[fc->list[i].sort].x;
1162         float average = get_floor_average(fc, coeffs, i);
1163         int j;
1164
1165         average /= pow(average, 0.5) / tot_average * pow(0.8, position/200.); // MAGIC!
1166         for (j = 0; j < range - 1; j++) if (ff_vorbis_floor1_inverse_db_table[j * fc->multiplier] > average) break;
1167         posts[fc->list[i].sort] = j;
1168     }
1169 }
1170
1171 static int render_point(int x0, int y0, int x1, int y1, int x) {
1172     return y0 +  (x - x0) * (y1 - y0) / (x1 - x0);
1173 }
1174
1175 static void floor_encode(venc_context_t * venc, floor_t * fc, PutBitContext * pb, int * posts, float * floor, int samples) {
1176     int range = 255 / fc->multiplier + 1;
1177     int coded[fc->values]; // first 2 values are unused
1178     int i, counter;
1179     int lx, ly;
1180
1181     put_bits(pb, 1, 1); // non zero
1182     put_bits(pb, ilog(range - 1), posts[0]);
1183     put_bits(pb, ilog(range - 1), posts[1]);
1184     coded[0] = coded[1] = 1;
1185
1186     for (i = 2; i < fc->values; i++) {
1187         int predicted = render_point(fc->list[fc->list[i].low].x,
1188                                      posts[fc->list[i].low],
1189                                      fc->list[fc->list[i].high].x,
1190                                      posts[fc->list[i].high],
1191                                      fc->list[i].x);
1192         int highroom = range - predicted;
1193         int lowroom = predicted;
1194         int room = FFMIN(highroom, lowroom);
1195         if (predicted == posts[i]) {
1196             coded[i] = 0; // must be used later as flag!
1197             continue;
1198         } else {
1199             if (!coded[fc->list[i].low]) coded[fc->list[i].low] = -1;
1200             if (!coded[fc->list[i].high]) coded[fc->list[i].high] = -1;
1201         }
1202         if (posts[i] > predicted) {
1203             if (posts[i] - predicted > room) coded[i] = posts[i] - predicted + lowroom;
1204             else coded[i] = (posts[i] - predicted) << 1;
1205         } else {
1206             if (predicted - posts[i] > room) coded[i] = predicted - posts[i] + highroom - 1;
1207             else coded[i] = ((predicted - posts[i]) << 1) - 1;
1208         }
1209     }
1210
1211     counter = 2;
1212     for (i = 0; i < fc->partitions; i++) {
1213         floor_class_t * c = &fc->classes[fc->partition_to_class[i]];
1214         int k, cval = 0, csub = 1<<c->subclass;
1215         if (c->subclass) {
1216             codebook_t * book = &venc->codebooks[c->masterbook];
1217             int cshift = 0;
1218             for (k = 0; k < c->dim; k++) {
1219                 int l;
1220                 for (l = 0; l < csub; l++) {
1221                     int maxval = 1;
1222                     if (c->books[l] != -1) maxval = venc->codebooks[c->books[l]].nentries;
1223                     // coded could be -1, but this still works, cause thats 0
1224                     if (coded[counter + k] < maxval) break;
1225                 }
1226                 assert(l != csub);
1227                 cval |= l << cshift;
1228                 cshift += c->subclass;
1229             }
1230             put_codeword(pb, book, cval);
1231         }
1232         for (k = 0; k < c->dim; k++) {
1233             int book = c->books[cval & (csub-1)];
1234             int entry = coded[counter++];
1235             cval >>= c->subclass;
1236             if (book == -1) continue;
1237             if (entry == -1) entry = 0;
1238             put_codeword(pb, &venc->codebooks[book], entry);
1239         }
1240     }
1241
1242     lx = 0;
1243     ly = posts[0] * fc->multiplier; // sorted 0 is still 0
1244     for (i = 1; i < fc->values; i++) {
1245         int pos = fc->list[i].sort;
1246         if (coded[pos]) {
1247             render_line(lx, ly, fc->list[pos].x, posts[pos] * fc->multiplier, floor, samples);
1248             lx = fc->list[pos].x;
1249             ly = posts[pos] * fc->multiplier;
1250         }
1251         if (lx >= samples) break;
1252     }
1253     if (lx < samples) render_line(lx, ly, samples, ly, floor, samples);
1254 }
1255
1256 static float * put_vector(codebook_t * book, PutBitContext * pb, float * num) {
1257     int i;
1258     int entry = -1;
1259     float distance = 0;
1260     assert(book->dimentions);
1261     for (i = 0; i < book->nentries; i++) {
1262         float d = 0.;
1263         int j;
1264         if (!book->lens[i]) continue;
1265         for (j = 0; j < book->ndimentions; j++) {
1266             float a = (book->dimentions[i * book->ndimentions + j] - num[j]);
1267             d += a*a;
1268         }
1269         if (entry == -1 || distance > d) {
1270             entry = i;
1271             distance = d;
1272         }
1273     }
1274     put_codeword(pb, book, entry);
1275     return &book->dimentions[entry * book->ndimentions];
1276 }
1277
1278 static void residue_encode(venc_context_t * venc, residue_t * rc, PutBitContext * pb, float * coeffs, int samples, int real_ch) {
1279     int pass, i, j, p, k;
1280     int psize = rc->partition_size;
1281     int partitions = (rc->end - rc->begin) / psize;
1282     int channels = (rc->type == 2) ? 1 : real_ch;
1283     int classes[channels][partitions];
1284     int classwords = venc->codebooks[rc->classbook].ndimentions;
1285
1286     assert(rc->type == 2);
1287     assert(real_ch == 2);
1288     for (p = 0; p < partitions; p++) {
1289         float max1 = 0., max2 = 0.;
1290         int s = rc->begin + p * psize;
1291         for (k = s; k < s + psize; k += 2) {
1292             if (fabs(coeffs[k / real_ch]) > max1) max1 = fabs(coeffs[k / real_ch]);
1293             if (fabs(coeffs[samples + k / real_ch]) > max2) max2 = fabs(coeffs[samples + k / real_ch]);
1294         }
1295
1296         for (i = 0; i < rc->classifications - 1; i++) {
1297             if (max1 < rc->maxes[i][0] && max2 < rc->maxes[i][1]) break;
1298         }
1299         classes[0][p] = i;
1300     }
1301
1302     for (pass = 0; pass < 8; pass++) {
1303         p = 0;
1304         while (p < partitions) {
1305             if (pass == 0) for (j = 0; j < channels; j++) {
1306                 codebook_t * book = &venc->codebooks[rc->classbook];
1307                 int entry = 0;
1308                 for (i = 0; i < classwords; i++) {
1309                     entry *= rc->classifications;
1310                     entry += classes[j][p + i];
1311                 }
1312                 put_codeword(pb, book, entry);
1313             }
1314             for (i = 0; i < classwords && p < partitions; i++, p++) {
1315                 for (j = 0; j < channels; j++) {
1316                     int nbook = rc->books[classes[j][p]][pass];
1317                     codebook_t * book = &venc->codebooks[nbook];
1318                     float * buf = coeffs + samples*j + rc->begin + p*psize;
1319                     if (nbook == -1) continue;
1320
1321                     assert(rc->type == 0 || rc->type == 2);
1322                     assert(!(psize % book->ndimentions));
1323
1324                     if (rc->type == 0) {
1325                         for (k = 0; k < psize; k += book->ndimentions) {
1326                             float * a = put_vector(book, pb, &buf[k]);
1327                             int l;
1328                             for (l = 0; l < book->ndimentions; l++) buf[k + l] -= a[l];
1329                         }
1330                     } else {
1331                         for (k = 0; k < psize; k += book->ndimentions) {
1332                             int dim = book->ndimentions, s = rc->begin + p * psize + k, l;
1333                             float vec[dim], * a = vec;
1334                             for (l = s; l < s + dim; l++)
1335                                 *a++ = coeffs[(l % real_ch) * samples + l / real_ch];
1336                             a = put_vector(book, pb, vec);
1337                             for (l = s; l < s + dim; l++)
1338                                 coeffs[(l % real_ch) * samples + l / real_ch] -= *a++;
1339                         }
1340                     }
1341                 }
1342             }
1343         }
1344     }
1345 }
1346
1347 static int window(venc_context_t * venc, signed short * audio, int samples) {
1348     int i, j, channel;
1349     const float * win = venc->win[0];
1350     int window_len = 1 << (venc->blocksize[0] - 1);
1351     float n = (float)(1 << venc->blocksize[0]) / 4.;
1352     // FIXME use dsp
1353
1354     if (!venc->have_saved && !samples) return 0;
1355
1356     if (venc->have_saved) {
1357         for (channel = 0; channel < venc->channels; channel++) {
1358             memcpy(venc->samples + channel*window_len*2, venc->saved + channel*window_len, sizeof(float)*window_len);
1359         }
1360     } else {
1361         for (channel = 0; channel < venc->channels; channel++) {
1362             memset(venc->samples + channel*window_len*2, 0, sizeof(float)*window_len);
1363         }
1364     }
1365
1366     if (samples) {
1367         for (channel = 0; channel < venc->channels; channel++) {
1368             float * offset = venc->samples + channel*window_len*2 + window_len;
1369             j = channel;
1370             for (i = 0; i < samples; i++, j += venc->channels)
1371                 offset[i] = audio[j] / 32768. / n * win[window_len - i - 1];
1372         }
1373     } else {
1374         for (channel = 0; channel < venc->channels; channel++) {
1375             memset(venc->samples + channel*window_len*2 + window_len, 0, sizeof(float)*window_len);
1376         }
1377     }
1378
1379     for (channel = 0; channel < venc->channels; channel++) {
1380         ff_mdct_calc(&venc->mdct[0], venc->coeffs + channel*window_len, venc->samples + channel*window_len*2, venc->floor/*tmp*/);
1381     }
1382
1383     if (samples) {
1384         for (channel = 0; channel < venc->channels; channel++) {
1385             float * offset = venc->saved + channel*window_len;
1386             j = channel;
1387             for (i = 0; i < samples; i++, j += venc->channels)
1388                 offset[i] = audio[j] / 32768. / n * win[i];
1389         }
1390         venc->have_saved = 1;
1391     } else {
1392         venc->have_saved = 0;
1393     }
1394     return 1;
1395 }
1396
1397 static int vorbis_encode_init(AVCodecContext * avccontext)
1398 {
1399     venc_context_t * venc = avccontext->priv_data;
1400
1401     if (avccontext->channels != 2) return -1;
1402
1403     create_vorbis_context(venc, avccontext);
1404
1405     if (avccontext->flags & CODEC_FLAG_QSCALE) venc->quality = avccontext->global_quality / (float)FF_QP2LAMBDA / 100.;
1406     else venc->quality = 0.17;
1407
1408     avccontext->extradata_size = put_main_header(venc, (uint8_t**)&avccontext->extradata);
1409
1410     avccontext->frame_size = 1 << (venc->blocksize[0] - 1);
1411
1412     avccontext->coded_frame = avcodec_alloc_frame();
1413     avccontext->coded_frame->key_frame = 1;
1414
1415     return 0;
1416 }
1417
1418 static int vorbis_encode_frame(AVCodecContext * avccontext, unsigned char * packets, int buf_size, void *data)
1419 {
1420     venc_context_t * venc = avccontext->priv_data;
1421     signed short * audio = data;
1422     int samples = data ? avccontext->frame_size : 0;
1423     vorbis_mode_t * mode;
1424     mapping_t * mapping;
1425     PutBitContext pb;
1426     int i;
1427
1428     if (!window(venc, audio, samples)) return 0;
1429     samples = 1 << (venc->blocksize[0] - 1);
1430
1431     init_put_bits(&pb, packets, buf_size);
1432
1433     put_bits(&pb, 1, 0); // magic bit
1434
1435     put_bits(&pb, ilog(venc->nmodes - 1), 0); // 0 bits, the mode
1436
1437     mode = &venc->modes[0];
1438     mapping = &venc->mappings[mode->mapping];
1439     if (mode->blockflag) {
1440         put_bits(&pb, 1, 0);
1441         put_bits(&pb, 1, 0);
1442     }
1443
1444     for (i = 0; i < venc->channels; i++) {
1445         floor_t * fc = &venc->floors[mapping->floor[mapping->mux[i]]];
1446         int posts[fc->values];
1447         floor_fit(venc, fc, &venc->coeffs[i * samples], posts, samples);
1448         floor_encode(venc, fc, &pb, posts, &venc->floor[i * samples], samples);
1449     }
1450
1451     for (i = 0; i < venc->channels; i++) {
1452         int j;
1453         for (j = 0; j < samples; j++) {
1454             venc->coeffs[i * samples + j] /= venc->floor[i * samples + j];
1455         }
1456     }
1457
1458     for (i = 0; i < mapping->coupling_steps; i++) {
1459         float * mag = venc->coeffs + mapping->magnitude[i] * samples;
1460         float * ang = venc->coeffs + mapping->angle[i] * samples;
1461         int j;
1462         for (j = 0; j < samples; j++) {
1463             float m = mag[j];
1464             float a = ang[j];
1465             if (m > 0) {
1466                 ang[j] = m - a;
1467                 if (a > m) mag[j] = a;
1468                 else mag[j] = m;
1469             } else {
1470                 ang[j] = a - m;
1471                 if (a > m) mag[j] = m;
1472                 else mag[j] = a;
1473             }
1474         }
1475     }
1476
1477     residue_encode(venc, &venc->residues[mapping->residue[mapping->mux[0]]], &pb, venc->coeffs, samples, venc->channels);
1478
1479     flush_put_bits(&pb);
1480     return (put_bits_count(&pb) + 7) / 8;
1481 }
1482
1483
1484 static int vorbis_encode_close(AVCodecContext * avccontext)
1485 {
1486     venc_context_t * venc = avccontext->priv_data;
1487     int i;
1488
1489     if (venc->codebooks) for (i = 0; i < venc->ncodebooks; i++) {
1490         av_freep(&venc->codebooks[i].lens);
1491         av_freep(&venc->codebooks[i].codewords);
1492         av_freep(&venc->codebooks[i].quantlist);
1493         av_freep(&venc->codebooks[i].dimentions);
1494     }
1495     av_freep(&venc->codebooks);
1496
1497     if (venc->floors) for (i = 0; i < venc->nfloors; i++) {
1498         int j;
1499         av_freep(&venc->floors[i].classes);
1500         if (venc->floors[i].classes)
1501             for (j = 0; j < venc->floors[i].nclasses; j++)
1502                 av_freep(&venc->floors[i].classes[j].books);
1503         av_freep(&venc->floors[i].partition_to_class);
1504         av_freep(&venc->floors[i].list);
1505     }
1506     av_freep(&venc->floors);
1507
1508     if (venc->residues) for (i = 0; i < venc->nresidues; i++) {
1509         av_freep(&venc->residues[i].books);
1510         av_freep(&venc->residues[i].maxes);
1511     }
1512     av_freep(&venc->residues);
1513
1514     if (venc->mappings) for (i = 0; i < venc->nmappings; i++) {
1515         av_freep(&venc->mappings[i].mux);
1516         av_freep(&venc->mappings[i].floor);
1517         av_freep(&venc->mappings[i].residue);
1518     }
1519     av_freep(&venc->mappings);
1520
1521     av_freep(&venc->modes);
1522
1523     av_freep(&venc->saved);
1524     av_freep(&venc->samples);
1525     av_freep(&venc->floor);
1526     av_freep(&venc->coeffs);
1527
1528     ff_mdct_end(&venc->mdct[0]);
1529     ff_mdct_end(&venc->mdct[1]);
1530
1531     av_freep(&avccontext->coded_frame);
1532     av_freep(&avccontext->extradata);
1533
1534     return 0 ;
1535 }
1536
1537 AVCodec vorbis_encoder = {
1538     "vorbis",
1539     CODEC_TYPE_AUDIO,
1540     CODEC_ID_VORBIS,
1541     sizeof(venc_context_t),
1542     vorbis_encode_init,
1543     vorbis_encode_frame,
1544     vorbis_encode_close,
1545     .capabilities= CODEC_CAP_DELAY,
1546 };