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