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