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