avprobe: also output dar/par if only defined in stream
[FFMpeg-mirror/mplayer-patches.git] / libavcodec / vorbisenc.c
blob42713f98cc4e8bc7b6b0f2843dbd1ff5d1836138
1 /*
2 * copyright (c) 2006 Oded Shimon <ods15@ods15.dyndns.org>
4 * This file is part of Libav.
6 * Libav is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * Libav is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Libav; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 /**
22 * @file
23 * Native Vorbis encoder.
24 * @author Oded Shimon <ods15@ods15.dyndns.org>
27 #include <float.h>
28 #include "avcodec.h"
29 #include "dsputil.h"
30 #include "internal.h"
31 #include "fft.h"
32 #include "vorbis.h"
33 #include "vorbis_enc_data.h"
35 #define BITSTREAM_WRITER_LE
36 #include "put_bits.h"
38 #undef NDEBUG
39 #include <assert.h>
41 typedef struct {
42 int nentries;
43 uint8_t *lens;
44 uint32_t *codewords;
45 int ndimensions;
46 float min;
47 float delta;
48 int seq_p;
49 int lookup;
50 int *quantlist;
51 float *dimensions;
52 float *pow2;
53 } vorbis_enc_codebook;
55 typedef struct {
56 int dim;
57 int subclass;
58 int masterbook;
59 int *books;
60 } vorbis_enc_floor_class;
62 typedef struct {
63 int partitions;
64 int *partition_to_class;
65 int nclasses;
66 vorbis_enc_floor_class *classes;
67 int multiplier;
68 int rangebits;
69 int values;
70 vorbis_floor1_entry *list;
71 } vorbis_enc_floor;
73 typedef struct {
74 int type;
75 int begin;
76 int end;
77 int partition_size;
78 int classifications;
79 int classbook;
80 int8_t (*books)[8];
81 float (*maxes)[2];
82 } vorbis_enc_residue;
84 typedef struct {
85 int submaps;
86 int *mux;
87 int *floor;
88 int *residue;
89 int coupling_steps;
90 int *magnitude;
91 int *angle;
92 } vorbis_enc_mapping;
94 typedef struct {
95 int blockflag;
96 int mapping;
97 } vorbis_enc_mode;
99 typedef struct {
100 int channels;
101 int sample_rate;
102 int log2_blocksize[2];
103 FFTContext mdct[2];
104 const float *win[2];
105 int have_saved;
106 float *saved;
107 float *samples;
108 float *floor; // also used for tmp values for mdct
109 float *coeffs; // also used for residue after floor
110 float quality;
112 int ncodebooks;
113 vorbis_enc_codebook *codebooks;
115 int nfloors;
116 vorbis_enc_floor *floors;
118 int nresidues;
119 vorbis_enc_residue *residues;
121 int nmappings;
122 vorbis_enc_mapping *mappings;
124 int nmodes;
125 vorbis_enc_mode *modes;
127 int64_t next_pts;
128 } vorbis_enc_context;
130 #define MAX_CHANNELS 2
131 #define MAX_CODEBOOK_DIM 8
133 #define MAX_FLOOR_CLASS_DIM 4
134 #define NUM_FLOOR_PARTITIONS 8
135 #define MAX_FLOOR_VALUES (MAX_FLOOR_CLASS_DIM*NUM_FLOOR_PARTITIONS+2)
137 #define RESIDUE_SIZE 1600
138 #define RESIDUE_PART_SIZE 32
139 #define NUM_RESIDUE_PARTITIONS (RESIDUE_SIZE/RESIDUE_PART_SIZE)
141 static inline int put_codeword(PutBitContext *pb, vorbis_enc_codebook *cb,
142 int entry)
144 assert(entry >= 0);
145 assert(entry < cb->nentries);
146 assert(cb->lens[entry]);
147 if (pb->size_in_bits - put_bits_count(pb) < cb->lens[entry])
148 return AVERROR(EINVAL);
149 put_bits(pb, cb->lens[entry], cb->codewords[entry]);
150 return 0;
153 static int cb_lookup_vals(int lookup, int dimensions, int entries)
155 if (lookup == 1)
156 return ff_vorbis_nth_root(entries, dimensions);
157 else if (lookup == 2)
158 return dimensions *entries;
159 return 0;
162 static int ready_codebook(vorbis_enc_codebook *cb)
164 int i;
166 ff_vorbis_len2vlc(cb->lens, cb->codewords, cb->nentries);
168 if (!cb->lookup) {
169 cb->pow2 = cb->dimensions = NULL;
170 } else {
171 int vals = cb_lookup_vals(cb->lookup, cb->ndimensions, cb->nentries);
172 cb->dimensions = av_malloc(sizeof(float) * cb->nentries * cb->ndimensions);
173 cb->pow2 = av_mallocz(sizeof(float) * cb->nentries);
174 if (!cb->dimensions || !cb->pow2)
175 return AVERROR(ENOMEM);
176 for (i = 0; i < cb->nentries; i++) {
177 float last = 0;
178 int j;
179 int div = 1;
180 for (j = 0; j < cb->ndimensions; j++) {
181 int off;
182 if (cb->lookup == 1)
183 off = (i / div) % vals; // lookup type 1
184 else
185 off = i * cb->ndimensions + j; // lookup type 2
187 cb->dimensions[i * cb->ndimensions + j] = last + cb->min + cb->quantlist[off] * cb->delta;
188 if (cb->seq_p)
189 last = cb->dimensions[i * cb->ndimensions + j];
190 cb->pow2[i] += cb->dimensions[i * cb->ndimensions + j] * cb->dimensions[i * cb->ndimensions + j];
191 div *= vals;
193 cb->pow2[i] /= 2.;
196 return 0;
199 static int ready_residue(vorbis_enc_residue *rc, vorbis_enc_context *venc)
201 int i;
202 assert(rc->type == 2);
203 rc->maxes = av_mallocz(sizeof(float[2]) * rc->classifications);
204 if (!rc->maxes)
205 return AVERROR(ENOMEM);
206 for (i = 0; i < rc->classifications; i++) {
207 int j;
208 vorbis_enc_codebook * cb;
209 for (j = 0; j < 8; j++)
210 if (rc->books[i][j] != -1)
211 break;
212 if (j == 8) // zero
213 continue;
214 cb = &venc->codebooks[rc->books[i][j]];
215 assert(cb->ndimensions >= 2);
216 assert(cb->lookup);
218 for (j = 0; j < cb->nentries; j++) {
219 float a;
220 if (!cb->lens[j])
221 continue;
222 a = fabs(cb->dimensions[j * cb->ndimensions]);
223 if (a > rc->maxes[i][0])
224 rc->maxes[i][0] = a;
225 a = fabs(cb->dimensions[j * cb->ndimensions + 1]);
226 if (a > rc->maxes[i][1])
227 rc->maxes[i][1] = a;
230 // small bias
231 for (i = 0; i < rc->classifications; i++) {
232 rc->maxes[i][0] += 0.8;
233 rc->maxes[i][1] += 0.8;
235 return 0;
238 static int create_vorbis_context(vorbis_enc_context *venc,
239 AVCodecContext *avccontext)
241 vorbis_enc_floor *fc;
242 vorbis_enc_residue *rc;
243 vorbis_enc_mapping *mc;
244 int i, book, ret;
246 venc->channels = avccontext->channels;
247 venc->sample_rate = avccontext->sample_rate;
248 venc->log2_blocksize[0] = venc->log2_blocksize[1] = 11;
250 venc->ncodebooks = FF_ARRAY_ELEMS(cvectors);
251 venc->codebooks = av_malloc(sizeof(vorbis_enc_codebook) * venc->ncodebooks);
252 if (!venc->codebooks)
253 return AVERROR(ENOMEM);
255 // codebook 0..14 - floor1 book, values 0..255
256 // codebook 15 residue masterbook
257 // codebook 16..29 residue
258 for (book = 0; book < venc->ncodebooks; book++) {
259 vorbis_enc_codebook *cb = &venc->codebooks[book];
260 int vals;
261 cb->ndimensions = cvectors[book].dim;
262 cb->nentries = cvectors[book].real_len;
263 cb->min = cvectors[book].min;
264 cb->delta = cvectors[book].delta;
265 cb->lookup = cvectors[book].lookup;
266 cb->seq_p = 0;
268 cb->lens = av_malloc(sizeof(uint8_t) * cb->nentries);
269 cb->codewords = av_malloc(sizeof(uint32_t) * cb->nentries);
270 if (!cb->lens || !cb->codewords)
271 return AVERROR(ENOMEM);
272 memcpy(cb->lens, cvectors[book].clens, cvectors[book].len);
273 memset(cb->lens + cvectors[book].len, 0, cb->nentries - cvectors[book].len);
275 if (cb->lookup) {
276 vals = cb_lookup_vals(cb->lookup, cb->ndimensions, cb->nentries);
277 cb->quantlist = av_malloc(sizeof(int) * vals);
278 if (!cb->quantlist)
279 return AVERROR(ENOMEM);
280 for (i = 0; i < vals; i++)
281 cb->quantlist[i] = cvectors[book].quant[i];
282 } else {
283 cb->quantlist = NULL;
285 if ((ret = ready_codebook(cb)) < 0)
286 return ret;
289 venc->nfloors = 1;
290 venc->floors = av_malloc(sizeof(vorbis_enc_floor) * venc->nfloors);
291 if (!venc->floors)
292 return AVERROR(ENOMEM);
294 // just 1 floor
295 fc = &venc->floors[0];
296 fc->partitions = NUM_FLOOR_PARTITIONS;
297 fc->partition_to_class = av_malloc(sizeof(int) * fc->partitions);
298 if (!fc->partition_to_class)
299 return AVERROR(ENOMEM);
300 fc->nclasses = 0;
301 for (i = 0; i < fc->partitions; i++) {
302 static const int a[] = {0, 1, 2, 2, 3, 3, 4, 4};
303 fc->partition_to_class[i] = a[i];
304 fc->nclasses = FFMAX(fc->nclasses, fc->partition_to_class[i]);
306 fc->nclasses++;
307 fc->classes = av_malloc(sizeof(vorbis_enc_floor_class) * fc->nclasses);
308 if (!fc->classes)
309 return AVERROR(ENOMEM);
310 for (i = 0; i < fc->nclasses; i++) {
311 vorbis_enc_floor_class * c = &fc->classes[i];
312 int j, books;
313 c->dim = floor_classes[i].dim;
314 c->subclass = floor_classes[i].subclass;
315 c->masterbook = floor_classes[i].masterbook;
316 books = (1 << c->subclass);
317 c->books = av_malloc(sizeof(int) * books);
318 if (!c->books)
319 return AVERROR(ENOMEM);
320 for (j = 0; j < books; j++)
321 c->books[j] = floor_classes[i].nbooks[j];
323 fc->multiplier = 2;
324 fc->rangebits = venc->log2_blocksize[0] - 1;
326 fc->values = 2;
327 for (i = 0; i < fc->partitions; i++)
328 fc->values += fc->classes[fc->partition_to_class[i]].dim;
330 fc->list = av_malloc(sizeof(vorbis_floor1_entry) * fc->values);
331 if (!fc->list)
332 return AVERROR(ENOMEM);
333 fc->list[0].x = 0;
334 fc->list[1].x = 1 << fc->rangebits;
335 for (i = 2; i < fc->values; i++) {
336 static const int a[] = {
337 93, 23,372, 6, 46,186,750, 14, 33, 65,
338 130,260,556, 3, 10, 18, 28, 39, 55, 79,
339 111,158,220,312,464,650,850
341 fc->list[i].x = a[i - 2];
343 if (ff_vorbis_ready_floor1_list(avccontext, fc->list, fc->values))
344 return AVERROR_BUG;
346 venc->nresidues = 1;
347 venc->residues = av_malloc(sizeof(vorbis_enc_residue) * venc->nresidues);
348 if (!venc->residues)
349 return AVERROR(ENOMEM);
351 // single residue
352 rc = &venc->residues[0];
353 rc->type = 2;
354 rc->begin = 0;
355 rc->end = 1600;
356 rc->partition_size = 32;
357 rc->classifications = 10;
358 rc->classbook = 15;
359 rc->books = av_malloc(sizeof(*rc->books) * rc->classifications);
360 if (!rc->books)
361 return AVERROR(ENOMEM);
363 static const int8_t a[10][8] = {
364 { -1, -1, -1, -1, -1, -1, -1, -1, },
365 { -1, -1, 16, -1, -1, -1, -1, -1, },
366 { -1, -1, 17, -1, -1, -1, -1, -1, },
367 { -1, -1, 18, -1, -1, -1, -1, -1, },
368 { -1, -1, 19, -1, -1, -1, -1, -1, },
369 { -1, -1, 20, -1, -1, -1, -1, -1, },
370 { -1, -1, 21, -1, -1, -1, -1, -1, },
371 { 22, 23, -1, -1, -1, -1, -1, -1, },
372 { 24, 25, -1, -1, -1, -1, -1, -1, },
373 { 26, 27, 28, -1, -1, -1, -1, -1, },
375 memcpy(rc->books, a, sizeof a);
377 if ((ret = ready_residue(rc, venc)) < 0)
378 return ret;
380 venc->nmappings = 1;
381 venc->mappings = av_malloc(sizeof(vorbis_enc_mapping) * venc->nmappings);
382 if (!venc->mappings)
383 return AVERROR(ENOMEM);
385 // single mapping
386 mc = &venc->mappings[0];
387 mc->submaps = 1;
388 mc->mux = av_malloc(sizeof(int) * venc->channels);
389 if (!mc->mux)
390 return AVERROR(ENOMEM);
391 for (i = 0; i < venc->channels; i++)
392 mc->mux[i] = 0;
393 mc->floor = av_malloc(sizeof(int) * mc->submaps);
394 mc->residue = av_malloc(sizeof(int) * mc->submaps);
395 if (!mc->floor || !mc->residue)
396 return AVERROR(ENOMEM);
397 for (i = 0; i < mc->submaps; i++) {
398 mc->floor[i] = 0;
399 mc->residue[i] = 0;
401 mc->coupling_steps = venc->channels == 2 ? 1 : 0;
402 mc->magnitude = av_malloc(sizeof(int) * mc->coupling_steps);
403 mc->angle = av_malloc(sizeof(int) * mc->coupling_steps);
404 if (!mc->magnitude || !mc->angle)
405 return AVERROR(ENOMEM);
406 if (mc->coupling_steps) {
407 mc->magnitude[0] = 0;
408 mc->angle[0] = 1;
411 venc->nmodes = 1;
412 venc->modes = av_malloc(sizeof(vorbis_enc_mode) * venc->nmodes);
413 if (!venc->modes)
414 return AVERROR(ENOMEM);
416 // single mode
417 venc->modes[0].blockflag = 0;
418 venc->modes[0].mapping = 0;
420 venc->have_saved = 0;
421 venc->saved = av_malloc(sizeof(float) * venc->channels * (1 << venc->log2_blocksize[1]) / 2);
422 venc->samples = av_malloc(sizeof(float) * venc->channels * (1 << venc->log2_blocksize[1]));
423 venc->floor = av_malloc(sizeof(float) * venc->channels * (1 << venc->log2_blocksize[1]) / 2);
424 venc->coeffs = av_malloc(sizeof(float) * venc->channels * (1 << venc->log2_blocksize[1]) / 2);
425 if (!venc->saved || !venc->samples || !venc->floor || !venc->coeffs)
426 return AVERROR(ENOMEM);
428 venc->win[0] = ff_vorbis_vwin[venc->log2_blocksize[0] - 6];
429 venc->win[1] = ff_vorbis_vwin[venc->log2_blocksize[1] - 6];
431 if ((ret = ff_mdct_init(&venc->mdct[0], venc->log2_blocksize[0], 0, 1.0)) < 0)
432 return ret;
433 if ((ret = ff_mdct_init(&venc->mdct[1], venc->log2_blocksize[1], 0, 1.0)) < 0)
434 return ret;
436 return 0;
439 static void put_float(PutBitContext *pb, float f)
441 int exp, mant;
442 uint32_t res = 0;
443 mant = (int)ldexp(frexp(f, &exp), 20);
444 exp += 788 - 20;
445 if (mant < 0) {
446 res |= (1U << 31);
447 mant = -mant;
449 res |= mant | (exp << 21);
450 put_bits32(pb, res);
453 static void put_codebook_header(PutBitContext *pb, vorbis_enc_codebook *cb)
455 int i;
456 int ordered = 0;
458 put_bits(pb, 24, 0x564342); //magic
459 put_bits(pb, 16, cb->ndimensions);
460 put_bits(pb, 24, cb->nentries);
462 for (i = 1; i < cb->nentries; i++)
463 if (cb->lens[i] < cb->lens[i-1])
464 break;
465 if (i == cb->nentries)
466 ordered = 1;
468 put_bits(pb, 1, ordered);
469 if (ordered) {
470 int len = cb->lens[0];
471 put_bits(pb, 5, len - 1);
472 i = 0;
473 while (i < cb->nentries) {
474 int j;
475 for (j = 0; j+i < cb->nentries; j++)
476 if (cb->lens[j+i] != len)
477 break;
478 put_bits(pb, ilog(cb->nentries - i), j);
479 i += j;
480 len++;
482 } else {
483 int sparse = 0;
484 for (i = 0; i < cb->nentries; i++)
485 if (!cb->lens[i])
486 break;
487 if (i != cb->nentries)
488 sparse = 1;
489 put_bits(pb, 1, sparse);
491 for (i = 0; i < cb->nentries; i++) {
492 if (sparse)
493 put_bits(pb, 1, !!cb->lens[i]);
494 if (cb->lens[i])
495 put_bits(pb, 5, cb->lens[i] - 1);
499 put_bits(pb, 4, cb->lookup);
500 if (cb->lookup) {
501 int tmp = cb_lookup_vals(cb->lookup, cb->ndimensions, cb->nentries);
502 int bits = ilog(cb->quantlist[0]);
504 for (i = 1; i < tmp; i++)
505 bits = FFMAX(bits, ilog(cb->quantlist[i]));
507 put_float(pb, cb->min);
508 put_float(pb, cb->delta);
510 put_bits(pb, 4, bits - 1);
511 put_bits(pb, 1, cb->seq_p);
513 for (i = 0; i < tmp; i++)
514 put_bits(pb, bits, cb->quantlist[i]);
518 static void put_floor_header(PutBitContext *pb, vorbis_enc_floor *fc)
520 int i;
522 put_bits(pb, 16, 1); // type, only floor1 is supported
524 put_bits(pb, 5, fc->partitions);
526 for (i = 0; i < fc->partitions; i++)
527 put_bits(pb, 4, fc->partition_to_class[i]);
529 for (i = 0; i < fc->nclasses; i++) {
530 int j, books;
532 put_bits(pb, 3, fc->classes[i].dim - 1);
533 put_bits(pb, 2, fc->classes[i].subclass);
535 if (fc->classes[i].subclass)
536 put_bits(pb, 8, fc->classes[i].masterbook);
538 books = (1 << fc->classes[i].subclass);
540 for (j = 0; j < books; j++)
541 put_bits(pb, 8, fc->classes[i].books[j] + 1);
544 put_bits(pb, 2, fc->multiplier - 1);
545 put_bits(pb, 4, fc->rangebits);
547 for (i = 2; i < fc->values; i++)
548 put_bits(pb, fc->rangebits, fc->list[i].x);
551 static void put_residue_header(PutBitContext *pb, vorbis_enc_residue *rc)
553 int i;
555 put_bits(pb, 16, rc->type);
557 put_bits(pb, 24, rc->begin);
558 put_bits(pb, 24, rc->end);
559 put_bits(pb, 24, rc->partition_size - 1);
560 put_bits(pb, 6, rc->classifications - 1);
561 put_bits(pb, 8, rc->classbook);
563 for (i = 0; i < rc->classifications; i++) {
564 int j, tmp = 0;
565 for (j = 0; j < 8; j++)
566 tmp |= (rc->books[i][j] != -1) << j;
568 put_bits(pb, 3, tmp & 7);
569 put_bits(pb, 1, tmp > 7);
571 if (tmp > 7)
572 put_bits(pb, 5, tmp >> 3);
575 for (i = 0; i < rc->classifications; i++) {
576 int j;
577 for (j = 0; j < 8; j++)
578 if (rc->books[i][j] != -1)
579 put_bits(pb, 8, rc->books[i][j]);
583 static int put_main_header(vorbis_enc_context *venc, uint8_t **out)
585 int i;
586 PutBitContext pb;
587 uint8_t buffer[50000] = {0}, *p = buffer;
588 int buffer_len = sizeof buffer;
589 int len, hlens[3];
591 // identification header
592 init_put_bits(&pb, p, buffer_len);
593 put_bits(&pb, 8, 1); //magic
594 for (i = 0; "vorbis"[i]; i++)
595 put_bits(&pb, 8, "vorbis"[i]);
596 put_bits32(&pb, 0); // version
597 put_bits(&pb, 8, venc->channels);
598 put_bits32(&pb, venc->sample_rate);
599 put_bits32(&pb, 0); // bitrate
600 put_bits32(&pb, 0); // bitrate
601 put_bits32(&pb, 0); // bitrate
602 put_bits(&pb, 4, venc->log2_blocksize[0]);
603 put_bits(&pb, 4, venc->log2_blocksize[1]);
604 put_bits(&pb, 1, 1); // framing
606 flush_put_bits(&pb);
607 hlens[0] = put_bits_count(&pb) >> 3;
608 buffer_len -= hlens[0];
609 p += hlens[0];
611 // comment header
612 init_put_bits(&pb, p, buffer_len);
613 put_bits(&pb, 8, 3); //magic
614 for (i = 0; "vorbis"[i]; i++)
615 put_bits(&pb, 8, "vorbis"[i]);
616 put_bits32(&pb, 0); // vendor length TODO
617 put_bits32(&pb, 0); // amount of comments
618 put_bits(&pb, 1, 1); // framing
620 flush_put_bits(&pb);
621 hlens[1] = put_bits_count(&pb) >> 3;
622 buffer_len -= hlens[1];
623 p += hlens[1];
625 // setup header
626 init_put_bits(&pb, p, buffer_len);
627 put_bits(&pb, 8, 5); //magic
628 for (i = 0; "vorbis"[i]; i++)
629 put_bits(&pb, 8, "vorbis"[i]);
631 // codebooks
632 put_bits(&pb, 8, venc->ncodebooks - 1);
633 for (i = 0; i < venc->ncodebooks; i++)
634 put_codebook_header(&pb, &venc->codebooks[i]);
636 // time domain, reserved, zero
637 put_bits(&pb, 6, 0);
638 put_bits(&pb, 16, 0);
640 // floors
641 put_bits(&pb, 6, venc->nfloors - 1);
642 for (i = 0; i < venc->nfloors; i++)
643 put_floor_header(&pb, &venc->floors[i]);
645 // residues
646 put_bits(&pb, 6, venc->nresidues - 1);
647 for (i = 0; i < venc->nresidues; i++)
648 put_residue_header(&pb, &venc->residues[i]);
650 // mappings
651 put_bits(&pb, 6, venc->nmappings - 1);
652 for (i = 0; i < venc->nmappings; i++) {
653 vorbis_enc_mapping *mc = &venc->mappings[i];
654 int j;
655 put_bits(&pb, 16, 0); // mapping type
657 put_bits(&pb, 1, mc->submaps > 1);
658 if (mc->submaps > 1)
659 put_bits(&pb, 4, mc->submaps - 1);
661 put_bits(&pb, 1, !!mc->coupling_steps);
662 if (mc->coupling_steps) {
663 put_bits(&pb, 8, mc->coupling_steps - 1);
664 for (j = 0; j < mc->coupling_steps; j++) {
665 put_bits(&pb, ilog(venc->channels - 1), mc->magnitude[j]);
666 put_bits(&pb, ilog(venc->channels - 1), mc->angle[j]);
670 put_bits(&pb, 2, 0); // reserved
672 if (mc->submaps > 1)
673 for (j = 0; j < venc->channels; j++)
674 put_bits(&pb, 4, mc->mux[j]);
676 for (j = 0; j < mc->submaps; j++) {
677 put_bits(&pb, 8, 0); // reserved time configuration
678 put_bits(&pb, 8, mc->floor[j]);
679 put_bits(&pb, 8, mc->residue[j]);
683 // modes
684 put_bits(&pb, 6, venc->nmodes - 1);
685 for (i = 0; i < venc->nmodes; i++) {
686 put_bits(&pb, 1, venc->modes[i].blockflag);
687 put_bits(&pb, 16, 0); // reserved window type
688 put_bits(&pb, 16, 0); // reserved transform type
689 put_bits(&pb, 8, venc->modes[i].mapping);
692 put_bits(&pb, 1, 1); // framing
694 flush_put_bits(&pb);
695 hlens[2] = put_bits_count(&pb) >> 3;
697 len = hlens[0] + hlens[1] + hlens[2];
698 p = *out = av_mallocz(64 + len + len/255);
699 if (!p)
700 return AVERROR(ENOMEM);
702 *p++ = 2;
703 p += av_xiphlacing(p, hlens[0]);
704 p += av_xiphlacing(p, hlens[1]);
705 buffer_len = 0;
706 for (i = 0; i < 3; i++) {
707 memcpy(p, buffer + buffer_len, hlens[i]);
708 p += hlens[i];
709 buffer_len += hlens[i];
712 return p - *out;
715 static float get_floor_average(vorbis_enc_floor * fc, float *coeffs, int i)
717 int begin = fc->list[fc->list[FFMAX(i-1, 0)].sort].x;
718 int end = fc->list[fc->list[FFMIN(i+1, fc->values - 1)].sort].x;
719 int j;
720 float average = 0;
722 for (j = begin; j < end; j++)
723 average += fabs(coeffs[j]);
724 return average / (end - begin);
727 static void floor_fit(vorbis_enc_context *venc, vorbis_enc_floor *fc,
728 float *coeffs, uint16_t *posts, int samples)
730 int range = 255 / fc->multiplier + 1;
731 int i;
732 float tot_average = 0.;
733 float averages[MAX_FLOOR_VALUES];
734 for (i = 0; i < fc->values; i++) {
735 averages[i] = get_floor_average(fc, coeffs, i);
736 tot_average += averages[i];
738 tot_average /= fc->values;
739 tot_average /= venc->quality;
741 for (i = 0; i < fc->values; i++) {
742 int position = fc->list[fc->list[i].sort].x;
743 float average = averages[i];
744 int j;
746 average = sqrt(tot_average * average) * pow(1.25f, position*0.005f); // MAGIC!
747 for (j = 0; j < range - 1; j++)
748 if (ff_vorbis_floor1_inverse_db_table[j * fc->multiplier] > average)
749 break;
750 posts[fc->list[i].sort] = j;
754 static int render_point(int x0, int y0, int x1, int y1, int x)
756 return y0 + (x - x0) * (y1 - y0) / (x1 - x0);
759 static int floor_encode(vorbis_enc_context *venc, vorbis_enc_floor *fc,
760 PutBitContext *pb, uint16_t *posts,
761 float *floor, int samples)
763 int range = 255 / fc->multiplier + 1;
764 int coded[MAX_FLOOR_VALUES]; // first 2 values are unused
765 int i, counter;
767 if (pb->size_in_bits - put_bits_count(pb) < 1 + 2 * ilog(range - 1))
768 return AVERROR(EINVAL);
769 put_bits(pb, 1, 1); // non zero
770 put_bits(pb, ilog(range - 1), posts[0]);
771 put_bits(pb, ilog(range - 1), posts[1]);
772 coded[0] = coded[1] = 1;
774 for (i = 2; i < fc->values; i++) {
775 int predicted = render_point(fc->list[fc->list[i].low].x,
776 posts[fc->list[i].low],
777 fc->list[fc->list[i].high].x,
778 posts[fc->list[i].high],
779 fc->list[i].x);
780 int highroom = range - predicted;
781 int lowroom = predicted;
782 int room = FFMIN(highroom, lowroom);
783 if (predicted == posts[i]) {
784 coded[i] = 0; // must be used later as flag!
785 continue;
786 } else {
787 if (!coded[fc->list[i].low ])
788 coded[fc->list[i].low ] = -1;
789 if (!coded[fc->list[i].high])
790 coded[fc->list[i].high] = -1;
792 if (posts[i] > predicted) {
793 if (posts[i] - predicted > room)
794 coded[i] = posts[i] - predicted + lowroom;
795 else
796 coded[i] = (posts[i] - predicted) << 1;
797 } else {
798 if (predicted - posts[i] > room)
799 coded[i] = predicted - posts[i] + highroom - 1;
800 else
801 coded[i] = ((predicted - posts[i]) << 1) - 1;
805 counter = 2;
806 for (i = 0; i < fc->partitions; i++) {
807 vorbis_enc_floor_class * c = &fc->classes[fc->partition_to_class[i]];
808 int k, cval = 0, csub = 1<<c->subclass;
809 if (c->subclass) {
810 vorbis_enc_codebook * book = &venc->codebooks[c->masterbook];
811 int cshift = 0;
812 for (k = 0; k < c->dim; k++) {
813 int l;
814 for (l = 0; l < csub; l++) {
815 int maxval = 1;
816 if (c->books[l] != -1)
817 maxval = venc->codebooks[c->books[l]].nentries;
818 // coded could be -1, but this still works, cause that is 0
819 if (coded[counter + k] < maxval)
820 break;
822 assert(l != csub);
823 cval |= l << cshift;
824 cshift += c->subclass;
826 if (put_codeword(pb, book, cval))
827 return AVERROR(EINVAL);
829 for (k = 0; k < c->dim; k++) {
830 int book = c->books[cval & (csub-1)];
831 int entry = coded[counter++];
832 cval >>= c->subclass;
833 if (book == -1)
834 continue;
835 if (entry == -1)
836 entry = 0;
837 if (put_codeword(pb, &venc->codebooks[book], entry))
838 return AVERROR(EINVAL);
842 ff_vorbis_floor1_render_list(fc->list, fc->values, posts, coded,
843 fc->multiplier, floor, samples);
845 return 0;
848 static float *put_vector(vorbis_enc_codebook *book, PutBitContext *pb,
849 float *num)
851 int i, entry = -1;
852 float distance = FLT_MAX;
853 assert(book->dimensions);
854 for (i = 0; i < book->nentries; i++) {
855 float * vec = book->dimensions + i * book->ndimensions, d = book->pow2[i];
856 int j;
857 if (!book->lens[i])
858 continue;
859 for (j = 0; j < book->ndimensions; j++)
860 d -= vec[j] * num[j];
861 if (distance > d) {
862 entry = i;
863 distance = d;
866 if (put_codeword(pb, book, entry))
867 return NULL;
868 return &book->dimensions[entry * book->ndimensions];
871 static int residue_encode(vorbis_enc_context *venc, vorbis_enc_residue *rc,
872 PutBitContext *pb, float *coeffs, int samples,
873 int real_ch)
875 int pass, i, j, p, k;
876 int psize = rc->partition_size;
877 int partitions = (rc->end - rc->begin) / psize;
878 int channels = (rc->type == 2) ? 1 : real_ch;
879 int classes[MAX_CHANNELS][NUM_RESIDUE_PARTITIONS];
880 int classwords = venc->codebooks[rc->classbook].ndimensions;
882 assert(rc->type == 2);
883 assert(real_ch == 2);
884 for (p = 0; p < partitions; p++) {
885 float max1 = 0., max2 = 0.;
886 int s = rc->begin + p * psize;
887 for (k = s; k < s + psize; k += 2) {
888 max1 = FFMAX(max1, fabs(coeffs[ k / real_ch]));
889 max2 = FFMAX(max2, fabs(coeffs[samples + k / real_ch]));
892 for (i = 0; i < rc->classifications - 1; i++)
893 if (max1 < rc->maxes[i][0] && max2 < rc->maxes[i][1])
894 break;
895 classes[0][p] = i;
898 for (pass = 0; pass < 8; pass++) {
899 p = 0;
900 while (p < partitions) {
901 if (pass == 0)
902 for (j = 0; j < channels; j++) {
903 vorbis_enc_codebook * book = &venc->codebooks[rc->classbook];
904 int entry = 0;
905 for (i = 0; i < classwords; i++) {
906 entry *= rc->classifications;
907 entry += classes[j][p + i];
909 if (put_codeword(pb, book, entry))
910 return AVERROR(EINVAL);
912 for (i = 0; i < classwords && p < partitions; i++, p++) {
913 for (j = 0; j < channels; j++) {
914 int nbook = rc->books[classes[j][p]][pass];
915 vorbis_enc_codebook * book = &venc->codebooks[nbook];
916 float *buf = coeffs + samples*j + rc->begin + p*psize;
917 if (nbook == -1)
918 continue;
920 assert(rc->type == 0 || rc->type == 2);
921 assert(!(psize % book->ndimensions));
923 if (rc->type == 0) {
924 for (k = 0; k < psize; k += book->ndimensions) {
925 int l;
926 float *a = put_vector(book, pb, &buf[k]);
927 if (!a)
928 return AVERROR(EINVAL);
929 for (l = 0; l < book->ndimensions; l++)
930 buf[k + l] -= a[l];
932 } else {
933 int s = rc->begin + p * psize, a1, b1;
934 a1 = (s % real_ch) * samples;
935 b1 = s / real_ch;
936 s = real_ch * samples;
937 for (k = 0; k < psize; k += book->ndimensions) {
938 int dim, a2 = a1, b2 = b1;
939 float vec[MAX_CODEBOOK_DIM], *pv = vec;
940 for (dim = book->ndimensions; dim--; ) {
941 *pv++ = coeffs[a2 + b2];
942 if ((a2 += samples) == s) {
943 a2 = 0;
944 b2++;
947 pv = put_vector(book, pb, vec);
948 if (!pv)
949 return AVERROR(EINVAL);
950 for (dim = book->ndimensions; dim--; ) {
951 coeffs[a1 + b1] -= *pv++;
952 if ((a1 += samples) == s) {
953 a1 = 0;
954 b1++;
963 return 0;
966 static int apply_window_and_mdct(vorbis_enc_context *venc,
967 float **audio, int samples)
969 int i, channel;
970 const float * win = venc->win[0];
971 int window_len = 1 << (venc->log2_blocksize[0] - 1);
972 float n = (float)(1 << venc->log2_blocksize[0]) / 4.;
973 // FIXME use dsp
975 if (!venc->have_saved && !samples)
976 return 0;
978 if (venc->have_saved) {
979 for (channel = 0; channel < venc->channels; channel++)
980 memcpy(venc->samples + channel * window_len * 2,
981 venc->saved + channel * window_len, sizeof(float) * window_len);
982 } else {
983 for (channel = 0; channel < venc->channels; channel++)
984 memset(venc->samples + channel * window_len * 2, 0,
985 sizeof(float) * window_len);
988 if (samples) {
989 for (channel = 0; channel < venc->channels; channel++) {
990 float * offset = venc->samples + channel*window_len*2 + window_len;
991 for (i = 0; i < samples; i++)
992 offset[i] = audio[channel][i] / n * win[window_len - i - 1];
994 } else {
995 for (channel = 0; channel < venc->channels; channel++)
996 memset(venc->samples + channel * window_len * 2 + window_len,
997 0, sizeof(float) * window_len);
1000 for (channel = 0; channel < venc->channels; channel++)
1001 venc->mdct[0].mdct_calc(&venc->mdct[0], venc->coeffs + channel * window_len,
1002 venc->samples + channel * window_len * 2);
1004 if (samples) {
1005 for (channel = 0; channel < venc->channels; channel++) {
1006 float *offset = venc->saved + channel * window_len;
1007 for (i = 0; i < samples; i++)
1008 offset[i] = audio[channel][i] / n * win[i];
1010 venc->have_saved = 1;
1011 } else {
1012 venc->have_saved = 0;
1014 return 1;
1018 static int vorbis_encode_frame(AVCodecContext *avccontext, AVPacket *avpkt,
1019 const AVFrame *frame, int *got_packet_ptr)
1021 vorbis_enc_context *venc = avccontext->priv_data;
1022 float **audio = frame ? (float **)frame->extended_data : NULL;
1023 int samples = frame ? frame->nb_samples : 0;
1024 vorbis_enc_mode *mode;
1025 vorbis_enc_mapping *mapping;
1026 PutBitContext pb;
1027 int i, ret;
1029 if (!apply_window_and_mdct(venc, audio, samples))
1030 return 0;
1031 samples = 1 << (venc->log2_blocksize[0] - 1);
1033 if ((ret = ff_alloc_packet(avpkt, 8192))) {
1034 av_log(avccontext, AV_LOG_ERROR, "Error getting output packet\n");
1035 return ret;
1038 init_put_bits(&pb, avpkt->data, avpkt->size);
1040 if (pb.size_in_bits - put_bits_count(&pb) < 1 + ilog(venc->nmodes - 1)) {
1041 av_log(avccontext, AV_LOG_ERROR, "output buffer is too small\n");
1042 return AVERROR(EINVAL);
1045 put_bits(&pb, 1, 0); // magic bit
1047 put_bits(&pb, ilog(venc->nmodes - 1), 0); // 0 bits, the mode
1049 mode = &venc->modes[0];
1050 mapping = &venc->mappings[mode->mapping];
1051 if (mode->blockflag) {
1052 put_bits(&pb, 1, 0);
1053 put_bits(&pb, 1, 0);
1056 for (i = 0; i < venc->channels; i++) {
1057 vorbis_enc_floor *fc = &venc->floors[mapping->floor[mapping->mux[i]]];
1058 uint16_t posts[MAX_FLOOR_VALUES];
1059 floor_fit(venc, fc, &venc->coeffs[i * samples], posts, samples);
1060 if (floor_encode(venc, fc, &pb, posts, &venc->floor[i * samples], samples)) {
1061 av_log(avccontext, AV_LOG_ERROR, "output buffer is too small\n");
1062 return AVERROR(EINVAL);
1066 for (i = 0; i < venc->channels * samples; i++)
1067 venc->coeffs[i] /= venc->floor[i];
1069 for (i = 0; i < mapping->coupling_steps; i++) {
1070 float *mag = venc->coeffs + mapping->magnitude[i] * samples;
1071 float *ang = venc->coeffs + mapping->angle[i] * samples;
1072 int j;
1073 for (j = 0; j < samples; j++) {
1074 float a = ang[j];
1075 ang[j] -= mag[j];
1076 if (mag[j] > 0)
1077 ang[j] = -ang[j];
1078 if (ang[j] < 0)
1079 mag[j] = a;
1083 if (residue_encode(venc, &venc->residues[mapping->residue[mapping->mux[0]]],
1084 &pb, venc->coeffs, samples, venc->channels)) {
1085 av_log(avccontext, AV_LOG_ERROR, "output buffer is too small\n");
1086 return AVERROR(EINVAL);
1089 flush_put_bits(&pb);
1090 avpkt->size = put_bits_count(&pb) >> 3;
1092 avpkt->duration = ff_samples_to_time_base(avccontext, avccontext->frame_size);
1093 if (frame)
1094 if (frame->pts != AV_NOPTS_VALUE)
1095 avpkt->pts = ff_samples_to_time_base(avccontext, frame->pts);
1096 else
1097 avpkt->pts = venc->next_pts;
1098 if (avpkt->pts != AV_NOPTS_VALUE)
1099 venc->next_pts = avpkt->pts + avpkt->duration;
1101 *got_packet_ptr = 1;
1102 return 0;
1106 static av_cold int vorbis_encode_close(AVCodecContext *avccontext)
1108 vorbis_enc_context *venc = avccontext->priv_data;
1109 int i;
1111 if (venc->codebooks)
1112 for (i = 0; i < venc->ncodebooks; i++) {
1113 av_freep(&venc->codebooks[i].lens);
1114 av_freep(&venc->codebooks[i].codewords);
1115 av_freep(&venc->codebooks[i].quantlist);
1116 av_freep(&venc->codebooks[i].dimensions);
1117 av_freep(&venc->codebooks[i].pow2);
1119 av_freep(&venc->codebooks);
1121 if (venc->floors)
1122 for (i = 0; i < venc->nfloors; i++) {
1123 int j;
1124 if (venc->floors[i].classes)
1125 for (j = 0; j < venc->floors[i].nclasses; j++)
1126 av_freep(&venc->floors[i].classes[j].books);
1127 av_freep(&venc->floors[i].classes);
1128 av_freep(&venc->floors[i].partition_to_class);
1129 av_freep(&venc->floors[i].list);
1131 av_freep(&venc->floors);
1133 if (venc->residues)
1134 for (i = 0; i < venc->nresidues; i++) {
1135 av_freep(&venc->residues[i].books);
1136 av_freep(&venc->residues[i].maxes);
1138 av_freep(&venc->residues);
1140 if (venc->mappings)
1141 for (i = 0; i < venc->nmappings; i++) {
1142 av_freep(&venc->mappings[i].mux);
1143 av_freep(&venc->mappings[i].floor);
1144 av_freep(&venc->mappings[i].residue);
1145 av_freep(&venc->mappings[i].magnitude);
1146 av_freep(&venc->mappings[i].angle);
1148 av_freep(&venc->mappings);
1150 av_freep(&venc->modes);
1152 av_freep(&venc->saved);
1153 av_freep(&venc->samples);
1154 av_freep(&venc->floor);
1155 av_freep(&venc->coeffs);
1157 ff_mdct_end(&venc->mdct[0]);
1158 ff_mdct_end(&venc->mdct[1]);
1160 #if FF_API_OLD_ENCODE_AUDIO
1161 av_freep(&avccontext->coded_frame);
1162 #endif
1163 av_freep(&avccontext->extradata);
1165 return 0 ;
1168 static av_cold int vorbis_encode_init(AVCodecContext *avccontext)
1170 vorbis_enc_context *venc = avccontext->priv_data;
1171 int ret;
1173 if (avccontext->channels != 2) {
1174 av_log(avccontext, AV_LOG_ERROR, "Current Libav Vorbis encoder only supports 2 channels.\n");
1175 return -1;
1178 if ((ret = create_vorbis_context(venc, avccontext)) < 0)
1179 goto error;
1181 avccontext->bit_rate = 0;
1182 if (avccontext->flags & CODEC_FLAG_QSCALE)
1183 venc->quality = avccontext->global_quality / (float)FF_QP2LAMBDA;
1184 else
1185 venc->quality = 3.0;
1186 venc->quality *= venc->quality;
1188 if ((ret = put_main_header(venc, (uint8_t**)&avccontext->extradata)) < 0)
1189 goto error;
1190 avccontext->extradata_size = ret;
1192 avccontext->frame_size = 1 << (venc->log2_blocksize[0] - 1);
1194 #if FF_API_OLD_ENCODE_AUDIO
1195 avccontext->coded_frame = avcodec_alloc_frame();
1196 if (!avccontext->coded_frame) {
1197 ret = AVERROR(ENOMEM);
1198 goto error;
1200 #endif
1202 return 0;
1203 error:
1204 vorbis_encode_close(avccontext);
1205 return ret;
1208 AVCodec ff_vorbis_encoder = {
1209 .name = "vorbis",
1210 .type = AVMEDIA_TYPE_AUDIO,
1211 .id = AV_CODEC_ID_VORBIS,
1212 .priv_data_size = sizeof(vorbis_enc_context),
1213 .init = vorbis_encode_init,
1214 .encode2 = vorbis_encode_frame,
1215 .close = vorbis_encode_close,
1216 .capabilities = CODEC_CAP_DELAY | CODEC_CAP_EXPERIMENTAL,
1217 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLTP,
1218 AV_SAMPLE_FMT_NONE },
1219 .long_name = NULL_IF_CONFIG_SMALL("Vorbis"),