]> git.sesse.net Git - ffmpeg/blob - libavcodec/vorbisenc.c
avcodec/vorbisenc: Use a bufqueue in encoding with smaller lengths
[ffmpeg] / 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 "libavutil/float_dsp.h"
29
30 #include "avcodec.h"
31 #include "internal.h"
32 #include "fft.h"
33 #include "mathops.h"
34 #include "vorbis.h"
35 #include "vorbis_enc_data.h"
36
37 #include "audio_frame_queue.h"
38 #include "libavfilter/bufferqueue.h"
39
40 #define BITSTREAM_WRITER_LE
41 #include "put_bits.h"
42
43 #undef NDEBUG
44 #include <assert.h>
45
46 typedef struct vorbis_enc_codebook {
47     int nentries;
48     uint8_t *lens;
49     uint32_t *codewords;
50     int ndimensions;
51     float min;
52     float delta;
53     int seq_p;
54     int lookup;
55     int *quantlist;
56     float *dimensions;
57     float *pow2;
58 } vorbis_enc_codebook;
59
60 typedef struct vorbis_enc_floor_class {
61     int dim;
62     int subclass;
63     int masterbook;
64     int *books;
65 } vorbis_enc_floor_class;
66
67 typedef struct vorbis_enc_floor {
68     int partitions;
69     int *partition_to_class;
70     int nclasses;
71     vorbis_enc_floor_class *classes;
72     int multiplier;
73     int rangebits;
74     int values;
75     vorbis_floor1_entry *list;
76 } vorbis_enc_floor;
77
78 typedef struct vorbis_enc_residue {
79     int type;
80     int begin;
81     int end;
82     int partition_size;
83     int classifications;
84     int classbook;
85     int8_t (*books)[8];
86     float (*maxes)[2];
87 } vorbis_enc_residue;
88
89 typedef struct vorbis_enc_mapping {
90     int submaps;
91     int *mux;
92     int *floor;
93     int *residue;
94     int coupling_steps;
95     int *magnitude;
96     int *angle;
97 } vorbis_enc_mapping;
98
99 typedef struct vorbis_enc_mode {
100     int blockflag;
101     int mapping;
102 } vorbis_enc_mode;
103
104 typedef struct vorbis_enc_context {
105     int channels;
106     int sample_rate;
107     int log2_blocksize[2];
108     FFTContext mdct[2];
109     const float *win[2];
110     int have_saved;
111     float *saved;
112     float *samples;
113     float *floor;  // also used for tmp values for mdct
114     float *coeffs; // also used for residue after floor
115     float quality;
116
117     AudioFrameQueue afq;
118     struct FFBufQueue bufqueue;
119
120     int ncodebooks;
121     vorbis_enc_codebook *codebooks;
122
123     int nfloors;
124     vorbis_enc_floor *floors;
125
126     int nresidues;
127     vorbis_enc_residue *residues;
128
129     int nmappings;
130     vorbis_enc_mapping *mappings;
131
132     int nmodes;
133     vorbis_enc_mode *modes;
134
135     int64_t next_pts;
136
137     AVFloatDSPContext *fdsp;
138 } vorbis_enc_context;
139
140 #define MAX_CHANNELS     2
141 #define MAX_CODEBOOK_DIM 8
142
143 #define MAX_FLOOR_CLASS_DIM  4
144 #define NUM_FLOOR_PARTITIONS 8
145 #define MAX_FLOOR_VALUES     (MAX_FLOOR_CLASS_DIM*NUM_FLOOR_PARTITIONS+2)
146
147 #define RESIDUE_SIZE           1600
148 #define RESIDUE_PART_SIZE      32
149 #define NUM_RESIDUE_PARTITIONS (RESIDUE_SIZE/RESIDUE_PART_SIZE)
150
151 static inline int put_codeword(PutBitContext *pb, vorbis_enc_codebook *cb,
152                                int entry)
153 {
154     av_assert2(entry >= 0);
155     av_assert2(entry < cb->nentries);
156     av_assert2(cb->lens[entry]);
157     if (pb->size_in_bits - put_bits_count(pb) < cb->lens[entry])
158         return AVERROR(EINVAL);
159     put_bits(pb, cb->lens[entry], cb->codewords[entry]);
160     return 0;
161 }
162
163 static int cb_lookup_vals(int lookup, int dimensions, int entries)
164 {
165     if (lookup == 1)
166         return ff_vorbis_nth_root(entries, dimensions);
167     else if (lookup == 2)
168         return dimensions *entries;
169     return 0;
170 }
171
172 static int ready_codebook(vorbis_enc_codebook *cb)
173 {
174     int i;
175
176     ff_vorbis_len2vlc(cb->lens, cb->codewords, cb->nentries);
177
178     if (!cb->lookup) {
179         cb->pow2 = cb->dimensions = NULL;
180     } else {
181         int vals = cb_lookup_vals(cb->lookup, cb->ndimensions, cb->nentries);
182         cb->dimensions = av_malloc_array(cb->nentries, sizeof(float) * cb->ndimensions);
183         cb->pow2 = av_mallocz_array(cb->nentries, sizeof(float));
184         if (!cb->dimensions || !cb->pow2)
185             return AVERROR(ENOMEM);
186         for (i = 0; i < cb->nentries; i++) {
187             float last = 0;
188             int j;
189             int div = 1;
190             for (j = 0; j < cb->ndimensions; j++) {
191                 int off;
192                 if (cb->lookup == 1)
193                     off = (i / div) % vals; // lookup type 1
194                 else
195                     off = i * cb->ndimensions + j; // lookup type 2
196
197                 cb->dimensions[i * cb->ndimensions + j] = last + cb->min + cb->quantlist[off] * cb->delta;
198                 if (cb->seq_p)
199                     last = cb->dimensions[i * cb->ndimensions + j];
200                 cb->pow2[i] += cb->dimensions[i * cb->ndimensions + j] * cb->dimensions[i * cb->ndimensions + j];
201                 div *= vals;
202             }
203             cb->pow2[i] /= 2.0;
204         }
205     }
206     return 0;
207 }
208
209 static int ready_residue(vorbis_enc_residue *rc, vorbis_enc_context *venc)
210 {
211     int i;
212     av_assert0(rc->type == 2);
213     rc->maxes = av_mallocz_array(rc->classifications, sizeof(float[2]));
214     if (!rc->maxes)
215         return AVERROR(ENOMEM);
216     for (i = 0; i < rc->classifications; i++) {
217         int j;
218         vorbis_enc_codebook * cb;
219         for (j = 0; j < 8; j++)
220             if (rc->books[i][j] != -1)
221                 break;
222         if (j == 8) // zero
223             continue;
224         cb = &venc->codebooks[rc->books[i][j]];
225         assert(cb->ndimensions >= 2);
226         assert(cb->lookup);
227
228         for (j = 0; j < cb->nentries; j++) {
229             float a;
230             if (!cb->lens[j])
231                 continue;
232             a = fabs(cb->dimensions[j * cb->ndimensions]);
233             if (a > rc->maxes[i][0])
234                 rc->maxes[i][0] = a;
235             a = fabs(cb->dimensions[j * cb->ndimensions + 1]);
236             if (a > rc->maxes[i][1])
237                 rc->maxes[i][1] = a;
238         }
239     }
240     // small bias
241     for (i = 0; i < rc->classifications; i++) {
242         rc->maxes[i][0] += 0.8;
243         rc->maxes[i][1] += 0.8;
244     }
245     return 0;
246 }
247
248 static av_cold int dsp_init(AVCodecContext *avctx, vorbis_enc_context *venc)
249 {
250     int ret = 0;
251
252     venc->fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
253     if (!venc->fdsp)
254         return AVERROR(ENOMEM);
255
256     // init windows
257     venc->win[0] = ff_vorbis_vwin[venc->log2_blocksize[0] - 6];
258     venc->win[1] = ff_vorbis_vwin[venc->log2_blocksize[1] - 6];
259
260     if ((ret = ff_mdct_init(&venc->mdct[0], venc->log2_blocksize[0], 0, 1.0)) < 0)
261         return ret;
262     if ((ret = ff_mdct_init(&venc->mdct[1], venc->log2_blocksize[1], 0, 1.0)) < 0)
263         return ret;
264
265     return 0;
266 }
267
268 static int create_vorbis_context(vorbis_enc_context *venc,
269                                  AVCodecContext *avctx)
270 {
271     vorbis_enc_floor   *fc;
272     vorbis_enc_residue *rc;
273     vorbis_enc_mapping *mc;
274     int i, book, ret;
275
276     venc->channels    = avctx->channels;
277     venc->sample_rate = avctx->sample_rate;
278     venc->log2_blocksize[0] = venc->log2_blocksize[1] = 11;
279
280     venc->ncodebooks = FF_ARRAY_ELEMS(cvectors);
281     venc->codebooks  = av_malloc(sizeof(vorbis_enc_codebook) * venc->ncodebooks);
282     if (!venc->codebooks)
283         return AVERROR(ENOMEM);
284
285     // codebook 0..14 - floor1 book, values 0..255
286     // codebook 15 residue masterbook
287     // codebook 16..29 residue
288     for (book = 0; book < venc->ncodebooks; book++) {
289         vorbis_enc_codebook *cb = &venc->codebooks[book];
290         int vals;
291         cb->ndimensions = cvectors[book].dim;
292         cb->nentries    = cvectors[book].real_len;
293         cb->min         = cvectors[book].min;
294         cb->delta       = cvectors[book].delta;
295         cb->lookup      = cvectors[book].lookup;
296         cb->seq_p       = 0;
297
298         cb->lens      = av_malloc_array(cb->nentries, sizeof(uint8_t));
299         cb->codewords = av_malloc_array(cb->nentries, sizeof(uint32_t));
300         if (!cb->lens || !cb->codewords)
301             return AVERROR(ENOMEM);
302         memcpy(cb->lens, cvectors[book].clens, cvectors[book].len);
303         memset(cb->lens + cvectors[book].len, 0, cb->nentries - cvectors[book].len);
304
305         if (cb->lookup) {
306             vals = cb_lookup_vals(cb->lookup, cb->ndimensions, cb->nentries);
307             cb->quantlist = av_malloc_array(vals, sizeof(int));
308             if (!cb->quantlist)
309                 return AVERROR(ENOMEM);
310             for (i = 0; i < vals; i++)
311                 cb->quantlist[i] = cvectors[book].quant[i];
312         } else {
313             cb->quantlist = NULL;
314         }
315         if ((ret = ready_codebook(cb)) < 0)
316             return ret;
317     }
318
319     venc->nfloors = 1;
320     venc->floors  = av_malloc(sizeof(vorbis_enc_floor) * venc->nfloors);
321     if (!venc->floors)
322         return AVERROR(ENOMEM);
323
324     // just 1 floor
325     fc = &venc->floors[0];
326     fc->partitions         = NUM_FLOOR_PARTITIONS;
327     fc->partition_to_class = av_malloc(sizeof(int) * fc->partitions);
328     if (!fc->partition_to_class)
329         return AVERROR(ENOMEM);
330     fc->nclasses           = 0;
331     for (i = 0; i < fc->partitions; i++) {
332         static const int a[] = {0, 1, 2, 2, 3, 3, 4, 4};
333         fc->partition_to_class[i] = a[i];
334         fc->nclasses = FFMAX(fc->nclasses, fc->partition_to_class[i]);
335     }
336     fc->nclasses++;
337     fc->classes = av_malloc_array(fc->nclasses, sizeof(vorbis_enc_floor_class));
338     if (!fc->classes)
339         return AVERROR(ENOMEM);
340     for (i = 0; i < fc->nclasses; i++) {
341         vorbis_enc_floor_class * c = &fc->classes[i];
342         int j, books;
343         c->dim        = floor_classes[i].dim;
344         c->subclass   = floor_classes[i].subclass;
345         c->masterbook = floor_classes[i].masterbook;
346         books         = (1 << c->subclass);
347         c->books      = av_malloc_array(books, sizeof(int));
348         if (!c->books)
349             return AVERROR(ENOMEM);
350         for (j = 0; j < books; j++)
351             c->books[j] = floor_classes[i].nbooks[j];
352     }
353     fc->multiplier = 2;
354     fc->rangebits  = venc->log2_blocksize[0] - 1;
355
356     fc->values = 2;
357     for (i = 0; i < fc->partitions; i++)
358         fc->values += fc->classes[fc->partition_to_class[i]].dim;
359
360     fc->list = av_malloc_array(fc->values, sizeof(vorbis_floor1_entry));
361     if (!fc->list)
362         return AVERROR(ENOMEM);
363     fc->list[0].x = 0;
364     fc->list[1].x = 1 << fc->rangebits;
365     for (i = 2; i < fc->values; i++) {
366         static const int a[] = {
367              93, 23,372,  6, 46,186,750, 14, 33, 65,
368             130,260,556,  3, 10, 18, 28, 39, 55, 79,
369             111,158,220,312,464,650,850
370         };
371         fc->list[i].x = a[i - 2];
372     }
373     if (ff_vorbis_ready_floor1_list(avctx, fc->list, fc->values))
374         return AVERROR_BUG;
375
376     venc->nresidues = 1;
377     venc->residues  = av_malloc(sizeof(vorbis_enc_residue) * venc->nresidues);
378     if (!venc->residues)
379         return AVERROR(ENOMEM);
380
381     // single residue
382     rc = &venc->residues[0];
383     rc->type            = 2;
384     rc->begin           = 0;
385     rc->end             = 1600;
386     rc->partition_size  = 32;
387     rc->classifications = 10;
388     rc->classbook       = 15;
389     rc->books           = av_malloc(sizeof(*rc->books) * rc->classifications);
390     if (!rc->books)
391         return AVERROR(ENOMEM);
392     {
393         static const int8_t a[10][8] = {
394             { -1, -1, -1, -1, -1, -1, -1, -1, },
395             { -1, -1, 16, -1, -1, -1, -1, -1, },
396             { -1, -1, 17, -1, -1, -1, -1, -1, },
397             { -1, -1, 18, -1, -1, -1, -1, -1, },
398             { -1, -1, 19, -1, -1, -1, -1, -1, },
399             { -1, -1, 20, -1, -1, -1, -1, -1, },
400             { -1, -1, 21, -1, -1, -1, -1, -1, },
401             { 22, 23, -1, -1, -1, -1, -1, -1, },
402             { 24, 25, -1, -1, -1, -1, -1, -1, },
403             { 26, 27, 28, -1, -1, -1, -1, -1, },
404         };
405         memcpy(rc->books, a, sizeof a);
406     }
407     if ((ret = ready_residue(rc, venc)) < 0)
408         return ret;
409
410     venc->nmappings = 1;
411     venc->mappings  = av_malloc(sizeof(vorbis_enc_mapping) * venc->nmappings);
412     if (!venc->mappings)
413         return AVERROR(ENOMEM);
414
415     // single mapping
416     mc = &venc->mappings[0];
417     mc->submaps = 1;
418     mc->mux     = av_malloc(sizeof(int) * venc->channels);
419     if (!mc->mux)
420         return AVERROR(ENOMEM);
421     for (i = 0; i < venc->channels; i++)
422         mc->mux[i] = 0;
423     mc->floor   = av_malloc(sizeof(int) * mc->submaps);
424     mc->residue = av_malloc(sizeof(int) * mc->submaps);
425     if (!mc->floor || !mc->residue)
426         return AVERROR(ENOMEM);
427     for (i = 0; i < mc->submaps; i++) {
428         mc->floor[i]   = 0;
429         mc->residue[i] = 0;
430     }
431     mc->coupling_steps = venc->channels == 2 ? 1 : 0;
432     mc->magnitude      = av_malloc(sizeof(int) * mc->coupling_steps);
433     mc->angle          = av_malloc(sizeof(int) * mc->coupling_steps);
434     if (!mc->magnitude || !mc->angle)
435         return AVERROR(ENOMEM);
436     if (mc->coupling_steps) {
437         mc->magnitude[0] = 0;
438         mc->angle[0]     = 1;
439     }
440
441     venc->nmodes = 1;
442     venc->modes  = av_malloc(sizeof(vorbis_enc_mode) * venc->nmodes);
443     if (!venc->modes)
444         return AVERROR(ENOMEM);
445
446     // single mode
447     venc->modes[0].blockflag = 0;
448     venc->modes[0].mapping   = 0;
449
450     venc->have_saved = 0;
451     venc->saved      = av_malloc_array(sizeof(float) * venc->channels, (1 << venc->log2_blocksize[1]) / 2);
452     venc->samples    = av_malloc_array(sizeof(float) * venc->channels, (1 << venc->log2_blocksize[1]));
453     venc->floor      = av_malloc_array(sizeof(float) * venc->channels, (1 << venc->log2_blocksize[1]) / 2);
454     venc->coeffs     = av_malloc_array(sizeof(float) * venc->channels, (1 << venc->log2_blocksize[1]) / 2);
455     if (!venc->saved || !venc->samples || !venc->floor || !venc->coeffs)
456         return AVERROR(ENOMEM);
457
458     if ((ret = dsp_init(avctx, venc)) < 0)
459         return ret;
460
461     return 0;
462 }
463
464 static void put_float(PutBitContext *pb, float f)
465 {
466     int exp, mant;
467     uint32_t res = 0;
468     mant = (int)ldexp(frexp(f, &exp), 20);
469     exp += 788 - 20;
470     if (mant < 0) {
471         res |= (1U << 31);
472         mant = -mant;
473     }
474     res |= mant | (exp << 21);
475     put_bits32(pb, res);
476 }
477
478 static void put_codebook_header(PutBitContext *pb, vorbis_enc_codebook *cb)
479 {
480     int i;
481     int ordered = 0;
482
483     put_bits(pb, 24, 0x564342); //magic
484     put_bits(pb, 16, cb->ndimensions);
485     put_bits(pb, 24, cb->nentries);
486
487     for (i = 1; i < cb->nentries; i++)
488         if (cb->lens[i] < cb->lens[i-1])
489             break;
490     if (i == cb->nentries)
491         ordered = 1;
492
493     put_bits(pb, 1, ordered);
494     if (ordered) {
495         int len = cb->lens[0];
496         put_bits(pb, 5, len - 1);
497         i = 0;
498         while (i < cb->nentries) {
499             int j;
500             for (j = 0; j+i < cb->nentries; j++)
501                 if (cb->lens[j+i] != len)
502                     break;
503             put_bits(pb, ilog(cb->nentries - i), j);
504             i += j;
505             len++;
506         }
507     } else {
508         int sparse = 0;
509         for (i = 0; i < cb->nentries; i++)
510             if (!cb->lens[i])
511                 break;
512         if (i != cb->nentries)
513             sparse = 1;
514         put_bits(pb, 1, sparse);
515
516         for (i = 0; i < cb->nentries; i++) {
517             if (sparse)
518                 put_bits(pb, 1, !!cb->lens[i]);
519             if (cb->lens[i])
520                 put_bits(pb, 5, cb->lens[i] - 1);
521         }
522     }
523
524     put_bits(pb, 4, cb->lookup);
525     if (cb->lookup) {
526         int tmp  = cb_lookup_vals(cb->lookup, cb->ndimensions, cb->nentries);
527         int bits = ilog(cb->quantlist[0]);
528
529         for (i = 1; i < tmp; i++)
530             bits = FFMAX(bits, ilog(cb->quantlist[i]));
531
532         put_float(pb, cb->min);
533         put_float(pb, cb->delta);
534
535         put_bits(pb, 4, bits - 1);
536         put_bits(pb, 1, cb->seq_p);
537
538         for (i = 0; i < tmp; i++)
539             put_bits(pb, bits, cb->quantlist[i]);
540     }
541 }
542
543 static void put_floor_header(PutBitContext *pb, vorbis_enc_floor *fc)
544 {
545     int i;
546
547     put_bits(pb, 16, 1); // type, only floor1 is supported
548
549     put_bits(pb, 5, fc->partitions);
550
551     for (i = 0; i < fc->partitions; i++)
552         put_bits(pb, 4, fc->partition_to_class[i]);
553
554     for (i = 0; i < fc->nclasses; i++) {
555         int j, books;
556
557         put_bits(pb, 3, fc->classes[i].dim - 1);
558         put_bits(pb, 2, fc->classes[i].subclass);
559
560         if (fc->classes[i].subclass)
561             put_bits(pb, 8, fc->classes[i].masterbook);
562
563         books = (1 << fc->classes[i].subclass);
564
565         for (j = 0; j < books; j++)
566             put_bits(pb, 8, fc->classes[i].books[j] + 1);
567     }
568
569     put_bits(pb, 2, fc->multiplier - 1);
570     put_bits(pb, 4, fc->rangebits);
571
572     for (i = 2; i < fc->values; i++)
573         put_bits(pb, fc->rangebits, fc->list[i].x);
574 }
575
576 static void put_residue_header(PutBitContext *pb, vorbis_enc_residue *rc)
577 {
578     int i;
579
580     put_bits(pb, 16, rc->type);
581
582     put_bits(pb, 24, rc->begin);
583     put_bits(pb, 24, rc->end);
584     put_bits(pb, 24, rc->partition_size - 1);
585     put_bits(pb, 6, rc->classifications - 1);
586     put_bits(pb, 8, rc->classbook);
587
588     for (i = 0; i < rc->classifications; i++) {
589         int j, tmp = 0;
590         for (j = 0; j < 8; j++)
591             tmp |= (rc->books[i][j] != -1) << j;
592
593         put_bits(pb, 3, tmp & 7);
594         put_bits(pb, 1, tmp > 7);
595
596         if (tmp > 7)
597             put_bits(pb, 5, tmp >> 3);
598     }
599
600     for (i = 0; i < rc->classifications; i++) {
601         int j;
602         for (j = 0; j < 8; j++)
603             if (rc->books[i][j] != -1)
604                 put_bits(pb, 8, rc->books[i][j]);
605     }
606 }
607
608 static int put_main_header(vorbis_enc_context *venc, uint8_t **out)
609 {
610     int i;
611     PutBitContext pb;
612     int len, hlens[3];
613     int buffer_len = 50000;
614     uint8_t *buffer = av_mallocz(buffer_len), *p = buffer;
615     if (!buffer)
616         return AVERROR(ENOMEM);
617
618     // identification header
619     init_put_bits(&pb, p, buffer_len);
620     put_bits(&pb, 8, 1); //magic
621     for (i = 0; "vorbis"[i]; i++)
622         put_bits(&pb, 8, "vorbis"[i]);
623     put_bits32(&pb, 0); // version
624     put_bits(&pb,  8, venc->channels);
625     put_bits32(&pb, venc->sample_rate);
626     put_bits32(&pb, 0); // bitrate
627     put_bits32(&pb, 0); // bitrate
628     put_bits32(&pb, 0); // bitrate
629     put_bits(&pb,  4, venc->log2_blocksize[0]);
630     put_bits(&pb,  4, venc->log2_blocksize[1]);
631     put_bits(&pb,  1, 1); // framing
632
633     flush_put_bits(&pb);
634     hlens[0] = put_bits_count(&pb) >> 3;
635     buffer_len -= hlens[0];
636     p += hlens[0];
637
638     // comment header
639     init_put_bits(&pb, p, buffer_len);
640     put_bits(&pb, 8, 3); //magic
641     for (i = 0; "vorbis"[i]; i++)
642         put_bits(&pb, 8, "vorbis"[i]);
643     put_bits32(&pb, 0); // vendor length TODO
644     put_bits32(&pb, 0); // amount of comments
645     put_bits(&pb,  1, 1); // framing
646
647     flush_put_bits(&pb);
648     hlens[1] = put_bits_count(&pb) >> 3;
649     buffer_len -= hlens[1];
650     p += hlens[1];
651
652     // setup header
653     init_put_bits(&pb, p, buffer_len);
654     put_bits(&pb, 8, 5); //magic
655     for (i = 0; "vorbis"[i]; i++)
656         put_bits(&pb, 8, "vorbis"[i]);
657
658     // codebooks
659     put_bits(&pb, 8, venc->ncodebooks - 1);
660     for (i = 0; i < venc->ncodebooks; i++)
661         put_codebook_header(&pb, &venc->codebooks[i]);
662
663     // time domain, reserved, zero
664     put_bits(&pb,  6, 0);
665     put_bits(&pb, 16, 0);
666
667     // floors
668     put_bits(&pb, 6, venc->nfloors - 1);
669     for (i = 0; i < venc->nfloors; i++)
670         put_floor_header(&pb, &venc->floors[i]);
671
672     // residues
673     put_bits(&pb, 6, venc->nresidues - 1);
674     for (i = 0; i < venc->nresidues; i++)
675         put_residue_header(&pb, &venc->residues[i]);
676
677     // mappings
678     put_bits(&pb, 6, venc->nmappings - 1);
679     for (i = 0; i < venc->nmappings; i++) {
680         vorbis_enc_mapping *mc = &venc->mappings[i];
681         int j;
682         put_bits(&pb, 16, 0); // mapping type
683
684         put_bits(&pb, 1, mc->submaps > 1);
685         if (mc->submaps > 1)
686             put_bits(&pb, 4, mc->submaps - 1);
687
688         put_bits(&pb, 1, !!mc->coupling_steps);
689         if (mc->coupling_steps) {
690             put_bits(&pb, 8, mc->coupling_steps - 1);
691             for (j = 0; j < mc->coupling_steps; j++) {
692                 put_bits(&pb, ilog(venc->channels - 1), mc->magnitude[j]);
693                 put_bits(&pb, ilog(venc->channels - 1), mc->angle[j]);
694             }
695         }
696
697         put_bits(&pb, 2, 0); // reserved
698
699         if (mc->submaps > 1)
700             for (j = 0; j < venc->channels; j++)
701                 put_bits(&pb, 4, mc->mux[j]);
702
703         for (j = 0; j < mc->submaps; j++) {
704             put_bits(&pb, 8, 0); // reserved time configuration
705             put_bits(&pb, 8, mc->floor[j]);
706             put_bits(&pb, 8, mc->residue[j]);
707         }
708     }
709
710     // modes
711     put_bits(&pb, 6, venc->nmodes - 1);
712     for (i = 0; i < venc->nmodes; i++) {
713         put_bits(&pb, 1, venc->modes[i].blockflag);
714         put_bits(&pb, 16, 0); // reserved window type
715         put_bits(&pb, 16, 0); // reserved transform type
716         put_bits(&pb, 8, venc->modes[i].mapping);
717     }
718
719     put_bits(&pb, 1, 1); // framing
720
721     flush_put_bits(&pb);
722     hlens[2] = put_bits_count(&pb) >> 3;
723
724     len = hlens[0] + hlens[1] + hlens[2];
725     p = *out = av_mallocz(64 + len + len/255);
726     if (!p)
727         return AVERROR(ENOMEM);
728
729     *p++ = 2;
730     p += av_xiphlacing(p, hlens[0]);
731     p += av_xiphlacing(p, hlens[1]);
732     buffer_len = 0;
733     for (i = 0; i < 3; i++) {
734         memcpy(p, buffer + buffer_len, hlens[i]);
735         p += hlens[i];
736         buffer_len += hlens[i];
737     }
738
739     av_freep(&buffer);
740     return p - *out;
741 }
742
743 static float get_floor_average(vorbis_enc_floor * fc, float *coeffs, int i)
744 {
745     int begin = fc->list[fc->list[FFMAX(i-1, 0)].sort].x;
746     int end   = fc->list[fc->list[FFMIN(i+1, fc->values - 1)].sort].x;
747     int j;
748     float average = 0;
749
750     for (j = begin; j < end; j++)
751         average += fabs(coeffs[j]);
752     return average / (end - begin);
753 }
754
755 static void floor_fit(vorbis_enc_context *venc, vorbis_enc_floor *fc,
756                       float *coeffs, uint16_t *posts, int samples)
757 {
758     int range = 255 / fc->multiplier + 1;
759     int i;
760     float tot_average = 0.0;
761     float averages[MAX_FLOOR_VALUES];
762     for (i = 0; i < fc->values; i++) {
763         averages[i] = get_floor_average(fc, coeffs, i);
764         tot_average += averages[i];
765     }
766     tot_average /= fc->values;
767     tot_average /= venc->quality;
768
769     for (i = 0; i < fc->values; i++) {
770         int position  = fc->list[fc->list[i].sort].x;
771         float average = averages[i];
772         int j;
773
774         average = sqrt(tot_average * average) * pow(1.25f, position*0.005f); // MAGIC!
775         for (j = 0; j < range - 1; j++)
776             if (ff_vorbis_floor1_inverse_db_table[j * fc->multiplier] > average)
777                 break;
778         posts[fc->list[i].sort] = j;
779     }
780 }
781
782 static int render_point(int x0, int y0, int x1, int y1, int x)
783 {
784     return y0 +  (x - x0) * (y1 - y0) / (x1 - x0);
785 }
786
787 static int floor_encode(vorbis_enc_context *venc, vorbis_enc_floor *fc,
788                         PutBitContext *pb, uint16_t *posts,
789                         float *floor, int samples)
790 {
791     int range = 255 / fc->multiplier + 1;
792     int coded[MAX_FLOOR_VALUES]; // first 2 values are unused
793     int i, counter;
794
795     if (pb->size_in_bits - put_bits_count(pb) < 1 + 2 * ilog(range - 1))
796         return AVERROR(EINVAL);
797     put_bits(pb, 1, 1); // non zero
798     put_bits(pb, ilog(range - 1), posts[0]);
799     put_bits(pb, ilog(range - 1), posts[1]);
800     coded[0] = coded[1] = 1;
801
802     for (i = 2; i < fc->values; i++) {
803         int predicted = render_point(fc->list[fc->list[i].low].x,
804                                      posts[fc->list[i].low],
805                                      fc->list[fc->list[i].high].x,
806                                      posts[fc->list[i].high],
807                                      fc->list[i].x);
808         int highroom = range - predicted;
809         int lowroom = predicted;
810         int room = FFMIN(highroom, lowroom);
811         if (predicted == posts[i]) {
812             coded[i] = 0; // must be used later as flag!
813             continue;
814         } else {
815             if (!coded[fc->list[i].low ])
816                 coded[fc->list[i].low ] = -1;
817             if (!coded[fc->list[i].high])
818                 coded[fc->list[i].high] = -1;
819         }
820         if (posts[i] > predicted) {
821             if (posts[i] - predicted > room)
822                 coded[i] = posts[i] - predicted + lowroom;
823             else
824                 coded[i] = (posts[i] - predicted) << 1;
825         } else {
826             if (predicted - posts[i] > room)
827                 coded[i] = predicted - posts[i] + highroom - 1;
828             else
829                 coded[i] = ((predicted - posts[i]) << 1) - 1;
830         }
831     }
832
833     counter = 2;
834     for (i = 0; i < fc->partitions; i++) {
835         vorbis_enc_floor_class * c = &fc->classes[fc->partition_to_class[i]];
836         int k, cval = 0, csub = 1<<c->subclass;
837         if (c->subclass) {
838             vorbis_enc_codebook * book = &venc->codebooks[c->masterbook];
839             int cshift = 0;
840             for (k = 0; k < c->dim; k++) {
841                 int l;
842                 for (l = 0; l < csub; l++) {
843                     int maxval = 1;
844                     if (c->books[l] != -1)
845                         maxval = venc->codebooks[c->books[l]].nentries;
846                     // coded could be -1, but this still works, cause that is 0
847                     if (coded[counter + k] < maxval)
848                         break;
849                 }
850                 assert(l != csub);
851                 cval   |= l << cshift;
852                 cshift += c->subclass;
853             }
854             if (put_codeword(pb, book, cval))
855                 return AVERROR(EINVAL);
856         }
857         for (k = 0; k < c->dim; k++) {
858             int book  = c->books[cval & (csub-1)];
859             int entry = coded[counter++];
860             cval >>= c->subclass;
861             if (book == -1)
862                 continue;
863             if (entry == -1)
864                 entry = 0;
865             if (put_codeword(pb, &venc->codebooks[book], entry))
866                 return AVERROR(EINVAL);
867         }
868     }
869
870     ff_vorbis_floor1_render_list(fc->list, fc->values, posts, coded,
871                                  fc->multiplier, floor, samples);
872
873     return 0;
874 }
875
876 static float *put_vector(vorbis_enc_codebook *book, PutBitContext *pb,
877                          float *num)
878 {
879     int i, entry = -1;
880     float distance = FLT_MAX;
881     assert(book->dimensions);
882     for (i = 0; i < book->nentries; i++) {
883         float * vec = book->dimensions + i * book->ndimensions, d = book->pow2[i];
884         int j;
885         if (!book->lens[i])
886             continue;
887         for (j = 0; j < book->ndimensions; j++)
888             d -= vec[j] * num[j];
889         if (distance > d) {
890             entry    = i;
891             distance = d;
892         }
893     }
894     if (put_codeword(pb, book, entry))
895         return NULL;
896     return &book->dimensions[entry * book->ndimensions];
897 }
898
899 static int residue_encode(vorbis_enc_context *venc, vorbis_enc_residue *rc,
900                           PutBitContext *pb, float *coeffs, int samples,
901                           int real_ch)
902 {
903     int pass, i, j, p, k;
904     int psize      = rc->partition_size;
905     int partitions = (rc->end - rc->begin) / psize;
906     int channels   = (rc->type == 2) ? 1 : real_ch;
907     int classes[MAX_CHANNELS][NUM_RESIDUE_PARTITIONS];
908     int classwords = venc->codebooks[rc->classbook].ndimensions;
909
910     av_assert0(rc->type == 2);
911     av_assert0(real_ch == 2);
912     for (p = 0; p < partitions; p++) {
913         float max1 = 0.0, max2 = 0.0;
914         int s = rc->begin + p * psize;
915         for (k = s; k < s + psize; k += 2) {
916             max1 = FFMAX(max1, fabs(coeffs[          k / real_ch]));
917             max2 = FFMAX(max2, fabs(coeffs[samples + k / real_ch]));
918         }
919
920         for (i = 0; i < rc->classifications - 1; i++)
921             if (max1 < rc->maxes[i][0] && max2 < rc->maxes[i][1])
922                 break;
923         classes[0][p] = i;
924     }
925
926     for (pass = 0; pass < 8; pass++) {
927         p = 0;
928         while (p < partitions) {
929             if (pass == 0)
930                 for (j = 0; j < channels; j++) {
931                     vorbis_enc_codebook * book = &venc->codebooks[rc->classbook];
932                     int entry = 0;
933                     for (i = 0; i < classwords; i++) {
934                         entry *= rc->classifications;
935                         entry += classes[j][p + i];
936                     }
937                     if (put_codeword(pb, book, entry))
938                         return AVERROR(EINVAL);
939                 }
940             for (i = 0; i < classwords && p < partitions; i++, p++) {
941                 for (j = 0; j < channels; j++) {
942                     int nbook = rc->books[classes[j][p]][pass];
943                     vorbis_enc_codebook * book = &venc->codebooks[nbook];
944                     float *buf = coeffs + samples*j + rc->begin + p*psize;
945                     if (nbook == -1)
946                         continue;
947
948                     assert(rc->type == 0 || rc->type == 2);
949                     assert(!(psize % book->ndimensions));
950
951                     if (rc->type == 0) {
952                         for (k = 0; k < psize; k += book->ndimensions) {
953                             int l;
954                             float *a = put_vector(book, pb, &buf[k]);
955                             if (!a)
956                                 return AVERROR(EINVAL);
957                             for (l = 0; l < book->ndimensions; l++)
958                                 buf[k + l] -= a[l];
959                         }
960                     } else {
961                         int s = rc->begin + p * psize, a1, b1;
962                         a1 = (s % real_ch) * samples;
963                         b1 =  s / real_ch;
964                         s  = real_ch * samples;
965                         for (k = 0; k < psize; k += book->ndimensions) {
966                             int dim, a2 = a1, b2 = b1;
967                             float vec[MAX_CODEBOOK_DIM], *pv = vec;
968                             for (dim = book->ndimensions; dim--; ) {
969                                 *pv++ = coeffs[a2 + b2];
970                                 if ((a2 += samples) == s) {
971                                     a2 = 0;
972                                     b2++;
973                                 }
974                             }
975                             pv = put_vector(book, pb, vec);
976                             if (!pv)
977                                 return AVERROR(EINVAL);
978                             for (dim = book->ndimensions; dim--; ) {
979                                 coeffs[a1 + b1] -= *pv++;
980                                 if ((a1 += samples) == s) {
981                                     a1 = 0;
982                                     b1++;
983                                 }
984                             }
985                         }
986                     }
987                 }
988             }
989         }
990     }
991     return 0;
992 }
993
994 static int apply_window_and_mdct(vorbis_enc_context *venc,
995                                  float **audio, int samples)
996 {
997     int channel;
998     const float * win = venc->win[0];
999     int window_len = 1 << (venc->log2_blocksize[0] - 1);
1000     float n = (float)(1 << venc->log2_blocksize[0]) / 4.0;
1001     AVFloatDSPContext *fdsp = venc->fdsp;
1002
1003     if (!venc->have_saved && !samples)
1004         return 0;
1005
1006     if (venc->have_saved) {
1007         for (channel = 0; channel < venc->channels; channel++)
1008             memcpy(venc->samples + channel * window_len * 2,
1009                    venc->saved + channel * window_len, sizeof(float) * window_len);
1010     } else {
1011         for (channel = 0; channel < venc->channels; channel++)
1012             memset(venc->samples + channel * window_len * 2, 0,
1013                    sizeof(float) * window_len);
1014     }
1015
1016     if (samples) {
1017         for (channel = 0; channel < venc->channels; channel++) {
1018             float *offset = venc->samples + channel * window_len * 2 + window_len;
1019
1020             fdsp->vector_fmul_reverse(offset, audio[channel], win, samples);
1021             fdsp->vector_fmul_scalar(offset, offset, 1/n, samples);
1022         }
1023     } else {
1024         for (channel = 0; channel < venc->channels; channel++)
1025             memset(venc->samples + channel * window_len * 2 + window_len,
1026                    0, sizeof(float) * window_len);
1027     }
1028
1029     for (channel = 0; channel < venc->channels; channel++)
1030         venc->mdct[0].mdct_calc(&venc->mdct[0], venc->coeffs + channel * window_len,
1031                      venc->samples + channel * window_len * 2);
1032
1033     if (samples) {
1034         for (channel = 0; channel < venc->channels; channel++) {
1035             float *offset = venc->saved + channel * window_len;
1036
1037             fdsp->vector_fmul(offset, audio[channel], win, samples);
1038             fdsp->vector_fmul_scalar(offset, offset, 1/n, samples);
1039         }
1040         venc->have_saved = 1;
1041     } else {
1042         venc->have_saved = 0;
1043     }
1044     return 1;
1045 }
1046
1047 /* Used for padding the last encoded packet */
1048 static AVFrame *spawn_empty_frame(AVCodecContext *avctx, int channels)
1049 {
1050     AVFrame *f = av_frame_alloc();
1051
1052     if (!f)
1053         return NULL;
1054
1055     f->format = avctx->sample_fmt;
1056     f->nb_samples = avctx->frame_size;
1057     f->channel_layout = avctx->channel_layout;
1058
1059     if (av_frame_get_buffer(f, 4)) {
1060         av_frame_free(&f);
1061         return NULL;
1062     }
1063
1064     for (int ch = 0; ch < channels; ch++) {
1065         size_t bps = av_get_bytes_per_sample(f->format);
1066         memset(f->extended_data[ch], 0, bps * f->nb_samples);
1067     }
1068     return f;
1069 }
1070
1071 static float **alloc_audio_arrays(int channels, int frame_size)
1072 {
1073     float **audio = av_mallocz_array(channels, sizeof(float *));
1074     if (!audio)
1075         return NULL;
1076
1077     for (int ch = 0; ch < channels; ch++) {
1078         audio[ch] = av_mallocz_array(frame_size, sizeof(float));
1079         if (!audio[ch]) {
1080             // alloc has failed, free everything allocated thus far
1081             for (ch--; ch >= 0; ch--)
1082                 av_free(audio[ch]);
1083             av_free(audio);
1084             return NULL;
1085         }
1086     }
1087
1088     return audio;
1089 }
1090
1091 /* Concatenate audio frames into an appropriately sized array of samples */
1092 static void move_audio(vorbis_enc_context *venc, float **audio, int *samples, int sf_size)
1093 {
1094     AVFrame *cur = NULL;
1095     int frame_size = 1 << (venc->log2_blocksize[1] - 1);
1096     int subframes = frame_size / sf_size;
1097
1098     for (int sf = 0; sf < subframes; sf++) {
1099         cur = ff_bufqueue_get(&venc->bufqueue);
1100         *samples += cur->nb_samples;
1101
1102         for (int ch = 0; ch < venc->channels; ch++) {
1103             const float *input = (float *) cur->extended_data[ch];
1104             const size_t len  = cur->nb_samples * sizeof(float);
1105             memcpy(&audio[ch][sf*sf_size], input, len);
1106         }
1107         av_frame_free(&cur);
1108     }
1109 }
1110
1111 static int vorbis_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
1112                                const AVFrame *frame, int *got_packet_ptr)
1113 {
1114     vorbis_enc_context *venc = avctx->priv_data;
1115     float **audio = NULL;
1116     int i, ret, need_more;
1117     int samples = 0, frame_size = 1 << (venc->log2_blocksize[1] - 1);
1118     vorbis_enc_mode *mode;
1119     vorbis_enc_mapping *mapping;
1120     PutBitContext pb;
1121
1122     if (frame) {
1123         if ((ret = ff_af_queue_add(&venc->afq, frame)) < 0)
1124             return ret;
1125         ff_bufqueue_add(avctx, &venc->bufqueue, av_frame_clone(frame));
1126     } else
1127         if (!venc->afq.remaining_samples)
1128             return 0;
1129
1130     need_more = venc->bufqueue.available * avctx->frame_size < frame_size;
1131     need_more = frame && need_more;
1132     if (need_more)
1133         return 0;
1134
1135     audio = alloc_audio_arrays(venc->channels, frame_size);
1136     if (!audio)
1137         return AVERROR(ENOMEM);
1138
1139     /* Pad the bufqueue with empty frames for encoding the last packet. */
1140     if (!frame) {
1141         if (venc->bufqueue.available * avctx->frame_size < frame_size) {
1142             int frames_needed = (frame_size/avctx->frame_size) - venc->bufqueue.available;
1143
1144             for (int i = 0; i < frames_needed; i++) {
1145                AVFrame *empty = spawn_empty_frame(avctx, venc->channels);
1146                if (!empty)
1147                    return AVERROR(ENOMEM);
1148
1149                ff_bufqueue_add(avctx, &venc->bufqueue, empty);
1150             }
1151         }
1152     }
1153
1154     move_audio(venc, audio, &samples, avctx->frame_size);
1155
1156     if (!apply_window_and_mdct(venc, audio, samples))
1157         return 0;
1158
1159     if ((ret = ff_alloc_packet2(avctx, avpkt, 8192, 0)) < 0)
1160         return ret;
1161
1162     init_put_bits(&pb, avpkt->data, avpkt->size);
1163
1164     if (pb.size_in_bits - put_bits_count(&pb) < 1 + ilog(venc->nmodes - 1)) {
1165         av_log(avctx, AV_LOG_ERROR, "output buffer is too small\n");
1166         return AVERROR(EINVAL);
1167     }
1168
1169     put_bits(&pb, 1, 0); // magic bit
1170
1171     put_bits(&pb, ilog(venc->nmodes - 1), 0); // 0 bits, the mode
1172
1173     mode    = &venc->modes[0];
1174     mapping = &venc->mappings[mode->mapping];
1175     if (mode->blockflag) {
1176         put_bits(&pb, 1, 0);
1177         put_bits(&pb, 1, 0);
1178     }
1179
1180     for (i = 0; i < venc->channels; i++) {
1181         vorbis_enc_floor *fc = &venc->floors[mapping->floor[mapping->mux[i]]];
1182         uint16_t posts[MAX_FLOOR_VALUES];
1183         floor_fit(venc, fc, &venc->coeffs[i * samples], posts, samples);
1184         if (floor_encode(venc, fc, &pb, posts, &venc->floor[i * samples], samples)) {
1185             av_log(avctx, AV_LOG_ERROR, "output buffer is too small\n");
1186             return AVERROR(EINVAL);
1187         }
1188     }
1189
1190     for (i = 0; i < venc->channels * samples; i++)
1191         venc->coeffs[i] /= venc->floor[i];
1192
1193     for (i = 0; i < mapping->coupling_steps; i++) {
1194         float *mag = venc->coeffs + mapping->magnitude[i] * samples;
1195         float *ang = venc->coeffs + mapping->angle[i]     * samples;
1196         int j;
1197         for (j = 0; j < samples; j++) {
1198             float a = ang[j];
1199             ang[j] -= mag[j];
1200             if (mag[j] > 0)
1201                 ang[j] = -ang[j];
1202             if (ang[j] < 0)
1203                 mag[j] = a;
1204         }
1205     }
1206
1207     if (residue_encode(venc, &venc->residues[mapping->residue[mapping->mux[0]]],
1208                        &pb, venc->coeffs, samples, venc->channels)) {
1209         av_log(avctx, AV_LOG_ERROR, "output buffer is too small\n");
1210         return AVERROR(EINVAL);
1211     }
1212
1213     flush_put_bits(&pb);
1214     avpkt->size = put_bits_count(&pb) >> 3;
1215
1216     for (int ch = 0; ch < venc->channels; ch++)
1217         av_free(audio[ch]);
1218     av_free(audio);
1219
1220     ff_af_queue_remove(&venc->afq, frame_size, &avpkt->pts, &avpkt->duration);
1221     *got_packet_ptr = 1;
1222     return 0;
1223 }
1224
1225
1226 static av_cold int vorbis_encode_close(AVCodecContext *avctx)
1227 {
1228     vorbis_enc_context *venc = avctx->priv_data;
1229     int i;
1230
1231     if (venc->codebooks)
1232         for (i = 0; i < venc->ncodebooks; i++) {
1233             av_freep(&venc->codebooks[i].lens);
1234             av_freep(&venc->codebooks[i].codewords);
1235             av_freep(&venc->codebooks[i].quantlist);
1236             av_freep(&venc->codebooks[i].dimensions);
1237             av_freep(&venc->codebooks[i].pow2);
1238         }
1239     av_freep(&venc->codebooks);
1240
1241     if (venc->floors)
1242         for (i = 0; i < venc->nfloors; i++) {
1243             int j;
1244             if (venc->floors[i].classes)
1245                 for (j = 0; j < venc->floors[i].nclasses; j++)
1246                     av_freep(&venc->floors[i].classes[j].books);
1247             av_freep(&venc->floors[i].classes);
1248             av_freep(&venc->floors[i].partition_to_class);
1249             av_freep(&venc->floors[i].list);
1250         }
1251     av_freep(&venc->floors);
1252
1253     if (venc->residues)
1254         for (i = 0; i < venc->nresidues; i++) {
1255             av_freep(&venc->residues[i].books);
1256             av_freep(&venc->residues[i].maxes);
1257         }
1258     av_freep(&venc->residues);
1259
1260     if (venc->mappings)
1261         for (i = 0; i < venc->nmappings; i++) {
1262             av_freep(&venc->mappings[i].mux);
1263             av_freep(&venc->mappings[i].floor);
1264             av_freep(&venc->mappings[i].residue);
1265             av_freep(&venc->mappings[i].magnitude);
1266             av_freep(&venc->mappings[i].angle);
1267         }
1268     av_freep(&venc->mappings);
1269
1270     av_freep(&venc->modes);
1271
1272     av_freep(&venc->saved);
1273     av_freep(&venc->samples);
1274     av_freep(&venc->floor);
1275     av_freep(&venc->coeffs);
1276     av_freep(&venc->fdsp);
1277
1278     ff_mdct_end(&venc->mdct[0]);
1279     ff_mdct_end(&venc->mdct[1]);
1280     ff_af_queue_close(&venc->afq);
1281     ff_bufqueue_discard_all(&venc->bufqueue);
1282
1283     av_freep(&avctx->extradata);
1284
1285     return 0 ;
1286 }
1287
1288 static av_cold int vorbis_encode_init(AVCodecContext *avctx)
1289 {
1290     vorbis_enc_context *venc = avctx->priv_data;
1291     int ret;
1292
1293     if (avctx->channels != 2) {
1294         av_log(avctx, AV_LOG_ERROR, "Current FFmpeg Vorbis encoder only supports 2 channels.\n");
1295         return -1;
1296     }
1297
1298     if ((ret = create_vorbis_context(venc, avctx)) < 0)
1299         goto error;
1300
1301     avctx->bit_rate = 0;
1302     if (avctx->flags & AV_CODEC_FLAG_QSCALE)
1303         venc->quality = avctx->global_quality / (float)FF_QP2LAMBDA;
1304     else
1305         venc->quality = 8;
1306     venc->quality *= venc->quality;
1307
1308     if ((ret = put_main_header(venc, (uint8_t**)&avctx->extradata)) < 0)
1309         goto error;
1310     avctx->extradata_size = ret;
1311
1312     avctx->frame_size = 64;
1313
1314     ff_af_queue_init(avctx, &venc->afq);
1315
1316     return 0;
1317 error:
1318     vorbis_encode_close(avctx);
1319     return ret;
1320 }
1321
1322 AVCodec ff_vorbis_encoder = {
1323     .name           = "vorbis",
1324     .long_name      = NULL_IF_CONFIG_SMALL("Vorbis"),
1325     .type           = AVMEDIA_TYPE_AUDIO,
1326     .id             = AV_CODEC_ID_VORBIS,
1327     .priv_data_size = sizeof(vorbis_enc_context),
1328     .init           = vorbis_encode_init,
1329     .encode2        = vorbis_encode_frame,
1330     .close          = vorbis_encode_close,
1331     .capabilities   = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_EXPERIMENTAL,
1332     .sample_fmts    = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLTP,
1333                                                      AV_SAMPLE_FMT_NONE },
1334 };