]> git.sesse.net Git - ffmpeg/blob - libavcodec/vorbis_enc.c
Original Commit: r60 | ods15 | 2006-09-25 12:46:30 +0300 (Mon, 25 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 } residue_t;
89
90 typedef struct {
91     int submaps;
92     int * mux;
93     int * floor;
94     int * residue;
95 } mapping_t;
96
97 typedef struct {
98     int blockflag;
99     int mapping;
100 } vorbis_mode_t;
101
102 typedef struct {
103     int channels;
104     int sample_rate;
105     int blocksize[2]; // in (1<<n) format
106     MDCTContext mdct[2];
107     const float * win[2];
108     int have_saved;
109     float * saved;
110     float * samples;
111     float * floor; // also used for tmp values for mdct
112     float * coeffs; // also used for residue after floor
113
114     int ncodebooks;
115     codebook_t * codebooks;
116
117     int nfloors;
118     floor_t * floors;
119
120     int nresidues;
121     residue_t * residues;
122
123     int nmappings;
124     mapping_t * mappings;
125
126     int nmodes;
127     vorbis_mode_t * modes;
128 } venc_context_t;
129
130 typedef struct {
131     int total;
132     int total_pos;
133     int pos;
134     uint8_t * buf_ptr;
135 } PutBitContext;
136
137 #define ilog(i) av_log2(2*(i))
138
139 static inline void init_put_bits(PutBitContext * pb, uint8_t * buf, int buffer_len) {
140     pb->total = buffer_len * 8;
141     pb->total_pos = 0;
142     pb->pos = 0;
143     pb->buf_ptr = buf;
144 }
145
146 static void put_bits(PutBitContext * pb, int bits, uint64_t val) {
147     if ((pb->total_pos += bits) >= pb->total) return;
148     if (!bits) return;
149     if (pb->pos) {
150         if (pb->pos > bits) {
151             *pb->buf_ptr |= val << (8 - pb->pos);
152             pb->pos -= bits;
153             bits = 0;
154         } else {
155             *pb->buf_ptr++ |= (val << (8 - pb->pos)) & 0xFF;
156             val >>= pb->pos;
157             bits -= pb->pos;
158             pb->pos = 0;
159         }
160     }
161     for (; bits >= 8; bits -= 8) {
162         *pb->buf_ptr++ = val & 0xFF;
163         val >>= 8;
164     }
165     if (bits) {
166         *pb->buf_ptr = val;
167         pb->pos = 8 - bits;
168     }
169 }
170
171 static inline void flush_put_bits(PutBitContext * pb) {
172 }
173
174 static inline int put_bits_count(PutBitContext * pb) {
175     return pb->total_pos;
176 }
177
178 static int cb_lookup_vals(int lookup, int dimentions, int entries) {
179     if (lookup == 1) {
180         int tmp, i;
181         for (tmp = 0; ; tmp++) {
182                 int n = 1;
183                 for (i = 0; i < dimentions; i++) n *= tmp;
184                 if (n > entries) break;
185         }
186         return tmp - 1;
187     } else if (lookup == 2) return dimentions * entries;
188     return 0;
189 }
190
191 static void ready_codebook(codebook_t * cb) {
192     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 };
193     int i;
194
195     for (i = 0; i < cb->nentries; i++) {
196         cb_entry_t * e = &cb->entries[i];
197         int j = 0;
198         if (h[0]) h[0] = 0;
199         else {
200             for (j = e->len; j; j--)
201                 if (h[j]) break;
202             assert(j);
203         }
204         e->codeword = h[j];
205         h[j] = 0;
206         for (j++; j <= e->len; j++) h[j] = e->codeword | (1 << (j - 1));
207     }
208     for (i = 0; i < 33; i++) assert(!h[i]);
209
210     if (!cb->lookup) cb->dimentions = NULL;
211     else {
212         int vals = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries);
213         cb->dimentions = av_malloc(sizeof(float) * cb->nentries * cb->ndimentions);
214         for (i = 0; i < cb->nentries; i++) {
215             float last = 0;
216             int j;
217             int div = 1;
218             for (j = 0; j < cb->ndimentions; j++) {
219                 int off;
220                 if (cb->lookup == 1) off = (i / div) % vals; // lookup type 1
221                 else off = i * cb->ndimentions + j; // lookup type 2
222
223                 cb->dimentions[i * cb->ndimentions + j] = last + cb->min + cb->quantlist[off] * cb->delta;
224                 if (cb->seq_p) last = cb->dimentions[i * cb->ndimentions + j];
225                 div *= vals;
226             }
227         }
228     }
229
230 }
231
232 static void ready_floor(floor_t * fc) {
233     int i;
234     fc->list[0].sort = 0;
235     fc->list[1].sort = 1;
236     for (i = 2; i < fc->values; i++) {
237         int j;
238         fc->list[i].low = 0;
239         fc->list[i].high = 1;
240         fc->list[i].sort = i;
241         for (j = 2; j < i; j++) {
242             int tmp = fc->list[j].x;
243             if (tmp < fc->list[i].x) {
244                 if (tmp > fc->list[fc->list[i].low].x) fc->list[i].low = j;
245             } else {
246                 if (tmp < fc->list[fc->list[i].high].x) fc->list[i].high = j;
247             }
248         }
249     }
250     for (i = 0; i < fc->values - 1; i++) {
251         int j;
252         for (j = i + 1; j < fc->values; j++) {
253             if (fc->list[fc->list[i].sort].x > fc->list[fc->list[j].sort].x) {
254                 int tmp = fc->list[i].sort;
255                 fc->list[i].sort = fc->list[j].sort;
256                 fc->list[j].sort = tmp;
257             }
258         }
259     }
260 }
261
262 static void create_vorbis_context(venc_context_t * venc, AVCodecContext * avccontext) {
263     codebook_t * cb;
264     floor_t * fc;
265     residue_t * rc;
266     mapping_t * mc;
267     int i, book;
268
269     venc->channels = avccontext->channels;
270     venc->sample_rate = avccontext->sample_rate;
271     venc->blocksize[0] = venc->blocksize[1] = 8;
272
273     venc->ncodebooks = 10;
274     venc->codebooks = av_malloc(sizeof(codebook_t) * venc->ncodebooks);
275
276     // codebook 0 - floor1 book, values 0..255
277     cb = &venc->codebooks[0];
278     cb->nentries = 256;
279     cb->entries = av_malloc(sizeof(cb_entry_t) * cb->nentries);
280     for (i = 0; i < cb->nentries; i++) cb->entries[i].len = 8;
281     cb->ndimentions = 0;
282     cb->min = 0.;
283     cb->delta = 0.;
284     cb->seq_p = 0;
285     cb->lookup = 0;
286     cb->quantlist = NULL;
287     ready_codebook(cb);
288
289     // codebook 1 - residue classbook, values 0..1, dimentions 4
290     cb = &venc->codebooks[1];
291     cb->nentries = 2;
292     cb->entries = av_malloc(sizeof(cb_entry_t) * cb->nentries);
293     for (i = 0; i < cb->nentries; i++) cb->entries[i].len = 1;
294     cb->ndimentions = 4;
295     cb->min = 0.;
296     cb->delta = 0.;
297     cb->seq_p = 0;
298     cb->lookup = 0;
299     cb->quantlist = NULL;
300     ready_codebook(cb);
301
302     // codebook 2..9 - vector, for the residue, values -32767..32767, dimentions 1
303     for (book = 0; book < 8; book++) {
304         cb = &venc->codebooks[2 + book];
305         cb->nentries = 5;
306         cb->entries = av_malloc(sizeof(cb_entry_t) * cb->nentries);
307         for (i = 0; i < cb->nentries; i++) cb->entries[i].len = i == 2 ? 1 : 3;
308         cb->ndimentions = 1;
309         cb->delta = 1 << ((7 - book) * 2);
310         cb->min = -cb->delta*2;
311         cb->seq_p = 0;
312         cb->lookup = 1;
313         cb->quantlist = av_malloc(sizeof(int) * cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries));
314         for (i = 0; i < cb->nentries; i++) cb->quantlist[i] = i;
315         ready_codebook(cb);
316     }
317
318     venc->nfloors = 1;
319     venc->floors = av_malloc(sizeof(floor_t) * venc->nfloors);
320
321     // just 1 floor
322     fc = &venc->floors[0];
323     fc->partitions = 1;
324     fc->partition_to_class = av_malloc(sizeof(int) * fc->partitions);
325     for (i = 0; i < fc->partitions; i++) fc->partition_to_class[i] = 0;
326     fc->nclasses = 1;
327     fc->classes = av_malloc(sizeof(floor_class_t) * fc->nclasses);
328     for (i = 0; i < fc->nclasses; i++) {
329         floor_class_t * c = &fc->classes[i];
330         int j, books;
331         c->dim = 1;
332         c->subclass = 0;
333         c->masterbook = 0;
334         books = (1 << c->subclass);
335         c->books = av_malloc(sizeof(int) * books);
336         for (j = 0; j < books; j++) c->books[j] = 0;
337     }
338     fc->multiplier = 1;
339     fc->rangebits = venc->blocksize[0] - 1;
340
341     fc->values = 2;
342     for (i = 0; i < fc->partitions; i++)
343         fc->values += fc->classes[fc->partition_to_class[i]].dim;
344
345     fc->list = av_malloc(sizeof(floor_entry_t) * fc->values);
346     fc->list[0].x = 0;
347     fc->list[1].x = 1 << fc->rangebits;
348     for (i = 2; i < fc->values; i++) {
349         int a = i - 1;
350         int g = ilog(a);
351         assert(g <= fc->rangebits);
352         a ^= 1 << (g-1);
353         g = 1 << (fc->rangebits - g);
354         fc->list[i].x = g + a*2*g;
355     }
356     ready_floor(fc);
357
358     venc->nresidues = 1;
359     venc->residues = av_malloc(sizeof(residue_t) * venc->nresidues);
360
361     // single residue
362     rc = &venc->residues[0];
363     rc->type = 0;
364     rc->begin = 0;
365     rc->end = 1 << (venc->blocksize[0] - 1);
366     rc->partition_size = 64;
367     rc->classifications = 2;
368     rc->classbook = 1;
369     rc->books = av_malloc(sizeof(int[8]) * rc->classifications);
370     for (i = 0; i < rc->classifications; i++) {
371         int j;
372         for (j = 0; j < 8; j++) rc->books[i][j] = 2 + j;
373         rc->books[i][0] = rc->books[i][1] = rc->books[i][2] = rc->books[i][3] = -1;
374     }
375
376     venc->nmappings = 1;
377     venc->mappings = av_malloc(sizeof(mapping_t) * venc->nmappings);
378
379     // single mapping
380     mc = &venc->mappings[0];
381     mc->submaps = 1;
382     mc->mux = av_malloc(sizeof(int) * venc->channels);
383     for (i = 0; i < venc->channels; i++) mc->mux[i] = 0;
384     mc->floor = av_malloc(sizeof(int) * mc->submaps);
385     mc->residue = av_malloc(sizeof(int) * mc->submaps);
386     for (i = 0; i < mc->submaps; i++) {
387         mc->floor[i] = 0;
388         mc->residue[i] = 0;
389     }
390
391     venc->nmodes = 1;
392     venc->modes = av_malloc(sizeof(vorbis_mode_t) * venc->nmodes);
393
394     // single mode
395     venc->modes[0].blockflag = 0;
396     venc->modes[0].mapping = 0;
397
398     venc->have_saved = 0;
399     venc->saved = av_malloc(sizeof(float) * venc->channels * (1 << venc->blocksize[1]) / 2);
400     venc->samples = av_malloc(sizeof(float) * venc->channels * (1 << venc->blocksize[1]));
401     venc->floor = av_malloc(sizeof(float) * venc->channels * (1 << venc->blocksize[1]) / 2);
402     venc->coeffs = av_malloc(sizeof(float) * venc->channels * (1 << venc->blocksize[1]) / 2);
403
404     {
405         const float *vwin[8]={ vwin64, vwin128, vwin256, vwin512, vwin1024, vwin2048, vwin4096, vwin8192 };
406         venc->win[0] = vwin[venc->blocksize[0] - 6];
407         venc->win[1] = vwin[venc->blocksize[1] - 6];
408     }
409
410     ff_mdct_init(&venc->mdct[0], venc->blocksize[0], 0);
411     ff_mdct_init(&venc->mdct[1], venc->blocksize[1], 0);
412 }
413
414 static void put_float(PutBitContext * pb, float f) {
415     int exp, mant;
416     uint32_t res = 0;
417     mant = (int)ldexp(frexp(f, &exp), 20);
418     exp += 788 - 20;
419     if (mant < 0) { res |= (1 << 31); mant = -mant; }
420     res |= mant | (exp << 21);
421     put_bits(pb, 32, res);
422 }
423
424 static void put_codebook_header(PutBitContext * pb, codebook_t * cb) {
425     int i;
426     int ordered = 0;
427
428     put_bits(pb, 24, 0x564342); //magic
429     put_bits(pb, 16, cb->ndimentions);
430     put_bits(pb, 24, cb->nentries);
431
432     for (i = 1; i < cb->nentries; i++) if (cb->entries[i].len < cb->entries[i-1].len) break;
433     if (i == cb->nentries) ordered = 1;
434
435     put_bits(pb, 1, ordered);
436     if (ordered) {
437         int len = cb->entries[0].len;
438         put_bits(pb, 5, len - 1);
439         i = 0;
440         while (i < cb->nentries) {
441             int j;
442             for (j = 0; j+i < cb->nentries; j++) if (cb->entries[j+i].len != len) break;
443             put_bits(pb, ilog(cb->nentries - i), j);
444             i += j;
445             len++;
446         }
447     } else {
448         int sparse = 0;
449         for (i = 0; i < cb->nentries; i++) if (!cb->entries[i].len) break;
450         if (i != cb->nentries) sparse = 1;
451         put_bits(pb, 1, sparse);
452
453         for (i = 0; i < cb->nentries; i++) {
454             if (sparse) put_bits(pb, 1, !!cb->entries[i].len);
455             if (cb->entries[i].len) put_bits(pb, 5, cb->entries[i].len - 1);
456         }
457     }
458
459     put_bits(pb, 4, cb->lookup);
460     if (cb->lookup) {
461         int tmp = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries);
462         int bits = ilog(cb->quantlist[0]);
463
464         for (i = 1; i < tmp; i++) bits = FFMAX(bits, ilog(cb->quantlist[i]));
465
466         put_float(pb, cb->min);
467         put_float(pb, cb->delta);
468
469         put_bits(pb, 4, bits - 1);
470         put_bits(pb, 1, cb->seq_p);
471
472         for (i = 0; i < tmp; i++) put_bits(pb, bits, cb->quantlist[i]);
473     }
474 }
475
476 static void put_floor_header(PutBitContext * pb, floor_t * fc) {
477     int i;
478
479     put_bits(pb, 16, 1); // type, only floor1 is supported
480
481     put_bits(pb, 5, fc->partitions);
482
483     for (i = 0; i < fc->partitions; i++) put_bits(pb, 4, fc->partition_to_class[i]);
484
485     for (i = 0; i < fc->nclasses; i++) {
486         int j, books;
487
488         put_bits(pb, 3, fc->classes[i].dim - 1);
489         put_bits(pb, 2, fc->classes[i].subclass);
490
491         if (fc->classes[i].subclass) put_bits(pb, 8, fc->classes[i].masterbook);
492
493         books = (1 << fc->classes[i].subclass);
494
495         for (j = 0; j < books; j++) put_bits(pb, 8, fc->classes[i].books[j] + 1);
496     }
497
498     put_bits(pb, 2, fc->multiplier - 1);
499     put_bits(pb, 4, fc->rangebits);
500
501     for (i = 2; i < fc->values; i++) put_bits(pb, fc->rangebits, fc->list[i].x);
502 }
503
504 static void put_residue_header(PutBitContext * pb, residue_t * rc) {
505     int i;
506
507     put_bits(pb, 16, rc->type);
508
509     put_bits(pb, 24, rc->begin);
510     put_bits(pb, 24, rc->end);
511     put_bits(pb, 24, rc->partition_size - 1);
512     put_bits(pb, 6, rc->classifications - 1);
513     put_bits(pb, 8, rc->classbook);
514
515     for (i = 0; i < rc->classifications; i++) {
516         int j, tmp = 0;
517         for (j = 0; j < 8; j++) tmp |= (rc->books[i][j] != -1) << j;
518
519         put_bits(pb, 3, tmp & 7);
520         put_bits(pb, 1, tmp > 7);
521
522         if (tmp > 7) put_bits(pb, 5, tmp >> 3);
523     }
524
525     for (i = 0; i < rc->classifications; i++) {
526         int j;
527         for (j = 0; j < 8; j++)
528             if (rc->books[i][j] != -1)
529                 put_bits(pb, 8, rc->books[i][j]);
530     }
531 }
532
533 static int put_main_header(venc_context_t * venc, uint8_t ** out) {
534     int i;
535     PutBitContext pb;
536     uint8_t buffer[50000] = {0}, * p = buffer;
537     int buffer_len = sizeof buffer;
538     int len, hlens[3];
539
540     // identification header
541     init_put_bits(&pb, p, buffer_len);
542     put_bits(&pb, 8, 1); //magic
543     for (i = 0; "vorbis"[i]; i++) put_bits(&pb, 8, "vorbis"[i]);
544     put_bits(&pb, 32, 0); // version
545     put_bits(&pb, 8, venc->channels);
546     put_bits(&pb, 32, venc->sample_rate);
547     put_bits(&pb, 32, 0); // bitrate
548     put_bits(&pb, 32, 0); // bitrate
549     put_bits(&pb, 32, 0); // bitrate
550     put_bits(&pb, 4, venc->blocksize[0]);
551     put_bits(&pb, 4, venc->blocksize[1]);
552     put_bits(&pb, 1, 1); // framing
553
554     flush_put_bits(&pb);
555     hlens[0] = (put_bits_count(&pb) + 7) / 8;
556     buffer_len -= hlens[0];
557     p += hlens[0];
558
559     // comment header
560     init_put_bits(&pb, p, buffer_len);
561     put_bits(&pb, 8, 3); //magic
562     for (i = 0; "vorbis"[i]; i++) put_bits(&pb, 8, "vorbis"[i]);
563     put_bits(&pb, 32, 0); // vendor length TODO
564     put_bits(&pb, 32, 0); // amount of comments
565     put_bits(&pb, 1, 1); // framing
566
567     flush_put_bits(&pb);
568     hlens[1] = (put_bits_count(&pb) + 7) / 8;
569     buffer_len -= hlens[1];
570     p += hlens[1];
571
572     // setup header
573     init_put_bits(&pb, p, buffer_len);
574     put_bits(&pb, 8, 5); //magic
575     for (i = 0; "vorbis"[i]; i++) put_bits(&pb, 8, "vorbis"[i]);
576
577     // codebooks
578     put_bits(&pb, 8, venc->ncodebooks - 1);
579     for (i = 0; i < venc->ncodebooks; i++) put_codebook_header(&pb, &venc->codebooks[i]);
580
581     // time domain, reserved, zero
582     put_bits(&pb, 6, 0);
583     put_bits(&pb, 16, 0);
584
585     // floors
586     put_bits(&pb, 6, venc->nfloors - 1);
587     for (i = 0; i < venc->nfloors; i++) put_floor_header(&pb, &venc->floors[i]);
588
589     // residues
590     put_bits(&pb, 6, venc->nresidues - 1);
591     for (i = 0; i < venc->nresidues; i++) put_residue_header(&pb, &venc->residues[i]);
592
593     // mappings
594     put_bits(&pb, 6, venc->nmappings - 1);
595     for (i = 0; i < venc->nmappings; i++) {
596         mapping_t * mc = &venc->mappings[i];
597         int j;
598         put_bits(&pb, 16, 0); // mapping type
599
600         put_bits(&pb, 1, mc->submaps > 1);
601         if (mc->submaps > 1) put_bits(&pb, 4, mc->submaps - 1);
602
603         put_bits(&pb, 1, 0); // channel coupling
604
605         put_bits(&pb, 2, 0); // reserved
606
607         if (mc->submaps > 1) for (j = 0; j < venc->channels; j++) put_bits(&pb, 4, mc->mux[j]);
608
609         for (j = 0; j < mc->submaps; j++) {
610             put_bits(&pb, 8, 0); // reserved time configuration
611             put_bits(&pb, 8, mc->floor[j]);
612             put_bits(&pb, 8, mc->residue[j]);
613         }
614     }
615
616     // modes
617     put_bits(&pb, 6, venc->nmodes - 1);
618     for (i = 0; i < venc->nmodes; i++) {
619         put_bits(&pb, 1, venc->modes[i].blockflag);
620         put_bits(&pb, 16, 0); // reserved window type
621         put_bits(&pb, 16, 0); // reserved transform type
622         put_bits(&pb, 8, venc->modes[i].mapping);
623     }
624
625     put_bits(&pb, 1, 1); // framing
626
627     flush_put_bits(&pb);
628     hlens[2] = (put_bits_count(&pb) + 7) / 8;
629
630     len = hlens[0] + hlens[1] + hlens[2];
631     p = *out = av_mallocz(64 + len + len/255);
632
633     *p++ = 2;
634     p += av_xiphlacing(p, hlens[0]);
635     p += av_xiphlacing(p, hlens[1]);
636     buffer_len = 0;
637     for (i = 0; i < 3; i++) {
638         memcpy(p, buffer + buffer_len, hlens[i]);
639         p += hlens[i];
640         buffer_len += hlens[i];
641     }
642
643     return p - *out;
644 }
645
646 static void floor_encode(venc_context_t * venc, floor_t * fc, PutBitContext * pb, float * floor, int samples) {
647     int range = 255 / fc->multiplier + 1;
648     int j;
649     put_bits(pb, 1, 1); // non zero
650     put_bits(pb, ilog(range - 1), 180); // magic value - 3.7180282E-05
651     put_bits(pb, ilog(range - 1), 180); // both sides of X
652     for (j = 0; j < fc->partitions; j++) {
653         floor_class_t * c = &fc->classes[fc->partition_to_class[j]];
654         codebook_t * book = &venc->codebooks[c->books[0]];
655         int entry = 0;
656         int k;
657         for (k = 0; k < c->dim; k++) {
658             put_bits(pb, book->entries[entry].len, book->entries[entry].codeword);
659         }
660     }
661
662     for (j = 0; j < samples; j++) {
663         floor[j] = floor1_inverse_db_table[180];
664     }
665 }
666
667 static float * put_vector(codebook_t * book, PutBitContext * pb, float * num) {
668     int i;
669     int entry = -1;
670     float distance = 0;
671     assert(book->dimentions);
672     for (i = 0; i < book->nentries; i++) {
673         float d = 0.;
674         int j;
675         for (j = 0; j < book->ndimentions; j++) {
676             float a = (book->dimentions[i * book->ndimentions + j] - num[j]);
677             d += a*a;
678         }
679         if (entry == -1 || distance > d) {
680             entry = i;
681             distance = d;
682         }
683     }
684     put_bits(pb, book->entries[entry].len, book->entries[entry].codeword);
685     return &book->dimentions[entry * book->ndimentions];
686 }
687
688 static void residue_encode(venc_context_t * venc, residue_t * rc, PutBitContext * pb, float * coeffs, int samples, int channels) {
689     int pass, i, j, p, k;
690     int psize = rc->partition_size;
691     int partitions = (rc->end - rc->begin) / psize;
692     int classes[channels][partitions];
693     int classwords = venc->codebooks[rc->classbook].ndimentions;
694
695     for (pass = 0; pass < 8; pass++) {
696         p = 0;
697         while (p < partitions) {
698             if (pass == 0) for (j = 0; j < channels; j++) {
699                 codebook_t * book = &venc->codebooks[rc->classbook];
700                 int entry = 0;
701                 put_bits(pb, book->entries[entry].len, book->entries[entry].codeword);
702                 for (i = classwords; i--; ) {
703                     classes[j][p + i] = entry % rc->classifications;
704                     entry /= rc->classifications;
705                 }
706             }
707             for (i = 0; i < classwords && p < partitions; i++, p++) {
708                 for (j = 0; j < channels; j++) {
709                     int nbook = rc->books[classes[j][p]][pass];
710                     codebook_t * book = &venc->codebooks[nbook];
711                     float * buf = coeffs + samples*j + rc->begin + p*psize;
712                     if (nbook == -1) continue;
713
714                     assert(rc->type == 0);
715                     assert(!(psize % book->ndimentions));
716
717                     for (k = 0; k < psize; k += book->ndimentions) {
718                         float * a = put_vector(book, pb, &buf[k]);
719                         int l;
720                         for (l = 0; l < book->ndimentions; l++) buf[k + l] -= a[l];
721                     }
722                 }
723             }
724         }
725     }
726 }
727
728 static int window(venc_context_t * venc, signed short * audio, int samples) {
729     int i, j, channel;
730     const float * win = venc->win[0];
731     int window_len = 1 << (venc->blocksize[0] - 1);
732     float n = (float)(1 << venc->blocksize[0]) / 4.;
733     // FIXME use dsp
734
735     if (!venc->have_saved && !samples) return 0;
736
737     if (venc->have_saved) {
738         for (channel = 0; channel < venc->channels; channel++) {
739             memcpy(venc->samples + channel*window_len*2, venc->saved + channel*window_len, sizeof(float)*window_len);
740         }
741     } else {
742         for (channel = 0; channel < venc->channels; channel++) {
743             memset(venc->samples + channel*window_len*2, 0, sizeof(float)*window_len);
744         }
745     }
746
747     if (samples) {
748         for (channel = 0; channel < venc->channels; channel++) {
749             float * offset = venc->samples + channel*window_len*2 + window_len;
750             j = channel;
751             for (i = 0; i < samples; i++, j += venc->channels)
752                 offset[i] = audio[j] / 32768. * win[window_len - i] / n;
753         }
754     } else {
755         for (channel = 0; channel < venc->channels; channel++) {
756             memset(venc->samples + channel*window_len*2 + window_len, 0, sizeof(float)*window_len);
757         }
758     }
759
760     for (channel = 0; channel < venc->channels; channel++) {
761         ff_mdct_calc(&venc->mdct[0], venc->coeffs + channel*window_len, venc->samples + channel*window_len*2, venc->floor/*tmp*/);
762     }
763
764     if (samples) {
765         for (channel = 0; channel < venc->channels; channel++) {
766             float * offset = venc->saved + channel*window_len;
767             j = channel;
768             for (i = 0; i < samples; i++, j += venc->channels)
769                 offset[i] = audio[j] / 32768. * win[i] / n;
770         }
771         venc->have_saved = 1;
772     } else {
773         venc->have_saved = 0;
774     }
775     return 1;
776 }
777
778 static int vorbis_encode_init(AVCodecContext * avccontext)
779 {
780     venc_context_t * venc = avccontext->priv_data;
781
782     create_vorbis_context(venc, avccontext);
783
784     //if (avccontext->flags & CODEC_FLAG_QSCALE) avccontext->global_quality / (float)FF_QP2LAMBDA); else avccontext->bit_rate;
785     //if(avccontext->cutoff > 0) cfreq = avccontext->cutoff / 1000.0;
786
787     avccontext->extradata_size = put_main_header(venc, (uint8_t**)&avccontext->extradata);
788
789     avccontext->frame_size = 1 << (venc->blocksize[0] - 1);
790
791     avccontext->coded_frame = avcodec_alloc_frame();
792     avccontext->coded_frame->key_frame = 1;
793
794     return 0;
795 }
796
797 static int vorbis_encode_frame(AVCodecContext * avccontext, unsigned char * packets, int buf_size, void *data)
798 {
799     venc_context_t * venc = avccontext->priv_data;
800     signed short * audio = data;
801     int samples = data ? avccontext->frame_size : 0;
802     vorbis_mode_t * mode;
803     mapping_t * mapping;
804     PutBitContext pb;
805     int i;
806
807     if (!window(venc, audio, samples)) return 0;
808
809     init_put_bits(&pb, packets, buf_size);
810
811     put_bits(&pb, 1, 0); // magic bit
812
813     put_bits(&pb, ilog(venc->nmodes - 1), 0); // 0 bits, the mode
814
815     mode = &venc->modes[0];
816     mapping = &venc->mappings[mode->mapping];
817     if (mode->blockflag) {
818         put_bits(&pb, 1, 0);
819         put_bits(&pb, 1, 0);
820     }
821
822     for (i = 0; i < venc->channels; i++) {
823         floor_t * fc = &venc->floors[mapping->floor[mapping->mux[i]]];
824         floor_encode(venc, fc, &pb, &venc->floor[i * samples], samples);
825     }
826
827     for (i = 0; i < venc->channels; i++) {
828         int j;
829         for (j = 0; j < samples; j++) {
830             venc->coeffs[i * samples + j] /= venc->floor[i * samples + j];
831         }
832     }
833
834     residue_encode(venc, &venc->residues[mapping->residue[mapping->mux[0]]], &pb, venc->coeffs, samples, venc->channels);
835
836     return (put_bits_count(&pb) + 7) / 8;
837 }
838
839
840 static int vorbis_encode_close(AVCodecContext * avccontext)
841 {
842     venc_context_t * venc = avccontext->priv_data;
843     int i;
844
845     if (venc->codebooks) for (i = 0; i < venc->ncodebooks; i++) {
846         av_freep(&venc->codebooks[i].entries);
847         av_freep(&venc->codebooks[i].quantlist);
848         av_freep(&venc->codebooks[i].dimentions);
849     }
850     av_freep(&venc->codebooks);
851
852     if (venc->floors) for (i = 0; i < venc->nfloors; i++) {
853         int j;
854         av_freep(&venc->floors[i].classes);
855         if (venc->floors[i].classes)
856             for (j = 0; j < venc->floors[i].nclasses; j++)
857                 av_freep(&venc->floors[i].classes[j].books);
858         av_freep(&venc->floors[i].partition_to_class);
859         av_freep(&venc->floors[i].list);
860     }
861     av_freep(&venc->floors);
862
863     if (venc->residues) for (i = 0; i < venc->nresidues; i++) {
864         av_freep(&venc->residues[i].books);
865     }
866     av_freep(&venc->residues);
867
868     if (venc->mappings) for (i = 0; i < venc->nmappings; i++) {
869         av_freep(&venc->mappings[i].mux);
870         av_freep(&venc->mappings[i].floor);
871         av_freep(&venc->mappings[i].residue);
872     }
873     av_freep(&venc->mappings);
874
875     av_freep(&venc->modes);
876
877     av_freep(&venc->saved);
878     av_freep(&venc->samples);
879     av_freep(&venc->floor);
880     av_freep(&venc->coeffs);
881
882     ff_mdct_end(&venc->mdct[0]);
883     ff_mdct_end(&venc->mdct[1]);
884
885     av_freep(&avccontext->coded_frame);
886     av_freep(&avccontext->extradata);
887
888     return 0 ;
889 }
890
891 AVCodec vorbis_encoder = {
892     "vorbis",
893     CODEC_TYPE_AUDIO,
894     CODEC_ID_VORBIS,
895     sizeof(venc_context_t),
896     vorbis_encode_init,
897     vorbis_encode_frame,
898     vorbis_encode_close,
899     .capabilities= CODEC_CAP_DELAY,
900 };