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