]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavcodec/vorbis_enc.c
Original Commit: r96 | ods15 | 2006-09-30 23:25:18 +0300 (Sat, 30 Sep 2006) | 2 lines
[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 //#define ALT_BITSTREAM_WRITER
33 //#include "bitstream.h"
34
35 typedef struct {
36     int len;
37     uint32_t codeword;
38 } cb_entry_t;
39
40 typedef struct {
41     int nentries;
42     cb_entry_t * entries;
43     int ndimentions;
44     float min;
45     float delta;
46     int seq_p;
47     int lookup;
48     int * quantlist;
49     float * dimentions;
50 } codebook_t;
51
52 typedef struct {
53     int dim;
54     int subclass;
55     int masterbook;
56     int * books;
57 } floor_class_t;
58
59 typedef struct {
60     int x;
61     int low;
62     int high;
63     int sort;
64 } floor_entry_t;
65
66 typedef struct {
67     int partitions;
68     int * partition_to_class;
69     int nclasses;
70     floor_class_t * classes;
71     int multiplier;
72     int rangebits;
73     int values;
74     floor_entry_t * list;
75 } floor_t;
76
77 typedef struct {
78     int type;
79     int begin;
80     int end;
81     int partition_size;
82     int classifications;
83     int classbook;
84     int (*books)[8];
85     float (*maxes)[2];
86 } residue_t;
87
88 typedef struct {
89     int submaps;
90     int * mux;
91     int * floor;
92     int * residue;
93     int coupling_steps;
94     int * magnitude;
95     int * angle;
96 } mapping_t;
97
98 typedef struct {
99     int blockflag;
100     int mapping;
101 } vorbis_mode_t;
102
103 typedef struct {
104     int channels;
105     int sample_rate;
106     int blocksize[2]; // in (1<<n) format
107     MDCTContext mdct[2];
108     const float * win[2];
109     int have_saved;
110     float * saved;
111     float * samples;
112     float * floor; // also used for tmp values for mdct
113     float * coeffs; // also used for residue after floor
114     float quality;
115
116     int ncodebooks;
117     codebook_t * codebooks;
118
119     int nfloors;
120     floor_t * floors;
121
122     int nresidues;
123     residue_t * residues;
124
125     int nmappings;
126     mapping_t * mappings;
127
128     int nmodes;
129     vorbis_mode_t * modes;
130 } venc_context_t;
131
132 typedef struct {
133     int total;
134     int total_pos;
135     int pos;
136     uint8_t * buf_ptr;
137 } PutBitContext;
138
139 #define ilog(i) av_log2(2*(i))
140
141 static inline void init_put_bits(PutBitContext * pb, uint8_t * buf, int buffer_len) {
142     pb->total = buffer_len * 8;
143     pb->total_pos = 0;
144     pb->pos = 0;
145     pb->buf_ptr = buf;
146 }
147
148 static void put_bits(PutBitContext * pb, int bits, uint64_t val) {
149     if ((pb->total_pos += bits) >= pb->total) return;
150     if (!bits) return;
151     if (pb->pos) {
152         if (pb->pos > bits) {
153             *pb->buf_ptr |= val << (8 - pb->pos);
154             pb->pos -= bits;
155             bits = 0;
156         } else {
157             *pb->buf_ptr++ |= (val << (8 - pb->pos)) & 0xFF;
158             val >>= pb->pos;
159             bits -= pb->pos;
160             pb->pos = 0;
161         }
162     }
163     for (; bits >= 8; bits -= 8) {
164         *pb->buf_ptr++ = val & 0xFF;
165         val >>= 8;
166     }
167     if (bits) {
168         *pb->buf_ptr = val;
169         pb->pos = 8 - bits;
170     }
171 }
172
173 static inline void flush_put_bits(PutBitContext * pb) {
174 }
175
176 static inline int put_bits_count(PutBitContext * pb) {
177     return pb->total_pos;
178 }
179
180 static int cb_lookup_vals(int lookup, int dimentions, int entries) {
181     if (lookup == 1) {
182         int tmp, i;
183         for (tmp = 0; ; tmp++) {
184                 int n = 1;
185                 for (i = 0; i < dimentions; i++) n *= tmp;
186                 if (n > entries) break;
187         }
188         return tmp - 1;
189     } else if (lookup == 2) return dimentions * entries;
190     return 0;
191 }
192
193 static void ready_codebook(codebook_t * cb) {
194     int h[33] = { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
195     int i;
196
197     for (i = 0; i < cb->nentries; i++) {
198         cb_entry_t * e = &cb->entries[i];
199         int j = 0;
200         if (!e->len) continue;
201         if (h[0]) h[0] = 0;
202         else {
203             for (j = e->len; j; j--)
204                 if (h[j]) break;
205             assert(j);
206         }
207         e->codeword = h[j];
208         h[j] = 0;
209         for (j++; j <= e->len; j++) h[j] = e->codeword | (1 << (j - 1));
210     }
211     for (i = 0; i < 33; i++) assert(!h[i]);
212
213     if (!cb->lookup) cb->dimentions = NULL;
214     else {
215         int vals = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries);
216         cb->dimentions = av_malloc(sizeof(float) * cb->nentries * cb->ndimentions);
217         for (i = 0; i < cb->nentries; i++) {
218             float last = 0;
219             int j;
220             int div = 1;
221             for (j = 0; j < cb->ndimentions; j++) {
222                 int off;
223                 if (cb->lookup == 1) off = (i / div) % vals; // lookup type 1
224                 else off = i * cb->ndimentions + j; // lookup type 2
225
226                 cb->dimentions[i * cb->ndimentions + j] = last + cb->min + cb->quantlist[off] * cb->delta;
227                 if (cb->seq_p) last = cb->dimentions[i * cb->ndimentions + j];
228                 div *= vals;
229             }
230         }
231     }
232
233 }
234
235 static void ready_floor(floor_t * fc) {
236     int i;
237     fc->list[0].sort = 0;
238     fc->list[1].sort = 1;
239     for (i = 2; i < fc->values; i++) {
240         int j;
241         fc->list[i].low = 0;
242         fc->list[i].high = 1;
243         fc->list[i].sort = i;
244         for (j = 2; j < i; j++) {
245             int tmp = fc->list[j].x;
246             if (tmp < fc->list[i].x) {
247                 if (tmp > fc->list[fc->list[i].low].x) fc->list[i].low = j;
248             } else {
249                 if (tmp < fc->list[fc->list[i].high].x) fc->list[i].high = j;
250             }
251         }
252     }
253     for (i = 0; i < fc->values - 1; i++) {
254         int j;
255         for (j = i + 1; j < fc->values; j++) {
256             if (fc->list[fc->list[i].sort].x > fc->list[fc->list[j].sort].x) {
257                 int tmp = fc->list[i].sort;
258                 fc->list[i].sort = fc->list[j].sort;
259                 fc->list[j].sort = tmp;
260             }
261         }
262     }
263 }
264
265 static void ready_residue(residue_t * rc, venc_context_t * venc) {
266     int i;
267     assert(rc->type == 2);
268     rc->maxes = av_mallocz(sizeof(float[2]) * rc->classifications);
269     for (i = 0; i < rc->classifications; i++) {
270         int j;
271         codebook_t * cb;
272         for (j = 0; j < 8; j++) if (rc->books[i][j] != -1) break;
273         if (j == 8) continue; // zero
274         cb = &venc->codebooks[rc->books[i][j]];
275         assert(cb->ndimentions >= 2);
276         assert(cb->lookup);
277
278         for (j = 0; j < cb->nentries; j++) {
279             float a;
280             if (!cb->entries[j].len) continue;
281             a = fabs(cb->dimentions[j * cb->ndimentions]);
282             if (a > rc->maxes[i][0]) rc->maxes[i][0] = a;
283             a = fabs(cb->dimentions[j * cb->ndimentions + 1]);
284             if (a > rc->maxes[i][1]) rc->maxes[i][1] = a;
285         }
286     }
287     // small bias
288     for (i = 0; i < rc->classifications; i++) {
289         rc->maxes[i][0] += 0.8;
290         rc->maxes[i][1] += 0.8;
291     }
292 }
293
294 static void create_vorbis_context(venc_context_t * venc, AVCodecContext * avccontext) {
295     codebook_t * cb;
296     floor_t * fc;
297     residue_t * rc;
298     mapping_t * mc;
299     int i, book;
300
301     int codebook0[] = { 2, 10, 8, 14, 7, 12, 11, 14, 1, 5, 3, 7, 4, 9, 7, 13, };
302     int codebook1[] = { 1, 4, 2, 6, 3, 7, 5, 7, };
303     int codebook2[] = { 1, 5, 7, 21, 5, 8, 9, 21, 10, 9, 12, 20, 20, 16, 20, 20, 4, 8, 9, 20, 6, 8, 9, 20, 11, 11, 13, 20, 20, 15, 17, 20, 9, 11, 14, 20, 8, 10, 15, 20, 11, 13, 15, 20, 20, 20, 20, 20, 20, 20, 20, 20, 13, 20, 20, 20, 18, 18, 20, 20, 20, 20, 20, 20, 3, 6, 8, 20, 6, 7, 9, 20, 10, 9, 12, 20, 20, 20, 20, 20, 5, 7, 9, 20, 6, 6, 9, 20, 10, 9, 12, 20, 20, 20, 20, 20, 8, 10, 13, 20, 8, 9, 12, 20, 11, 10, 12, 20, 20, 20, 20, 20, 18, 20, 20, 20, 15, 17, 18, 20, 18, 17, 18, 20, 20, 20, 20, 20, 7, 10, 12, 20, 8, 9, 11, 20, 14, 13, 14, 20, 20, 20, 20, 20, 6, 9, 12, 20, 7, 8, 11, 20, 12, 11, 13, 20, 20, 20, 20, 20, 9, 11, 15, 20, 8, 10, 14, 20, 12, 11, 14, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 11, 16, 18, 20, 15, 15, 17, 20, 20, 17, 20, 20, 20, 20, 20, 20, 9, 14, 16, 20, 12, 12, 15, 20, 17, 15, 18, 20, 20, 20, 20, 20, 16, 19, 18, 20, 15, 16, 20, 20, 17, 17, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, };
304     int codebook3[] = { 2, 3, 7, 13, 4, 4, 7, 15, 8, 6, 9, 17, 21, 16, 15, 21, 2, 5, 7, 11, 5, 5, 7, 14, 9, 7, 10, 16, 17, 15, 16, 21, 4, 7, 10, 17, 7, 7, 9, 15, 11, 9, 11, 16, 21, 18, 15, 21, 18, 21, 21, 21, 15, 17, 17, 19, 21, 19, 18, 20, 21, 21, 21, 20, };
305     int codebook4[] = { 5, 5, 5, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 5, 7, 5, 7, 5, 7, 5, 7, 5, 8, 6, 8, 6, 8, 6, 9, 6, 9, 6, 10, 6, 10, 6, 11, 6, 11, 7, 11, 7, 12, 7, 12, 7, 12, 7, 12, 7, 12, 7, 12, 7, 12, 7, 12, 8, 13, 8, 12, 8, 12, 8, 13, 8, 13, 9, 13, 9, 13, 9, 13, 9, 12, 10, 12, 10, 13, 10, 14, 11, 14, 12, 14, 13, 14, 13, 14, 14, 15, 16, 15, 15, 15, 14, 15, 17, 21, 22, 22, 21, 22, 22, 22, 22, 22, 22, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, };
306     int codebook5[] = { 2, 5, 5, 4, 5, 4, 5, 4, 5, 4, 6, 5, 6, 5, 6, 5, 6, 5, 7, 5, 7, 6, 8, 6, 8, 6, 8, 6, 9, 6, 9, 6, };
307     int codebook6[] = { 8, 5, 8, 4, 9, 4, 9, 4, 9, 4, 9, 4, 9, 4, 9, 4, 9, 4, 9, 4, 9, 4, 8, 4, 8, 4, 9, 5, 9, 5, 9, 5, 9, 5, 9, 6, 10, 6, 10, 7, 10, 8, 11, 9, 11, 11, 12, 13, 12, 14, 13, 15, 13, 15, 14, 16, 14, 17, 15, 17, 15, 15, 16, 16, 15, 16, 16, 16, 15, 18, 16, 15, 17, 17, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, };
308     int codebook7[] = { 1, 5, 5, 5, 5, 5, 5, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 6, 7, 7, 7, 7, 8, 7, 8, 8, 9, 8, 10, 9, 10, 9, };
309     int codebook8[] = { 4, 3, 4, 3, 4, 4, 5, 4, 5, 4, 5, 5, 6, 5, 6, 5, 7, 5, 7, 6, 7, 6, 8, 7, 8, 7, 8, 7, 9, 8, 9, 9, 9, 9, 10, 10, 10, 11, 9, 12, 9, 12, 9, 15, 10, 14, 9, 13, 10, 13, 10, 12, 10, 12, 10, 13, 10, 12, 11, 13, 11, 14, 12, 13, 13, 14, 14, 13, 14, 15, 14, 16, 13, 13, 14, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 15, 15, };
310     int codebook9[] = { 4, 5, 4, 5, 3, 5, 3, 5, 3, 5, 4, 4, 4, 4, 5, 5, 5, };
311     int codebook10[] = { 3, 3, 4, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 5, 7, 5, 8, 6, 8, 6, 9, 7, 10, 7, 10, 8, 10, 8, 11, 9, 11, };
312     int codebook11[] = { 3, 7, 3, 8, 3, 10, 3, 8, 3, 9, 3, 8, 4, 9, 4, 9, 5, 9, 6, 10, 6, 9, 7, 11, 7, 12, 9, 13, 10, 13, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, };
313     int codebook12[] = { 4, 5, 4, 5, 4, 5, 4, 5, 3, 5, 3, 5, 3, 5, 4, 5, 4, };
314     int codebook13[] = { 4, 2, 4, 2, 5, 3, 5, 4, 6, 6, 6, 7, 7, 8, 7, 8, 7, 8, 7, 9, 8, 9, 8, 9, 8, 10, 8, 11, 9, 12, 9, 12, };
315     int codebook14[] = { 2, 5, 2, 6, 3, 6, 4, 7, 4, 7, 5, 9, 5, 11, 6, 11, 6, 11, 7, 11, 6, 11, 6, 11, 9, 11, 8, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 10, 10, 10, 10, 10, 10, };
316     int codebook15[]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 5, 5, 7, 7, 7, 8, 9, 9, 0, 5, 5, 7, 7, 8, 8, 9, 9, 0, 7, 7, 8, 8, 8, 8, 10, 10, 0, 0, 0, 8, 8, 8, 8, 10, 10, 0, 0, 0, 9, 9, 9, 9, 10, 10, 0, 0, 0, 9, 9, 9, 9, 10, 10, 0, 0, 0, 10, 10, 10, 10, 11, 11, 0, 0, 0, 0, 0, 10, 10, 11, 11, };
322     int codebook21[] = { 2, 3, 3, 6, 6, 7, 7, 8, 8, 8, 8, 9, 9, 10, 10, 11, 10, 0, 5, 5, 7, 7, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 0, 5, 5, 7, 7, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 0, 6, 6, 7, 7, 8, 8, 9, 9, 9, 9, 10, 10, 11, 11, 11, 11, 0, 0, 0, 7, 7, 8, 8, 9, 9, 9, 9, 10, 10, 11, 11, 11, 12, 0, 0, 0, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 11, 11, 12, 12, 0, 0, 0, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 11, 11, 12, 12, 0, 0, 0, 9, 9, 9, 9, 10, 10, 10, 10, 11, 10, 11, 11, 12, 12, 0, 0, 0, 0, 0, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11, 12, 12, 0, 0, 0, 0, 0, 9, 8, 9, 9, 10, 10, 11, 11, 12, 12, 12, 12, 0, 0, 0, 0, 0, 8, 8, 9, 9, 10, 10, 11, 11, 12, 11, 12, 12, 0, 0, 0, 0, 0, 9, 10, 10, 10, 11, 11, 11, 11, 12, 12, 13, 13, 0, 0, 0, 0, 0, 0, 0, 10, 10, 10, 10, 11, 11, 12, 12, 13, 13, 0, 0, 0, 0, 0, 0, 0, 11, 11, 11, 11, 12, 12, 12, 12, 13, 13, 0, 0, 0, 0, 0, 0, 0, 11, 11, 11, 11, 12, 12, 12, 12, 13, 13, 0, 0, 0, 0, 0, 0, 0, 11, 11, 12, 12, 12, 12, 13, 13, 13, 13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 12, 12, 12, 13, 13, 13, 13, };
323     int codebook22[] = { 1, 4, 4, 7, 6, 6, 7, 6, 6, 4, 7, 7, 10, 9, 9, 11, 9, 9, 4, 7, 7, 10, 9, 9, 11, 9, 9, 7, 10, 10, 11, 11, 10, 12, 11, 11, 6, 9, 9, 11, 10, 10, 11, 10, 10, 6, 9, 9, 11, 10, 10, 11, 10, 10, 7, 11, 11, 11, 11, 11, 12, 11, 11, 6, 9, 9, 11, 10, 10, 11, 10, 10, 6, 9, 9, 11, 10, 10, 11, 10, 10, };
324     int codebook23[] = { 2, 4, 4, 6, 6, 7, 7, 7, 7, 8, 8, 10, 5, 5, 6, 6, 7, 7, 8, 8, 8, 8, 10, 5, 5, 6, 6, 7, 7, 8, 8, 8, 8, 10, 6, 6, 7, 7, 8, 8, 8, 8, 8, 8, 10, 10, 10, 7, 7, 8, 7, 8, 8, 8, 8, 10, 10, 10, 8, 8, 8, 8, 8, 8, 8, 8, 10, 10, 10, 7, 8, 8, 8, 8, 8, 8, 8, 10, 10, 10, 8, 8, 8, 8, 8, 8, 8, 8, 10, 10, 10, 10, 10, 8, 8, 8, 8, 8, 8, 10, 10, 10, 10, 10, 9, 9, 8, 8, 9, 8, 10, 10, 10, 10, 10, 8, 8, 8, 8, 8, 8, };
325     int codebook24[] = { 1, 4, 4, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 6, 5, 5, 7, 7, 8, 8, 8, 8, 9, 9, 10, 10, 7, 5, 5, 7, 7, 8, 8, 8, 8, 9, 9, 11, 10, 0, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 11, 11, 0, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 11, 11, 0, 12, 12, 9, 9, 10, 10, 10, 10, 11, 11, 11, 12, 0, 13, 13, 9, 9, 10, 10, 10, 10, 11, 11, 12, 12, 0, 0, 0, 10, 10, 10, 10, 11, 11, 12, 12, 12, 12, 0, 0, 0, 10, 10, 10, 10, 11, 11, 12, 12, 12, 12, 0, 0, 0, 14, 14, 11, 11, 11, 11, 12, 12, 13, 13, 0, 0, 0, 14, 14, 11, 11, 11, 11, 12, 12, 13, 13, 0, 0, 0, 0, 0, 12, 12, 12, 12, 13, 13, 14, 13, 0, 0, 0, 0, 0, 13, 13, 12, 12, 13, 12, 14, 13, };
326     int codebook25[] = { 2, 4, 4, 5, 5, 6, 5, 5, 5, 5, 6, 4, 5, 5, 5, 6, 5, 5, 5, 5, 6, 6, 6, 5, 5, };
327     int codebook26[] = { 1, 4, 4, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 4, 9, 8, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 2, 9, 7, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, };
328     int codebook27[] = { 1, 4, 4, 6, 6, 7, 7, 8, 7, 9, 9, 10, 10, 10, 10, 6, 5, 5, 7, 7, 8, 8, 10, 8, 11, 10, 12, 12, 13, 13, 6, 5, 5, 7, 7, 8, 8, 10, 9, 11, 11, 12, 12, 13, 12, 18, 8, 8, 8, 8, 9, 9, 10, 9, 11, 10, 12, 12, 13, 13, 18, 8, 8, 8, 8, 9, 9, 10, 10, 11, 11, 13, 12, 14, 13, 18, 11, 11, 9, 9, 10, 10, 11, 11, 11, 12, 13, 12, 13, 14, 18, 11, 11, 9, 8, 11, 10, 11, 11, 11, 11, 12, 12, 14, 13, 18, 18, 18, 10, 11, 10, 11, 12, 12, 12, 12, 13, 12, 14, 13, 18, 18, 18, 10, 11, 11, 9, 12, 11, 12, 12, 12, 13, 13, 13, 18, 18, 17, 14, 14, 11, 11, 12, 12, 13, 12, 14, 12, 14, 13, 18, 18, 18, 14, 14, 11, 10, 12, 9, 12, 13, 13, 13, 13, 13, 18, 18, 17, 16, 18, 13, 13, 12, 12, 13, 11, 14, 12, 14, 14, 17, 18, 18, 17, 18, 13, 12, 13, 10, 12, 11, 14, 14, 14, 14, 17, 18, 18, 18, 18, 15, 16, 12, 12, 13, 10, 14, 12, 14, 15, 18, 18, 18, 16, 17, 16, 14, 12, 11, 13, 10, 13, 13, 14, 15, };
329     int codebook28[] = { 2, 5, 5, 6, 6, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 10, 6, 6, 7, 7, 8, 7, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 10, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 10, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 7, 7, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 11, 11, 11, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 9, 10, 10, 10, 11, 11, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 11, 10, 11, 11, 11, 9, 9, 9, 9, 9, 9, 10, 10, 9, 9, 10, 9, 11, 10, 11, 11, 11, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 9, 11, 11, 11, 11, 11, 9, 9, 9, 9, 10, 10, 9, 9, 9, 9, 10, 9, 11, 11, 11, 11, 11, 11, 11, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 10, 9, 10, 10, 9, 10, 9, 9, 10, 9, 11, 10, 10, 11, 11, 11, 11, 9, 10, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 10, 10, 10, 9, 9, 10, 9, 10, 9, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 9, 9, 9, 9, 9, 10, 10, 10, };
330
331     int codebook_sizes[] = { 16, 8, 256, 64, 128, 32, 96, 32, 96, 17, 32, 78, 17, 32, 78, 100, 1641, 443, 105, 68, 81, 289, 81, 121, 169, 25, 169, 225, 289, };
332     int * codebook_lens[] = { codebook0,  codebook1,  codebook2,  codebook3,  codebook4,  codebook5,  codebook6,  codebook7,
333                               codebook8,  codebook9,  codebook10, codebook11, codebook12, codebook13, codebook14, codebook15,
334                               codebook16, codebook17, codebook18, codebook19, codebook20, codebook21, codebook22, codebook23,
335                               codebook24, codebook25, codebook26, codebook27, codebook28, };
336
337     struct {
338         int lookup;
339         int dim;
340         float min;
341         float delta;
342         int real_len;
343         int * quant;
344     } cvectors[] = {
345         { 1, 8,    -1.0,   1.0, 6561,(int[]){ 1, 0, 2, } },
346         { 1, 4,    -2.0,   1.0, 625, (int[]){ 2, 1, 3, 0, 4, } },
347         { 1, 4,    -2.0,   1.0, 625, (int[]){ 2, 1, 3, 0, 4, } },
348         { 1, 2,    -4.0,   1.0, 81,  (int[]){ 4, 3, 5, 2, 6, 1, 7, 0, 8, } },
349         { 1, 2,    -4.0,   1.0, 81,  (int[]){ 4, 3, 5, 2, 6, 1, 7, 0, 8, } },
350         { 1, 2,    -8.0,   1.0, 289, (int[]){ 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15, 0, 16, } },
351         { 1, 4,   -11.0,  11.0, 81,  (int[]){ 1, 0, 2, } },
352         { 1, 2,    -5.0,   1.0, 121, (int[]){ 5, 4, 6, 3, 7, 2, 8, 1, 9, 0, 10, } },
353         { 1, 2,   -30.0,   5.0, 169, (int[]){ 6, 5, 7, 4, 8, 3, 9, 2, 10, 1, 11, 0, 12, } },
354         { 1, 2,    -2.0,   1.0, 25,  (int[]){ 2, 1, 3, 0, 4, } },
355         { 1, 2, -1530.0, 255.0, 169, (int[]){ 6, 5, 7, 4, 8, 3, 9, 2, 10, 1, 11, 0, 12, } },
356         { 1, 2,  -119.0,  17.0, 225, (int[]){ 7, 6, 8, 5, 9, 4, 10, 3, 11, 2, 12, 1, 13, 0, 14, } },
357         { 1, 2,    -8.0,   1.0, 289, (int[]){ 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15, 0, 16, } },
358     };
359
360     venc->channels = avccontext->channels;
361     venc->sample_rate = avccontext->sample_rate;
362     venc->blocksize[0] = venc->blocksize[1] = 11;
363
364     venc->ncodebooks = 29;
365     venc->codebooks = av_malloc(sizeof(codebook_t) * venc->ncodebooks);
366
367     // codebook 0..14 - floor1 book, values 0..255
368     // codebook 15 residue masterbook
369     // codebook 16..29 residue
370     for (book = 0; book < venc->ncodebooks; book++) {
371         cb = &venc->codebooks[book];
372         cb->nentries = codebook_sizes[book];
373         if (book < 16) {
374             cb->ndimentions = 2;
375             cb->min = 0.;
376             cb->delta = 0.;
377             cb->seq_p = 0;
378             cb->lookup = 0;
379             cb->quantlist = NULL;
380         } else {
381             int vals;
382             cb->seq_p = 0;
383             cb->nentries = cvectors[book - 16].real_len;
384             cb->ndimentions = cvectors[book - 16].dim;
385             cb->min = cvectors[book - 16].min;
386             cb->delta = cvectors[book - 16].delta;
387             cb->lookup = cvectors[book - 16].lookup;
388             vals = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries);
389             cb->quantlist = av_malloc(sizeof(int) * vals);
390             for (i = 0; i < vals; i++) cb->quantlist[i] = cvectors[book - 16].quant[i];
391         }
392         cb->entries = av_malloc(sizeof(cb_entry_t) * cb->nentries);
393         for (i = 0; i < cb->nentries; i++) {
394             if (i < codebook_sizes[book]) cb->entries[i].len = codebook_lens[book][i];
395             else cb->entries[i].len = 0;
396         }
397         ready_codebook(cb);
398     }
399
400     venc->nfloors = 1;
401     venc->floors = av_malloc(sizeof(floor_t) * venc->nfloors);
402
403     // just 1 floor
404     fc = &venc->floors[0];
405     fc->partitions = 8;
406     fc->partition_to_class = av_malloc(sizeof(int) * fc->partitions);
407     fc->nclasses = 0;
408     for (i = 0; i < fc->partitions; i++) {
409         int a[] = {0,1,2,2,3,3,4,4};
410         fc->partition_to_class[i] = a[i];
411         fc->nclasses = FFMAX(fc->nclasses, fc->partition_to_class[i]);
412     }
413     fc->nclasses++;
414     fc->classes = av_malloc(sizeof(floor_class_t) * fc->nclasses);
415     for (i = 0; i < fc->nclasses; i++) {
416         floor_class_t * c = &fc->classes[i];
417         int j, books;
418         int dim[] = {3,4,3,4,3};
419         int subclass[] = {0,1,1,2,2};
420         int masterbook[] = {0/*none*/,0,1,2,3};
421         int * nbooks[] = {
422             (int[]){ 4 },
423             (int[]){ 5, 6 },
424             (int[]){ 7, 8 },
425             (int[]){ -1, 9, 10, 11 },
426             (int[]){ -1, 12, 13, 14 },
427         };
428         c->dim = dim[i];
429         c->subclass = subclass[i];
430         c->masterbook = masterbook[i];
431         books = (1 << c->subclass);
432         c->books = av_malloc(sizeof(int) * books);
433         for (j = 0; j < books; j++) c->books[j] = nbooks[i][j];
434     }
435     fc->multiplier = 2;
436     fc->rangebits = venc->blocksize[0] - 1;
437
438     fc->values = 2;
439     for (i = 0; i < fc->partitions; i++)
440         fc->values += fc->classes[fc->partition_to_class[i]].dim;
441
442     fc->list = av_malloc(sizeof(floor_entry_t) * fc->values);
443     fc->list[0].x = 0;
444     fc->list[1].x = 1 << fc->rangebits;
445     for (i = 2; i < fc->values; i++) {
446         /*int a = i - 1;
447         int g = ilog(a);
448         assert(g <= fc->rangebits);
449         a ^= 1 << (g-1);
450         g = 1 << (fc->rangebits - g);
451         fc->list[i].x = g + a*2*g;*/
452         //int a[] = {14, 4, 58, 2, 8, 28, 90};
453         int a[] = {93,23,372,6,46,186,750,14,33,65,130,260,556,3,10,18,28,39,55,79,111,158,220,312,464,650,850};
454         fc->list[i].x = a[i - 2];
455     }
456     ready_floor(fc);
457
458     venc->nresidues = 1;
459     venc->residues = av_malloc(sizeof(residue_t) * venc->nresidues);
460
461     // single residue
462     rc = &venc->residues[0];
463     rc->type = 2;
464     rc->begin = 0;
465     rc->end = 1600;
466     rc->partition_size = 32;
467     rc->classifications = 10;
468     rc->classbook = 15;
469     rc->books = av_malloc(sizeof(int[8]) * rc->classifications);
470     for (i = 0; i < rc->classifications; i++) {
471         int a[10][8] = {
472             { -1, -1, -1, -1, -1, -1, -1, -1, },
473             { -1, -1, 16, -1, -1, -1, -1, -1, },
474             { -1, -1, 17, -1, -1, -1, -1, -1, },
475             { -1, -1, 18, -1, -1, -1, -1, -1, },
476             { -1, -1, 19, -1, -1, -1, -1, -1, },
477             { -1, -1, 20, -1, -1, -1, -1, -1, },
478             { -1, -1, 21, -1, -1, -1, -1, -1, },
479             { 22, 23, -1, -1, -1, -1, -1, -1, },
480             { 24, 25, -1, -1, -1, -1, -1, -1, },
481             { 26, 27, 28, -1, -1, -1, -1, -1, },
482         };
483         int j;
484         for (j = 0; j < 8; j++) rc->books[i][j] = a[i][j];
485     }
486     ready_residue(rc, venc);
487
488     venc->nmappings = 1;
489     venc->mappings = av_malloc(sizeof(mapping_t) * venc->nmappings);
490
491     // single mapping
492     mc = &venc->mappings[0];
493     mc->submaps = 1;
494     mc->mux = av_malloc(sizeof(int) * venc->channels);
495     for (i = 0; i < venc->channels; i++) mc->mux[i] = 0;
496     mc->floor = av_malloc(sizeof(int) * mc->submaps);
497     mc->residue = av_malloc(sizeof(int) * mc->submaps);
498     for (i = 0; i < mc->submaps; i++) {
499         mc->floor[i] = 0;
500         mc->residue[i] = 0;
501     }
502     mc->coupling_steps = venc->channels == 2 ? 1 : 0;
503     mc->magnitude = av_malloc(sizeof(int) * mc->coupling_steps);
504     mc->angle = av_malloc(sizeof(int) * mc->coupling_steps);
505     if (mc->coupling_steps) {
506         mc->magnitude[0] = 0;
507         mc->angle[0] = 1;
508     }
509
510     venc->nmodes = 1;
511     venc->modes = av_malloc(sizeof(vorbis_mode_t) * venc->nmodes);
512
513     // single mode
514     venc->modes[0].blockflag = 0;
515     venc->modes[0].mapping = 0;
516
517     venc->have_saved = 0;
518     venc->saved = av_malloc(sizeof(float) * venc->channels * (1 << venc->blocksize[1]) / 2);
519     venc->samples = av_malloc(sizeof(float) * venc->channels * (1 << venc->blocksize[1]));
520     venc->floor = av_malloc(sizeof(float) * venc->channels * (1 << venc->blocksize[1]) / 2);
521     venc->coeffs = av_malloc(sizeof(float) * venc->channels * (1 << venc->blocksize[1]) / 2);
522
523     venc->win[0] = ff_vorbis_vwin[venc->blocksize[0] - 6];
524     venc->win[1] = ff_vorbis_vwin[venc->blocksize[1] - 6];
525
526     ff_mdct_init(&venc->mdct[0], venc->blocksize[0], 0);
527     ff_mdct_init(&venc->mdct[1], venc->blocksize[1], 0);
528 }
529
530 static void put_float(PutBitContext * pb, float f) {
531     int exp, mant;
532     uint32_t res = 0;
533     mant = (int)ldexp(frexp(f, &exp), 20);
534     exp += 788 - 20;
535     if (mant < 0) { res |= (1 << 31); mant = -mant; }
536     res |= mant | (exp << 21);
537     put_bits(pb, 32, res);
538 }
539
540 static void put_codebook_header(PutBitContext * pb, codebook_t * cb) {
541     int i;
542     int ordered = 0;
543
544     put_bits(pb, 24, 0x564342); //magic
545     put_bits(pb, 16, cb->ndimentions);
546     put_bits(pb, 24, cb->nentries);
547
548     for (i = 1; i < cb->nentries; i++) if (cb->entries[i].len < cb->entries[i-1].len) break;
549     if (i == cb->nentries) ordered = 1;
550
551     put_bits(pb, 1, ordered);
552     if (ordered) {
553         int len = cb->entries[0].len;
554         put_bits(pb, 5, len - 1);
555         i = 0;
556         while (i < cb->nentries) {
557             int j;
558             for (j = 0; j+i < cb->nentries; j++) if (cb->entries[j+i].len != len) break;
559             put_bits(pb, ilog(cb->nentries - i), j);
560             i += j;
561             len++;
562         }
563     } else {
564         int sparse = 0;
565         for (i = 0; i < cb->nentries; i++) if (!cb->entries[i].len) break;
566         if (i != cb->nentries) sparse = 1;
567         put_bits(pb, 1, sparse);
568
569         for (i = 0; i < cb->nentries; i++) {
570             if (sparse) put_bits(pb, 1, !!cb->entries[i].len);
571             if (cb->entries[i].len) put_bits(pb, 5, cb->entries[i].len - 1);
572         }
573     }
574
575     put_bits(pb, 4, cb->lookup);
576     if (cb->lookup) {
577         int tmp = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries);
578         int bits = ilog(cb->quantlist[0]);
579
580         for (i = 1; i < tmp; i++) bits = FFMAX(bits, ilog(cb->quantlist[i]));
581
582         put_float(pb, cb->min);
583         put_float(pb, cb->delta);
584
585         put_bits(pb, 4, bits - 1);
586         put_bits(pb, 1, cb->seq_p);
587
588         for (i = 0; i < tmp; i++) put_bits(pb, bits, cb->quantlist[i]);
589     }
590 }
591
592 static void put_floor_header(PutBitContext * pb, floor_t * fc) {
593     int i;
594
595     put_bits(pb, 16, 1); // type, only floor1 is supported
596
597     put_bits(pb, 5, fc->partitions);
598
599     for (i = 0; i < fc->partitions; i++) put_bits(pb, 4, fc->partition_to_class[i]);
600
601     for (i = 0; i < fc->nclasses; i++) {
602         int j, books;
603
604         put_bits(pb, 3, fc->classes[i].dim - 1);
605         put_bits(pb, 2, fc->classes[i].subclass);
606
607         if (fc->classes[i].subclass) put_bits(pb, 8, fc->classes[i].masterbook);
608
609         books = (1 << fc->classes[i].subclass);
610
611         for (j = 0; j < books; j++) put_bits(pb, 8, fc->classes[i].books[j] + 1);
612     }
613
614     put_bits(pb, 2, fc->multiplier - 1);
615     put_bits(pb, 4, fc->rangebits);
616
617     for (i = 2; i < fc->values; i++) put_bits(pb, fc->rangebits, fc->list[i].x);
618 }
619
620 static void put_residue_header(PutBitContext * pb, residue_t * rc) {
621     int i;
622
623     put_bits(pb, 16, rc->type);
624
625     put_bits(pb, 24, rc->begin);
626     put_bits(pb, 24, rc->end);
627     put_bits(pb, 24, rc->partition_size - 1);
628     put_bits(pb, 6, rc->classifications - 1);
629     put_bits(pb, 8, rc->classbook);
630
631     for (i = 0; i < rc->classifications; i++) {
632         int j, tmp = 0;
633         for (j = 0; j < 8; j++) tmp |= (rc->books[i][j] != -1) << j;
634
635         put_bits(pb, 3, tmp & 7);
636         put_bits(pb, 1, tmp > 7);
637
638         if (tmp > 7) put_bits(pb, 5, tmp >> 3);
639     }
640
641     for (i = 0; i < rc->classifications; i++) {
642         int j;
643         for (j = 0; j < 8; j++)
644             if (rc->books[i][j] != -1)
645                 put_bits(pb, 8, rc->books[i][j]);
646     }
647 }
648
649 static int put_main_header(venc_context_t * venc, uint8_t ** out) {
650     int i;
651     PutBitContext pb;
652     uint8_t buffer[50000] = {0}, * p = buffer;
653     int buffer_len = sizeof buffer;
654     int len, hlens[3];
655
656     // identification header
657     init_put_bits(&pb, p, buffer_len);
658     put_bits(&pb, 8, 1); //magic
659     for (i = 0; "vorbis"[i]; i++) put_bits(&pb, 8, "vorbis"[i]);
660     put_bits(&pb, 32, 0); // version
661     put_bits(&pb, 8, venc->channels);
662     put_bits(&pb, 32, venc->sample_rate);
663     put_bits(&pb, 32, 0); // bitrate
664     put_bits(&pb, 32, 0); // bitrate
665     put_bits(&pb, 32, 0); // bitrate
666     put_bits(&pb, 4, venc->blocksize[0]);
667     put_bits(&pb, 4, venc->blocksize[1]);
668     put_bits(&pb, 1, 1); // framing
669
670     flush_put_bits(&pb);
671     hlens[0] = (put_bits_count(&pb) + 7) / 8;
672     buffer_len -= hlens[0];
673     p += hlens[0];
674
675     // comment header
676     init_put_bits(&pb, p, buffer_len);
677     put_bits(&pb, 8, 3); //magic
678     for (i = 0; "vorbis"[i]; i++) put_bits(&pb, 8, "vorbis"[i]);
679     put_bits(&pb, 32, 0); // vendor length TODO
680     put_bits(&pb, 32, 0); // amount of comments
681     put_bits(&pb, 1, 1); // framing
682
683     flush_put_bits(&pb);
684     hlens[1] = (put_bits_count(&pb) + 7) / 8;
685     buffer_len -= hlens[1];
686     p += hlens[1];
687
688     // setup header
689     init_put_bits(&pb, p, buffer_len);
690     put_bits(&pb, 8, 5); //magic
691     for (i = 0; "vorbis"[i]; i++) put_bits(&pb, 8, "vorbis"[i]);
692
693     // codebooks
694     put_bits(&pb, 8, venc->ncodebooks - 1);
695     for (i = 0; i < venc->ncodebooks; i++) put_codebook_header(&pb, &venc->codebooks[i]);
696
697     // time domain, reserved, zero
698     put_bits(&pb, 6, 0);
699     put_bits(&pb, 16, 0);
700
701     // floors
702     put_bits(&pb, 6, venc->nfloors - 1);
703     for (i = 0; i < venc->nfloors; i++) put_floor_header(&pb, &venc->floors[i]);
704
705     // residues
706     put_bits(&pb, 6, venc->nresidues - 1);
707     for (i = 0; i < venc->nresidues; i++) put_residue_header(&pb, &venc->residues[i]);
708
709     // mappings
710     put_bits(&pb, 6, venc->nmappings - 1);
711     for (i = 0; i < venc->nmappings; i++) {
712         mapping_t * mc = &venc->mappings[i];
713         int j;
714         put_bits(&pb, 16, 0); // mapping type
715
716         put_bits(&pb, 1, mc->submaps > 1);
717         if (mc->submaps > 1) put_bits(&pb, 4, mc->submaps - 1);
718
719         put_bits(&pb, 1, !!mc->coupling_steps);
720         if (mc->coupling_steps) {
721             put_bits(&pb, 8, mc->coupling_steps - 1);
722             for (j = 0; j < mc->coupling_steps; j++) {
723                 put_bits(&pb, ilog(venc->channels - 1), mc->magnitude[j]);
724                 put_bits(&pb, ilog(venc->channels - 1), mc->angle[j]);
725             }
726         }
727
728         put_bits(&pb, 2, 0); // reserved
729
730         if (mc->submaps > 1) for (j = 0; j < venc->channels; j++) put_bits(&pb, 4, mc->mux[j]);
731
732         for (j = 0; j < mc->submaps; j++) {
733             put_bits(&pb, 8, 0); // reserved time configuration
734             put_bits(&pb, 8, mc->floor[j]);
735             put_bits(&pb, 8, mc->residue[j]);
736         }
737     }
738
739     // modes
740     put_bits(&pb, 6, venc->nmodes - 1);
741     for (i = 0; i < venc->nmodes; i++) {
742         put_bits(&pb, 1, venc->modes[i].blockflag);
743         put_bits(&pb, 16, 0); // reserved window type
744         put_bits(&pb, 16, 0); // reserved transform type
745         put_bits(&pb, 8, venc->modes[i].mapping);
746     }
747
748     put_bits(&pb, 1, 1); // framing
749
750     flush_put_bits(&pb);
751     hlens[2] = (put_bits_count(&pb) + 7) / 8;
752
753     len = hlens[0] + hlens[1] + hlens[2];
754     p = *out = av_mallocz(64 + len + len/255);
755
756     *p++ = 2;
757     p += av_xiphlacing(p, hlens[0]);
758     p += av_xiphlacing(p, hlens[1]);
759     buffer_len = 0;
760     for (i = 0; i < 3; i++) {
761         memcpy(p, buffer + buffer_len, hlens[i]);
762         p += hlens[i];
763         buffer_len += hlens[i];
764     }
765
766     return p - *out;
767 }
768
769 static float get_floor_average(floor_t * fc, float * coeffs, int i) {
770     int begin = fc->list[fc->list[FFMAX(i-1, 0)].sort].x;
771     int end   = fc->list[fc->list[FFMIN(i+1, fc->values - 1)].sort].x;
772     int j;
773     float average = 0;
774
775     for (j = begin; j < end; j++) average += fabs(coeffs[j]);
776     return average / (end - begin);
777 }
778
779 static void floor_fit(venc_context_t * venc, floor_t * fc, float * coeffs, int * posts, int samples) {
780     int range = 255 / fc->multiplier + 1;
781     int i;
782     float tot_average = 0.;
783     for (i = 0; i < fc->values; i++) tot_average += get_floor_average(fc, coeffs, i);
784     tot_average /= fc->values;
785     tot_average /= venc->quality;
786
787     for (i = 0; i < fc->values; i++) {
788         int position = fc->list[fc->list[i].sort].x;
789         float average = get_floor_average(fc, coeffs, i);
790         int j;
791
792         average /= pow(average, 0.5) / tot_average * pow(0.8, position/200.); // MAGIC!
793         for (j = 0; j < range - 1; j++) if (ff_vorbis_floor1_inverse_db_table[j * fc->multiplier] > average) break;
794         posts[fc->list[i].sort] = j;
795     }
796 }
797
798 static int render_point(int x0, int y0, int x1, int y1, int x) {
799     return y0 +  (x - x0) * (y1 - y0) / (x1 - x0);
800 }
801
802 static void render_line(int x0, int y0, int x1, int y1, float * buf, int n) {
803     int dy = y1 - y0;
804     int adx = x1 - x0;
805     int ady = FFMAX(dy, -dy);
806     int base = dy / adx;
807     int x = x0;
808     int y = y0;
809     int err = 0;
810     int sy;
811     if (dy < 0) sy = base - 1;
812     else sy = base + 1;
813     ady = ady - FFMAX(base, -base) * adx;
814     if (x >= n) return;
815     buf[x] = ff_vorbis_floor1_inverse_db_table[y];
816     for (x = x0 + 1; x < x1; x++) {
817         if (x >= n) return;
818         err += ady;
819         if (err >= adx) {
820             err -= adx;
821             y += sy;
822         } else {
823             y += base;
824         }
825         buf[x] = ff_vorbis_floor1_inverse_db_table[y];
826     }
827 }
828
829 static void floor_encode(venc_context_t * venc, floor_t * fc, PutBitContext * pb, int * posts, float * floor, int samples) {
830     int range = 255 / fc->multiplier + 1;
831     int coded[fc->values]; // first 2 values are unused
832     int i, counter;
833     int lx, ly;
834
835     put_bits(pb, 1, 1); // non zero
836     put_bits(pb, ilog(range - 1), posts[0]);
837     put_bits(pb, ilog(range - 1), posts[1]);
838     coded[0] = coded[1] = 1;
839
840     for (i = 2; i < fc->values; i++) {
841         int predicted = render_point(fc->list[fc->list[i].low].x,
842                                      posts[fc->list[i].low],
843                                      fc->list[fc->list[i].high].x,
844                                      posts[fc->list[i].high],
845                                      fc->list[i].x);
846         int highroom = range - predicted;
847         int lowroom = predicted;
848         int room = FFMIN(highroom, lowroom);
849         if (predicted == posts[i]) {
850             coded[i] = 0; // must be used later as flag!
851             continue;
852         } else {
853             if (!coded[fc->list[i].low]) coded[fc->list[i].low] = -1;
854             if (!coded[fc->list[i].high]) coded[fc->list[i].high] = -1;
855         }
856         if (posts[i] > predicted) {
857             if (posts[i] - predicted > room) coded[i] = posts[i] - predicted + lowroom;
858             else coded[i] = (posts[i] - predicted) << 1;
859         } else {
860             if (predicted - posts[i] > room) coded[i] = predicted - posts[i] + highroom - 1;
861             else coded[i] = ((predicted - posts[i]) << 1) - 1;
862         }
863     }
864
865     counter = 2;
866     for (i = 0; i < fc->partitions; i++) {
867         floor_class_t * c = &fc->classes[fc->partition_to_class[i]];
868         int k, cval = 0, csub = 1<<c->subclass;
869         if (c->subclass) {
870             codebook_t * book = &venc->codebooks[c->masterbook];
871             int cshift = 0;
872             for (k = 0; k < c->dim; k++) {
873                 int l;
874                 for (l = 0; l < csub; l++) {
875                     int maxval = 1;
876                     if (c->books[l] != -1) maxval = venc->codebooks[c->books[l]].nentries;
877                     // coded could be -1, but this still works, cause thats 0
878                     if (coded[counter + k] < maxval) break;
879                 }
880                 assert(l != csub);
881                 cval |= l << cshift;
882                 cshift += c->subclass;
883             }
884             assert(cval < book->nentries);
885             put_bits(pb, book->entries[cval].len, book->entries[cval].codeword);
886         }
887         for (k = 0; k < c->dim; k++) {
888             int book = c->books[cval & (csub-1)];
889             int entry = coded[counter++];
890             cval >>= c->subclass;
891             if (book == -1) continue;
892             if (entry == -1) entry = 0;
893             assert(entry < venc->codebooks[book].nentries);
894             assert(entry >= 0);
895             put_bits(pb, venc->codebooks[book].entries[entry].len, venc->codebooks[book].entries[entry].codeword);
896         }
897     }
898
899     lx = 0;
900     ly = posts[0] * fc->multiplier; // sorted 0 is still 0
901     for (i = 1; i < fc->values; i++) {
902         int pos = fc->list[i].sort;
903         if (coded[pos]) {
904             render_line(lx, ly, fc->list[pos].x, posts[pos] * fc->multiplier, floor, samples);
905             lx = fc->list[pos].x;
906             ly = posts[pos] * fc->multiplier;
907         }
908         if (lx >= samples) break;
909     }
910     if (lx < samples) render_line(lx, ly, samples, ly, floor, samples);
911 }
912
913 static float * put_vector(codebook_t * book, PutBitContext * pb, float * num) {
914     int i;
915     int entry = -1;
916     float distance = 0;
917     assert(book->dimentions);
918     for (i = 0; i < book->nentries; i++) {
919         float d = 0.;
920         int j;
921         if (!book->entries[i].len) continue;
922         for (j = 0; j < book->ndimentions; j++) {
923             float a = (book->dimentions[i * book->ndimentions + j] - num[j]);
924             d += a*a;
925         }
926         if (entry == -1 || distance > d) {
927             entry = i;
928             distance = d;
929         }
930     }
931     put_bits(pb, book->entries[entry].len, book->entries[entry].codeword);
932     return &book->dimentions[entry * book->ndimentions];
933 }
934
935 static void residue_encode(venc_context_t * venc, residue_t * rc, PutBitContext * pb, float * coeffs, int samples, int real_ch) {
936     int pass, i, j, p, k;
937     int psize = rc->partition_size;
938     int partitions = (rc->end - rc->begin) / psize;
939     int channels = (rc->type == 2) ? 1 : real_ch;
940     int classes[channels][partitions];
941     int classwords = venc->codebooks[rc->classbook].ndimentions;
942
943     assert(rc->type == 2);
944     assert(real_ch == 2);
945     for (p = 0; p < partitions; p++) {
946         float max1 = 0., max2 = 0.;
947         int s = rc->begin + p * psize;
948         for (k = s; k < s + psize; k += 2) {
949             if (fabs(coeffs[k / real_ch]) > max1) max1 = fabs(coeffs[k / real_ch]);
950             if (fabs(coeffs[samples + k / real_ch]) > max2) max2 = fabs(coeffs[samples + k / real_ch]);
951         }
952
953         for (i = 0; i < rc->classifications - 1; i++) {
954             if (max1 < rc->maxes[i][0] && max2 < rc->maxes[i][1]) break;
955         }
956         classes[0][p] = i;
957     }
958
959     for (pass = 0; pass < 8; pass++) {
960         p = 0;
961         while (p < partitions) {
962             if (pass == 0) for (j = 0; j < channels; j++) {
963                 codebook_t * book = &venc->codebooks[rc->classbook];
964                 int entry = 0;
965                 for (i = 0; i < classwords; i++) {
966                     entry *= rc->classifications;
967                     entry += classes[j][p + i];
968                 }
969                 assert(entry < book->nentries);
970                 assert(entry >= 0);
971                 put_bits(pb, book->entries[entry].len, book->entries[entry].codeword);
972             }
973             for (i = 0; i < classwords && p < partitions; i++, p++) {
974                 for (j = 0; j < channels; j++) {
975                     int nbook = rc->books[classes[j][p]][pass];
976                     codebook_t * book = &venc->codebooks[nbook];
977                     float * buf = coeffs + samples*j + rc->begin + p*psize;
978                     if (nbook == -1) continue;
979
980                     assert(rc->type == 0 || rc->type == 2);
981                     assert(!(psize % book->ndimentions));
982
983                     if (rc->type == 0) {
984                         for (k = 0; k < psize; k += book->ndimentions) {
985                             float * a = put_vector(book, pb, &buf[k]);
986                             int l;
987                             for (l = 0; l < book->ndimentions; l++) buf[k + l] -= a[l];
988                         }
989                     } else {
990                         for (k = 0; k < psize; k += book->ndimentions) {
991                             int dim = book->ndimentions, s = rc->begin + p * psize + k, l;
992                             float vec[dim], * a = vec;
993                             for (l = s; l < s + dim; l++)
994                                 *a++ = coeffs[(l % real_ch) * samples + l / real_ch];
995                             a = put_vector(book, pb, vec);
996                             for (l = s; l < s + dim; l++)
997                                 coeffs[(l % real_ch) * samples + l / real_ch] -= *a++;
998                         }
999                     }
1000                 }
1001             }
1002         }
1003     }
1004 }
1005
1006 static int window(venc_context_t * venc, signed short * audio, int samples) {
1007     int i, j, channel;
1008     const float * win = venc->win[0];
1009     int window_len = 1 << (venc->blocksize[0] - 1);
1010     float n = (float)(1 << venc->blocksize[0]) / 4.;
1011     // FIXME use dsp
1012
1013     if (!venc->have_saved && !samples) return 0;
1014
1015     if (venc->have_saved) {
1016         for (channel = 0; channel < venc->channels; channel++) {
1017             memcpy(venc->samples + channel*window_len*2, venc->saved + channel*window_len, sizeof(float)*window_len);
1018         }
1019     } else {
1020         for (channel = 0; channel < venc->channels; channel++) {
1021             memset(venc->samples + channel*window_len*2, 0, sizeof(float)*window_len);
1022         }
1023     }
1024
1025     if (samples) {
1026         for (channel = 0; channel < venc->channels; channel++) {
1027             float * offset = venc->samples + channel*window_len*2 + window_len;
1028             j = channel;
1029             for (i = 0; i < samples; i++, j += venc->channels)
1030                 offset[i] = audio[j] / 32768. / n * win[window_len - i - 1];
1031         }
1032     } else {
1033         for (channel = 0; channel < venc->channels; channel++) {
1034             memset(venc->samples + channel*window_len*2 + window_len, 0, sizeof(float)*window_len);
1035         }
1036     }
1037
1038     for (channel = 0; channel < venc->channels; channel++) {
1039         ff_mdct_calc(&venc->mdct[0], venc->coeffs + channel*window_len, venc->samples + channel*window_len*2, venc->floor/*tmp*/);
1040     }
1041
1042     if (samples) {
1043         for (channel = 0; channel < venc->channels; channel++) {
1044             float * offset = venc->saved + channel*window_len;
1045             j = channel;
1046             for (i = 0; i < samples; i++, j += venc->channels)
1047                 offset[i] = audio[j] / 32768. / n * win[i];
1048         }
1049         venc->have_saved = 1;
1050     } else {
1051         venc->have_saved = 0;
1052     }
1053     return 1;
1054 }
1055
1056 static int vorbis_encode_init(AVCodecContext * avccontext)
1057 {
1058     venc_context_t * venc = avccontext->priv_data;
1059
1060     create_vorbis_context(venc, avccontext);
1061
1062     if (avccontext->flags & CODEC_FLAG_QSCALE) venc->quality = avccontext->global_quality / (float)FF_QP2LAMBDA / 100.;
1063     else venc->quality = 0.17;
1064     //if(avccontext->cutoff > 0) cfreq = avccontext->cutoff / 1000.0;
1065
1066     avccontext->extradata_size = put_main_header(venc, (uint8_t**)&avccontext->extradata);
1067
1068     avccontext->frame_size = 1 << (venc->blocksize[0] - 1);
1069
1070     avccontext->coded_frame = avcodec_alloc_frame();
1071     avccontext->coded_frame->key_frame = 1;
1072
1073     return 0;
1074 }
1075
1076 static int vorbis_encode_frame(AVCodecContext * avccontext, unsigned char * packets, int buf_size, void *data)
1077 {
1078     venc_context_t * venc = avccontext->priv_data;
1079     signed short * audio = data;
1080     int samples = data ? avccontext->frame_size : 0;
1081     vorbis_mode_t * mode;
1082     mapping_t * mapping;
1083     PutBitContext pb;
1084     int i;
1085
1086     if (!window(venc, audio, samples)) return 0;
1087     samples = 1 << (venc->blocksize[0] - 1);
1088
1089     init_put_bits(&pb, packets, buf_size);
1090
1091     put_bits(&pb, 1, 0); // magic bit
1092
1093     put_bits(&pb, ilog(venc->nmodes - 1), 0); // 0 bits, the mode
1094
1095     mode = &venc->modes[0];
1096     mapping = &venc->mappings[mode->mapping];
1097     if (mode->blockflag) {
1098         put_bits(&pb, 1, 0);
1099         put_bits(&pb, 1, 0);
1100     }
1101
1102     for (i = 0; i < venc->channels; i++) {
1103         floor_t * fc = &venc->floors[mapping->floor[mapping->mux[i]]];
1104         int posts[fc->values];
1105         floor_fit(venc, fc, &venc->coeffs[i * samples], posts, samples);
1106         floor_encode(venc, fc, &pb, posts, &venc->floor[i * samples], samples);
1107     }
1108
1109     for (i = 0; i < venc->channels; i++) {
1110         int j;
1111         for (j = 0; j < samples; j++) {
1112             venc->coeffs[i * samples + j] /= venc->floor[i * samples + j];
1113         }
1114     }
1115
1116     for (i = 0; i < mapping->coupling_steps; i++) {
1117         float * mag = venc->coeffs + mapping->magnitude[i] * samples;
1118         float * ang = venc->coeffs + mapping->angle[i] * samples;
1119         int j;
1120         for (j = 0; j < samples; j++) {
1121             float m = mag[j];
1122             float a = ang[j];
1123             if (m > 0) {
1124                 ang[j] = m - a;
1125                 if (a > m) mag[j] = a;
1126                 else mag[j] = m;
1127             } else {
1128                 ang[j] = a - m;
1129                 if (a > m) mag[j] = m;
1130                 else mag[j] = a;
1131             }
1132         }
1133     }
1134
1135     residue_encode(venc, &venc->residues[mapping->residue[mapping->mux[0]]], &pb, venc->coeffs, samples, venc->channels);
1136
1137     flush_put_bits(&pb);
1138     return (put_bits_count(&pb) + 7) / 8;
1139 }
1140
1141
1142 static int vorbis_encode_close(AVCodecContext * avccontext)
1143 {
1144     venc_context_t * venc = avccontext->priv_data;
1145     int i;
1146
1147     if (venc->codebooks) for (i = 0; i < venc->ncodebooks; i++) {
1148         av_freep(&venc->codebooks[i].entries);
1149         av_freep(&venc->codebooks[i].quantlist);
1150         av_freep(&venc->codebooks[i].dimentions);
1151     }
1152     av_freep(&venc->codebooks);
1153
1154     if (venc->floors) for (i = 0; i < venc->nfloors; i++) {
1155         int j;
1156         av_freep(&venc->floors[i].classes);
1157         if (venc->floors[i].classes)
1158             for (j = 0; j < venc->floors[i].nclasses; j++)
1159                 av_freep(&venc->floors[i].classes[j].books);
1160         av_freep(&venc->floors[i].partition_to_class);
1161         av_freep(&venc->floors[i].list);
1162     }
1163     av_freep(&venc->floors);
1164
1165     if (venc->residues) for (i = 0; i < venc->nresidues; i++) {
1166         av_freep(&venc->residues[i].books);
1167         av_freep(&venc->residues[i].maxes);
1168     }
1169     av_freep(&venc->residues);
1170
1171     if (venc->mappings) for (i = 0; i < venc->nmappings; i++) {
1172         av_freep(&venc->mappings[i].mux);
1173         av_freep(&venc->mappings[i].floor);
1174         av_freep(&venc->mappings[i].residue);
1175     }
1176     av_freep(&venc->mappings);
1177
1178     av_freep(&venc->modes);
1179
1180     av_freep(&venc->saved);
1181     av_freep(&venc->samples);
1182     av_freep(&venc->floor);
1183     av_freep(&venc->coeffs);
1184
1185     ff_mdct_end(&venc->mdct[0]);
1186     ff_mdct_end(&venc->mdct[1]);
1187
1188     av_freep(&avccontext->coded_frame);
1189     av_freep(&avccontext->extradata);
1190
1191     return 0 ;
1192 }
1193
1194 AVCodec vorbis_encoder = {
1195     "vorbis",
1196     CODEC_TYPE_AUDIO,
1197     CODEC_ID_VORBIS,
1198     sizeof(venc_context_t),
1199     vorbis_encode_init,
1200     vorbis_encode_frame,
1201     vorbis_encode_close,
1202     .capabilities= CODEC_CAP_DELAY,
1203 };