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