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