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