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