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