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