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
23 * Native Vorbis encoder.
24 * @author Oded Shimon <ods15@ods15.dyndns.org>
32 #include "vorbis_enc_data.h"
34 #define BITSTREAM_WRITER_LE
52 } vorbis_enc_codebook;
59 } vorbis_enc_floor_class;
63 int *partition_to_class;
65 vorbis_enc_floor_class *classes;
69 vorbis_floor1_entry *list;
101 int log2_blocksize[2];
107 float *floor; // also used for tmp values for mdct
108 float *coeffs; // also used for residue after floor
112 vorbis_enc_codebook *codebooks;
115 vorbis_enc_floor *floors;
118 vorbis_enc_residue *residues;
121 vorbis_enc_mapping *mappings;
124 vorbis_enc_mode *modes;
126 int64_t sample_count;
127 } vorbis_enc_context;
129 #define MAX_CHANNELS 2
130 #define MAX_CODEBOOK_DIM 8
132 #define MAX_FLOOR_CLASS_DIM 4
133 #define NUM_FLOOR_PARTITIONS 8
134 #define MAX_FLOOR_VALUES (MAX_FLOOR_CLASS_DIM*NUM_FLOOR_PARTITIONS+2)
136 #define RESIDUE_SIZE 1600
137 #define RESIDUE_PART_SIZE 32
138 #define NUM_RESIDUE_PARTITIONS (RESIDUE_SIZE/RESIDUE_PART_SIZE)
140 static inline int put_codeword(PutBitContext *pb, vorbis_enc_codebook *cb,
144 assert(entry < cb->nentries);
145 assert(cb->lens[entry]);
146 if (pb->size_in_bits - put_bits_count(pb) < cb->lens[entry])
147 return AVERROR(EINVAL);
148 put_bits(pb, cb->lens[entry], cb->codewords[entry]);
152 static int cb_lookup_vals(int lookup, int dimentions, int entries)
155 return ff_vorbis_nth_root(entries, dimentions);
156 else if (lookup == 2)
157 return dimentions *entries;
161 static int ready_codebook(vorbis_enc_codebook *cb)
165 ff_vorbis_len2vlc(cb->lens, cb->codewords, cb->nentries);
168 cb->pow2 = cb->dimentions = NULL;
170 int vals = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries);
171 cb->dimentions = av_malloc(sizeof(float) * cb->nentries * cb->ndimentions);
172 cb->pow2 = av_mallocz(sizeof(float) * cb->nentries);
173 if (!cb->dimentions || !cb->pow2)
174 return AVERROR(ENOMEM);
175 for (i = 0; i < cb->nentries; i++) {
179 for (j = 0; j < cb->ndimentions; j++) {
182 off = (i / div) % vals; // lookup type 1
184 off = i * cb->ndimentions + j; // lookup type 2
186 cb->dimentions[i * cb->ndimentions + j] = last + cb->min + cb->quantlist[off] * cb->delta;
188 last = cb->dimentions[i * cb->ndimentions + j];
189 cb->pow2[i] += cb->dimentions[i * cb->ndimentions + j] * cb->dimentions[i * cb->ndimentions + j];
198 static int ready_residue(vorbis_enc_residue *rc, vorbis_enc_context *venc)
201 assert(rc->type == 2);
202 rc->maxes = av_mallocz(sizeof(float[2]) * rc->classifications);
204 return AVERROR(ENOMEM);
205 for (i = 0; i < rc->classifications; i++) {
207 vorbis_enc_codebook * cb;
208 for (j = 0; j < 8; j++)
209 if (rc->books[i][j] != -1)
213 cb = &venc->codebooks[rc->books[i][j]];
214 assert(cb->ndimentions >= 2);
217 for (j = 0; j < cb->nentries; j++) {
221 a = fabs(cb->dimentions[j * cb->ndimentions]);
222 if (a > rc->maxes[i][0])
224 a = fabs(cb->dimentions[j * cb->ndimentions + 1]);
225 if (a > rc->maxes[i][1])
230 for (i = 0; i < rc->classifications; i++) {
231 rc->maxes[i][0] += 0.8;
232 rc->maxes[i][1] += 0.8;
237 static int create_vorbis_context(vorbis_enc_context *venc,
238 AVCodecContext *avccontext)
240 vorbis_enc_floor *fc;
241 vorbis_enc_residue *rc;
242 vorbis_enc_mapping *mc;
245 venc->channels = avccontext->channels;
246 venc->sample_rate = avccontext->sample_rate;
247 venc->log2_blocksize[0] = venc->log2_blocksize[1] = 11;
249 venc->ncodebooks = FF_ARRAY_ELEMS(cvectors);
250 venc->codebooks = av_malloc(sizeof(vorbis_enc_codebook) * venc->ncodebooks);
251 if (!venc->codebooks)
252 return AVERROR(ENOMEM);
254 // codebook 0..14 - floor1 book, values 0..255
255 // codebook 15 residue masterbook
256 // codebook 16..29 residue
257 for (book = 0; book < venc->ncodebooks; book++) {
258 vorbis_enc_codebook *cb = &venc->codebooks[book];
260 cb->ndimentions = cvectors[book].dim;
261 cb->nentries = cvectors[book].real_len;
262 cb->min = cvectors[book].min;
263 cb->delta = cvectors[book].delta;
264 cb->lookup = cvectors[book].lookup;
267 cb->lens = av_malloc(sizeof(uint8_t) * cb->nentries);
268 cb->codewords = av_malloc(sizeof(uint32_t) * cb->nentries);
269 if (!cb->lens || !cb->codewords)
270 return AVERROR(ENOMEM);
271 memcpy(cb->lens, cvectors[book].clens, cvectors[book].len);
272 memset(cb->lens + cvectors[book].len, 0, cb->nentries - cvectors[book].len);
275 vals = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries);
276 cb->quantlist = av_malloc(sizeof(int) * vals);
278 return AVERROR(ENOMEM);
279 for (i = 0; i < vals; i++)
280 cb->quantlist[i] = cvectors[book].quant[i];
282 cb->quantlist = NULL;
284 if ((ret = ready_codebook(cb)) < 0)
289 venc->floors = av_malloc(sizeof(vorbis_enc_floor) * venc->nfloors);
291 return AVERROR(ENOMEM);
294 fc = &venc->floors[0];
295 fc->partitions = NUM_FLOOR_PARTITIONS;
296 fc->partition_to_class = av_malloc(sizeof(int) * fc->partitions);
297 if (!fc->partition_to_class)
298 return AVERROR(ENOMEM);
300 for (i = 0; i < fc->partitions; i++) {
301 static const int a[] = {0, 1, 2, 2, 3, 3, 4, 4};
302 fc->partition_to_class[i] = a[i];
303 fc->nclasses = FFMAX(fc->nclasses, fc->partition_to_class[i]);
306 fc->classes = av_malloc(sizeof(vorbis_enc_floor_class) * fc->nclasses);
308 return AVERROR(ENOMEM);
309 for (i = 0; i < fc->nclasses; i++) {
310 vorbis_enc_floor_class * c = &fc->classes[i];
312 c->dim = floor_classes[i].dim;
313 c->subclass = floor_classes[i].subclass;
314 c->masterbook = floor_classes[i].masterbook;
315 books = (1 << c->subclass);
316 c->books = av_malloc(sizeof(int) * books);
318 return AVERROR(ENOMEM);
319 for (j = 0; j < books; j++)
320 c->books[j] = floor_classes[i].nbooks[j];
323 fc->rangebits = venc->log2_blocksize[0] - 1;
326 for (i = 0; i < fc->partitions; i++)
327 fc->values += fc->classes[fc->partition_to_class[i]].dim;
329 fc->list = av_malloc(sizeof(vorbis_floor1_entry) * fc->values);
331 return AVERROR(ENOMEM);
333 fc->list[1].x = 1 << fc->rangebits;
334 for (i = 2; i < fc->values; i++) {
335 static const int a[] = {
336 93, 23,372, 6, 46,186,750, 14, 33, 65,
337 130,260,556, 3, 10, 18, 28, 39, 55, 79,
338 111,158,220,312,464,650,850
340 fc->list[i].x = a[i - 2];
342 ff_vorbis_ready_floor1_list(fc->list, fc->values);
345 venc->residues = av_malloc(sizeof(vorbis_enc_residue) * venc->nresidues);
347 return AVERROR(ENOMEM);
350 rc = &venc->residues[0];
354 rc->partition_size = 32;
355 rc->classifications = 10;
357 rc->books = av_malloc(sizeof(*rc->books) * rc->classifications);
359 return AVERROR(ENOMEM);
361 static const int8_t a[10][8] = {
362 { -1, -1, -1, -1, -1, -1, -1, -1, },
363 { -1, -1, 16, -1, -1, -1, -1, -1, },
364 { -1, -1, 17, -1, -1, -1, -1, -1, },
365 { -1, -1, 18, -1, -1, -1, -1, -1, },
366 { -1, -1, 19, -1, -1, -1, -1, -1, },
367 { -1, -1, 20, -1, -1, -1, -1, -1, },
368 { -1, -1, 21, -1, -1, -1, -1, -1, },
369 { 22, 23, -1, -1, -1, -1, -1, -1, },
370 { 24, 25, -1, -1, -1, -1, -1, -1, },
371 { 26, 27, 28, -1, -1, -1, -1, -1, },
373 memcpy(rc->books, a, sizeof a);
375 if ((ret = ready_residue(rc, venc)) < 0)
379 venc->mappings = av_malloc(sizeof(vorbis_enc_mapping) * venc->nmappings);
381 return AVERROR(ENOMEM);
384 mc = &venc->mappings[0];
386 mc->mux = av_malloc(sizeof(int) * venc->channels);
388 return AVERROR(ENOMEM);
389 for (i = 0; i < venc->channels; i++)
391 mc->floor = av_malloc(sizeof(int) * mc->submaps);
392 mc->residue = av_malloc(sizeof(int) * mc->submaps);
393 if (!mc->floor || !mc->residue)
394 return AVERROR(ENOMEM);
395 for (i = 0; i < mc->submaps; i++) {
399 mc->coupling_steps = venc->channels == 2 ? 1 : 0;
400 mc->magnitude = av_malloc(sizeof(int) * mc->coupling_steps);
401 mc->angle = av_malloc(sizeof(int) * mc->coupling_steps);
402 if (!mc->magnitude || !mc->angle)
403 return AVERROR(ENOMEM);
404 if (mc->coupling_steps) {
405 mc->magnitude[0] = 0;
410 venc->modes = av_malloc(sizeof(vorbis_enc_mode) * venc->nmodes);
412 return AVERROR(ENOMEM);
415 venc->modes[0].blockflag = 0;
416 venc->modes[0].mapping = 0;
418 venc->have_saved = 0;
419 venc->saved = av_malloc(sizeof(float) * venc->channels * (1 << venc->log2_blocksize[1]) / 2);
420 venc->samples = av_malloc(sizeof(float) * venc->channels * (1 << venc->log2_blocksize[1]));
421 venc->floor = av_malloc(sizeof(float) * venc->channels * (1 << venc->log2_blocksize[1]) / 2);
422 venc->coeffs = av_malloc(sizeof(float) * venc->channels * (1 << venc->log2_blocksize[1]) / 2);
423 if (!venc->saved || !venc->samples || !venc->floor || !venc->coeffs)
424 return AVERROR(ENOMEM);
426 venc->win[0] = ff_vorbis_vwin[venc->log2_blocksize[0] - 6];
427 venc->win[1] = ff_vorbis_vwin[venc->log2_blocksize[1] - 6];
429 if ((ret = ff_mdct_init(&venc->mdct[0], venc->log2_blocksize[0], 0, 1.0)) < 0)
431 if ((ret = ff_mdct_init(&venc->mdct[1], venc->log2_blocksize[1], 0, 1.0)) < 0)
437 static void put_float(PutBitContext *pb, float f)
441 mant = (int)ldexp(frexp(f, &exp), 20);
447 res |= mant | (exp << 21);
451 static void put_codebook_header(PutBitContext *pb, vorbis_enc_codebook *cb)
456 put_bits(pb, 24, 0x564342); //magic
457 put_bits(pb, 16, cb->ndimentions);
458 put_bits(pb, 24, cb->nentries);
460 for (i = 1; i < cb->nentries; i++)
461 if (cb->lens[i] < cb->lens[i-1])
463 if (i == cb->nentries)
466 put_bits(pb, 1, ordered);
468 int len = cb->lens[0];
469 put_bits(pb, 5, len - 1);
471 while (i < cb->nentries) {
473 for (j = 0; j+i < cb->nentries; j++)
474 if (cb->lens[j+i] != len)
476 put_bits(pb, ilog(cb->nentries - i), j);
482 for (i = 0; i < cb->nentries; i++)
485 if (i != cb->nentries)
487 put_bits(pb, 1, sparse);
489 for (i = 0; i < cb->nentries; i++) {
491 put_bits(pb, 1, !!cb->lens[i]);
493 put_bits(pb, 5, cb->lens[i] - 1);
497 put_bits(pb, 4, cb->lookup);
499 int tmp = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries);
500 int bits = ilog(cb->quantlist[0]);
502 for (i = 1; i < tmp; i++)
503 bits = FFMAX(bits, ilog(cb->quantlist[i]));
505 put_float(pb, cb->min);
506 put_float(pb, cb->delta);
508 put_bits(pb, 4, bits - 1);
509 put_bits(pb, 1, cb->seq_p);
511 for (i = 0; i < tmp; i++)
512 put_bits(pb, bits, cb->quantlist[i]);
516 static void put_floor_header(PutBitContext *pb, vorbis_enc_floor *fc)
520 put_bits(pb, 16, 1); // type, only floor1 is supported
522 put_bits(pb, 5, fc->partitions);
524 for (i = 0; i < fc->partitions; i++)
525 put_bits(pb, 4, fc->partition_to_class[i]);
527 for (i = 0; i < fc->nclasses; i++) {
530 put_bits(pb, 3, fc->classes[i].dim - 1);
531 put_bits(pb, 2, fc->classes[i].subclass);
533 if (fc->classes[i].subclass)
534 put_bits(pb, 8, fc->classes[i].masterbook);
536 books = (1 << fc->classes[i].subclass);
538 for (j = 0; j < books; j++)
539 put_bits(pb, 8, fc->classes[i].books[j] + 1);
542 put_bits(pb, 2, fc->multiplier - 1);
543 put_bits(pb, 4, fc->rangebits);
545 for (i = 2; i < fc->values; i++)
546 put_bits(pb, fc->rangebits, fc->list[i].x);
549 static void put_residue_header(PutBitContext *pb, vorbis_enc_residue *rc)
553 put_bits(pb, 16, rc->type);
555 put_bits(pb, 24, rc->begin);
556 put_bits(pb, 24, rc->end);
557 put_bits(pb, 24, rc->partition_size - 1);
558 put_bits(pb, 6, rc->classifications - 1);
559 put_bits(pb, 8, rc->classbook);
561 for (i = 0; i < rc->classifications; i++) {
563 for (j = 0; j < 8; j++)
564 tmp |= (rc->books[i][j] != -1) << j;
566 put_bits(pb, 3, tmp & 7);
567 put_bits(pb, 1, tmp > 7);
570 put_bits(pb, 5, tmp >> 3);
573 for (i = 0; i < rc->classifications; i++) {
575 for (j = 0; j < 8; j++)
576 if (rc->books[i][j] != -1)
577 put_bits(pb, 8, rc->books[i][j]);
581 static int put_main_header(vorbis_enc_context *venc, uint8_t **out)
585 uint8_t buffer[50000] = {0}, *p = buffer;
586 int buffer_len = sizeof buffer;
589 // identification header
590 init_put_bits(&pb, p, buffer_len);
591 put_bits(&pb, 8, 1); //magic
592 for (i = 0; "vorbis"[i]; i++)
593 put_bits(&pb, 8, "vorbis"[i]);
594 put_bits32(&pb, 0); // version
595 put_bits(&pb, 8, venc->channels);
596 put_bits32(&pb, venc->sample_rate);
597 put_bits32(&pb, 0); // bitrate
598 put_bits32(&pb, 0); // bitrate
599 put_bits32(&pb, 0); // bitrate
600 put_bits(&pb, 4, venc->log2_blocksize[0]);
601 put_bits(&pb, 4, venc->log2_blocksize[1]);
602 put_bits(&pb, 1, 1); // framing
605 hlens[0] = put_bits_count(&pb) >> 3;
606 buffer_len -= hlens[0];
610 init_put_bits(&pb, p, buffer_len);
611 put_bits(&pb, 8, 3); //magic
612 for (i = 0; "vorbis"[i]; i++)
613 put_bits(&pb, 8, "vorbis"[i]);
614 put_bits32(&pb, 0); // vendor length TODO
615 put_bits32(&pb, 0); // amount of comments
616 put_bits(&pb, 1, 1); // framing
619 hlens[1] = put_bits_count(&pb) >> 3;
620 buffer_len -= hlens[1];
624 init_put_bits(&pb, p, buffer_len);
625 put_bits(&pb, 8, 5); //magic
626 for (i = 0; "vorbis"[i]; i++)
627 put_bits(&pb, 8, "vorbis"[i]);
630 put_bits(&pb, 8, venc->ncodebooks - 1);
631 for (i = 0; i < venc->ncodebooks; i++)
632 put_codebook_header(&pb, &venc->codebooks[i]);
634 // time domain, reserved, zero
636 put_bits(&pb, 16, 0);
639 put_bits(&pb, 6, venc->nfloors - 1);
640 for (i = 0; i < venc->nfloors; i++)
641 put_floor_header(&pb, &venc->floors[i]);
644 put_bits(&pb, 6, venc->nresidues - 1);
645 for (i = 0; i < venc->nresidues; i++)
646 put_residue_header(&pb, &venc->residues[i]);
649 put_bits(&pb, 6, venc->nmappings - 1);
650 for (i = 0; i < venc->nmappings; i++) {
651 vorbis_enc_mapping *mc = &venc->mappings[i];
653 put_bits(&pb, 16, 0); // mapping type
655 put_bits(&pb, 1, mc->submaps > 1);
657 put_bits(&pb, 4, mc->submaps - 1);
659 put_bits(&pb, 1, !!mc->coupling_steps);
660 if (mc->coupling_steps) {
661 put_bits(&pb, 8, mc->coupling_steps - 1);
662 for (j = 0; j < mc->coupling_steps; j++) {
663 put_bits(&pb, ilog(venc->channels - 1), mc->magnitude[j]);
664 put_bits(&pb, ilog(venc->channels - 1), mc->angle[j]);
668 put_bits(&pb, 2, 0); // reserved
671 for (j = 0; j < venc->channels; j++)
672 put_bits(&pb, 4, mc->mux[j]);
674 for (j = 0; j < mc->submaps; j++) {
675 put_bits(&pb, 8, 0); // reserved time configuration
676 put_bits(&pb, 8, mc->floor[j]);
677 put_bits(&pb, 8, mc->residue[j]);
682 put_bits(&pb, 6, venc->nmodes - 1);
683 for (i = 0; i < venc->nmodes; i++) {
684 put_bits(&pb, 1, venc->modes[i].blockflag);
685 put_bits(&pb, 16, 0); // reserved window type
686 put_bits(&pb, 16, 0); // reserved transform type
687 put_bits(&pb, 8, venc->modes[i].mapping);
690 put_bits(&pb, 1, 1); // framing
693 hlens[2] = put_bits_count(&pb) >> 3;
695 len = hlens[0] + hlens[1] + hlens[2];
696 p = *out = av_mallocz(64 + len + len/255);
698 return AVERROR(ENOMEM);
701 p += av_xiphlacing(p, hlens[0]);
702 p += av_xiphlacing(p, hlens[1]);
704 for (i = 0; i < 3; i++) {
705 memcpy(p, buffer + buffer_len, hlens[i]);
707 buffer_len += hlens[i];
713 static float get_floor_average(vorbis_enc_floor * fc, float *coeffs, int i)
715 int begin = fc->list[fc->list[FFMAX(i-1, 0)].sort].x;
716 int end = fc->list[fc->list[FFMIN(i+1, fc->values - 1)].sort].x;
720 for (j = begin; j < end; j++)
721 average += fabs(coeffs[j]);
722 return average / (end - begin);
725 static void floor_fit(vorbis_enc_context *venc, vorbis_enc_floor *fc,
726 float *coeffs, uint16_t *posts, int samples)
728 int range = 255 / fc->multiplier + 1;
730 float tot_average = 0.;
731 float averages[MAX_FLOOR_VALUES];
732 for (i = 0; i < fc->values; i++) {
733 averages[i] = get_floor_average(fc, coeffs, i);
734 tot_average += averages[i];
736 tot_average /= fc->values;
737 tot_average /= venc->quality;
739 for (i = 0; i < fc->values; i++) {
740 int position = fc->list[fc->list[i].sort].x;
741 float average = averages[i];
744 average = sqrt(tot_average * average) * pow(1.25f, position*0.005f); // MAGIC!
745 for (j = 0; j < range - 1; j++)
746 if (ff_vorbis_floor1_inverse_db_table[j * fc->multiplier] > average)
748 posts[fc->list[i].sort] = j;
752 static int render_point(int x0, int y0, int x1, int y1, int x)
754 return y0 + (x - x0) * (y1 - y0) / (x1 - x0);
757 static int floor_encode(vorbis_enc_context *venc, vorbis_enc_floor *fc,
758 PutBitContext *pb, uint16_t *posts,
759 float *floor, int samples)
761 int range = 255 / fc->multiplier + 1;
762 int coded[MAX_FLOOR_VALUES]; // first 2 values are unused
765 if (pb->size_in_bits - put_bits_count(pb) < 1 + 2 * ilog(range - 1))
766 return AVERROR(EINVAL);
767 put_bits(pb, 1, 1); // non zero
768 put_bits(pb, ilog(range - 1), posts[0]);
769 put_bits(pb, ilog(range - 1), posts[1]);
770 coded[0] = coded[1] = 1;
772 for (i = 2; i < fc->values; i++) {
773 int predicted = render_point(fc->list[fc->list[i].low].x,
774 posts[fc->list[i].low],
775 fc->list[fc->list[i].high].x,
776 posts[fc->list[i].high],
778 int highroom = range - predicted;
779 int lowroom = predicted;
780 int room = FFMIN(highroom, lowroom);
781 if (predicted == posts[i]) {
782 coded[i] = 0; // must be used later as flag!
785 if (!coded[fc->list[i].low ])
786 coded[fc->list[i].low ] = -1;
787 if (!coded[fc->list[i].high])
788 coded[fc->list[i].high] = -1;
790 if (posts[i] > predicted) {
791 if (posts[i] - predicted > room)
792 coded[i] = posts[i] - predicted + lowroom;
794 coded[i] = (posts[i] - predicted) << 1;
796 if (predicted - posts[i] > room)
797 coded[i] = predicted - posts[i] + highroom - 1;
799 coded[i] = ((predicted - posts[i]) << 1) - 1;
804 for (i = 0; i < fc->partitions; i++) {
805 vorbis_enc_floor_class * c = &fc->classes[fc->partition_to_class[i]];
806 int k, cval = 0, csub = 1<<c->subclass;
808 vorbis_enc_codebook * book = &venc->codebooks[c->masterbook];
810 for (k = 0; k < c->dim; k++) {
812 for (l = 0; l < csub; l++) {
814 if (c->books[l] != -1)
815 maxval = venc->codebooks[c->books[l]].nentries;
816 // coded could be -1, but this still works, cause that is 0
817 if (coded[counter + k] < maxval)
822 cshift += c->subclass;
824 if (put_codeword(pb, book, cval))
825 return AVERROR(EINVAL);
827 for (k = 0; k < c->dim; k++) {
828 int book = c->books[cval & (csub-1)];
829 int entry = coded[counter++];
830 cval >>= c->subclass;
835 if (put_codeword(pb, &venc->codebooks[book], entry))
836 return AVERROR(EINVAL);
840 ff_vorbis_floor1_render_list(fc->list, fc->values, posts, coded,
841 fc->multiplier, floor, samples);
846 static float *put_vector(vorbis_enc_codebook *book, PutBitContext *pb,
850 float distance = FLT_MAX;
851 assert(book->dimentions);
852 for (i = 0; i < book->nentries; i++) {
853 float * vec = book->dimentions + i * book->ndimentions, d = book->pow2[i];
857 for (j = 0; j < book->ndimentions; j++)
858 d -= vec[j] * num[j];
864 if (put_codeword(pb, book, entry))
866 return &book->dimentions[entry * book->ndimentions];
869 static int residue_encode(vorbis_enc_context *venc, vorbis_enc_residue *rc,
870 PutBitContext *pb, float *coeffs, int samples,
873 int pass, i, j, p, k;
874 int psize = rc->partition_size;
875 int partitions = (rc->end - rc->begin) / psize;
876 int channels = (rc->type == 2) ? 1 : real_ch;
877 int classes[MAX_CHANNELS][NUM_RESIDUE_PARTITIONS];
878 int classwords = venc->codebooks[rc->classbook].ndimentions;
880 assert(rc->type == 2);
881 assert(real_ch == 2);
882 for (p = 0; p < partitions; p++) {
883 float max1 = 0., max2 = 0.;
884 int s = rc->begin + p * psize;
885 for (k = s; k < s + psize; k += 2) {
886 max1 = FFMAX(max1, fabs(coeffs[ k / real_ch]));
887 max2 = FFMAX(max2, fabs(coeffs[samples + k / real_ch]));
890 for (i = 0; i < rc->classifications - 1; i++)
891 if (max1 < rc->maxes[i][0] && max2 < rc->maxes[i][1])
896 for (pass = 0; pass < 8; pass++) {
898 while (p < partitions) {
900 for (j = 0; j < channels; j++) {
901 vorbis_enc_codebook * book = &venc->codebooks[rc->classbook];
903 for (i = 0; i < classwords; i++) {
904 entry *= rc->classifications;
905 entry += classes[j][p + i];
907 if (put_codeword(pb, book, entry))
908 return AVERROR(EINVAL);
910 for (i = 0; i < classwords && p < partitions; i++, p++) {
911 for (j = 0; j < channels; j++) {
912 int nbook = rc->books[classes[j][p]][pass];
913 vorbis_enc_codebook * book = &venc->codebooks[nbook];
914 float *buf = coeffs + samples*j + rc->begin + p*psize;
918 assert(rc->type == 0 || rc->type == 2);
919 assert(!(psize % book->ndimentions));
922 for (k = 0; k < psize; k += book->ndimentions) {
924 float *a = put_vector(book, pb, &buf[k]);
926 return AVERROR(EINVAL);
927 for (l = 0; l < book->ndimentions; l++)
931 int s = rc->begin + p * psize, a1, b1;
932 a1 = (s % real_ch) * samples;
934 s = real_ch * samples;
935 for (k = 0; k < psize; k += book->ndimentions) {
936 int dim, a2 = a1, b2 = b1;
937 float vec[MAX_CODEBOOK_DIM], *pv = vec;
938 for (dim = book->ndimentions; dim--; ) {
939 *pv++ = coeffs[a2 + b2];
940 if ((a2 += samples) == s) {
945 pv = put_vector(book, pb, vec);
947 return AVERROR(EINVAL);
948 for (dim = book->ndimentions; dim--; ) {
949 coeffs[a1 + b1] -= *pv++;
950 if ((a1 += samples) == s) {
964 static int apply_window_and_mdct(vorbis_enc_context *venc, const signed short *audio,
968 const float * win = venc->win[0];
969 int window_len = 1 << (venc->log2_blocksize[0] - 1);
970 float n = (float)(1 << venc->log2_blocksize[0]) / 4.;
973 if (!venc->have_saved && !samples)
976 if (venc->have_saved) {
977 for (channel = 0; channel < venc->channels; channel++)
978 memcpy(venc->samples + channel * window_len * 2,
979 venc->saved + channel * window_len, sizeof(float) * window_len);
981 for (channel = 0; channel < venc->channels; channel++)
982 memset(venc->samples + channel * window_len * 2, 0,
983 sizeof(float) * window_len);
987 for (channel = 0; channel < venc->channels; channel++) {
988 float * offset = venc->samples + channel*window_len*2 + window_len;
990 for (i = 0; i < samples; i++, j += venc->channels)
991 offset[i] = audio[j] / 32768. / n * win[window_len - i - 1];
994 for (channel = 0; channel < venc->channels; channel++)
995 memset(venc->samples + channel * window_len * 2 + window_len,
996 0, sizeof(float) * window_len);
999 for (channel = 0; channel < venc->channels; channel++)
1000 venc->mdct[0].mdct_calc(&venc->mdct[0], venc->coeffs + channel * window_len,
1001 venc->samples + channel * window_len * 2);
1004 for (channel = 0; channel < venc->channels; channel++) {
1005 float *offset = venc->saved + channel * window_len;
1007 for (i = 0; i < samples; i++, j += venc->channels)
1008 offset[i] = audio[j] / 32768. / n * win[i];
1010 venc->have_saved = 1;
1012 venc->have_saved = 0;
1018 static int vorbis_encode_frame(AVCodecContext *avccontext,
1019 unsigned char *packets,
1020 int buf_size, void *data)
1022 vorbis_enc_context *venc = avccontext->priv_data;
1023 const signed short *audio = data;
1024 int samples = data ? avccontext->frame_size : 0;
1025 vorbis_enc_mode *mode;
1026 vorbis_enc_mapping *mapping;
1030 if (!apply_window_and_mdct(venc, audio, samples))
1032 samples = 1 << (venc->log2_blocksize[0] - 1);
1034 init_put_bits(&pb, packets, buf_size);
1036 if (pb.size_in_bits - put_bits_count(&pb) < 1 + ilog(venc->nmodes - 1)) {
1037 av_log(avccontext, AV_LOG_ERROR, "output buffer is too small\n");
1038 return AVERROR(EINVAL);
1041 put_bits(&pb, 1, 0); // magic bit
1043 put_bits(&pb, ilog(venc->nmodes - 1), 0); // 0 bits, the mode
1045 mode = &venc->modes[0];
1046 mapping = &venc->mappings[mode->mapping];
1047 if (mode->blockflag) {
1048 put_bits(&pb, 1, 0);
1049 put_bits(&pb, 1, 0);
1052 for (i = 0; i < venc->channels; i++) {
1053 vorbis_enc_floor *fc = &venc->floors[mapping->floor[mapping->mux[i]]];
1054 uint16_t posts[MAX_FLOOR_VALUES];
1055 floor_fit(venc, fc, &venc->coeffs[i * samples], posts, samples);
1056 if (floor_encode(venc, fc, &pb, posts, &venc->floor[i * samples], samples)) {
1057 av_log(avccontext, AV_LOG_ERROR, "output buffer is too small\n");
1058 return AVERROR(EINVAL);
1062 for (i = 0; i < venc->channels * samples; i++)
1063 venc->coeffs[i] /= venc->floor[i];
1065 for (i = 0; i < mapping->coupling_steps; i++) {
1066 float *mag = venc->coeffs + mapping->magnitude[i] * samples;
1067 float *ang = venc->coeffs + mapping->angle[i] * samples;
1069 for (j = 0; j < samples; j++) {
1079 if (residue_encode(venc, &venc->residues[mapping->residue[mapping->mux[0]]],
1080 &pb, venc->coeffs, samples, venc->channels)) {
1081 av_log(avccontext, AV_LOG_ERROR, "output buffer is too small\n");
1082 return AVERROR(EINVAL);
1085 avccontext->coded_frame->pts = venc->sample_count;
1086 venc->sample_count += avccontext->frame_size;
1087 flush_put_bits(&pb);
1088 return put_bits_count(&pb) >> 3;
1092 static av_cold int vorbis_encode_close(AVCodecContext *avccontext)
1094 vorbis_enc_context *venc = avccontext->priv_data;
1097 if (venc->codebooks)
1098 for (i = 0; i < venc->ncodebooks; i++) {
1099 av_freep(&venc->codebooks[i].lens);
1100 av_freep(&venc->codebooks[i].codewords);
1101 av_freep(&venc->codebooks[i].quantlist);
1102 av_freep(&venc->codebooks[i].dimentions);
1103 av_freep(&venc->codebooks[i].pow2);
1105 av_freep(&venc->codebooks);
1108 for (i = 0; i < venc->nfloors; i++) {
1110 if (venc->floors[i].classes)
1111 for (j = 0; j < venc->floors[i].nclasses; j++)
1112 av_freep(&venc->floors[i].classes[j].books);
1113 av_freep(&venc->floors[i].classes);
1114 av_freep(&venc->floors[i].partition_to_class);
1115 av_freep(&venc->floors[i].list);
1117 av_freep(&venc->floors);
1120 for (i = 0; i < venc->nresidues; i++) {
1121 av_freep(&venc->residues[i].books);
1122 av_freep(&venc->residues[i].maxes);
1124 av_freep(&venc->residues);
1127 for (i = 0; i < venc->nmappings; i++) {
1128 av_freep(&venc->mappings[i].mux);
1129 av_freep(&venc->mappings[i].floor);
1130 av_freep(&venc->mappings[i].residue);
1131 av_freep(&venc->mappings[i].magnitude);
1132 av_freep(&venc->mappings[i].angle);
1134 av_freep(&venc->mappings);
1136 av_freep(&venc->modes);
1138 av_freep(&venc->saved);
1139 av_freep(&venc->samples);
1140 av_freep(&venc->floor);
1141 av_freep(&venc->coeffs);
1143 ff_mdct_end(&venc->mdct[0]);
1144 ff_mdct_end(&venc->mdct[1]);
1146 av_freep(&avccontext->coded_frame);
1147 av_freep(&avccontext->extradata);
1152 static av_cold int vorbis_encode_init(AVCodecContext *avccontext)
1154 vorbis_enc_context *venc = avccontext->priv_data;
1157 if (avccontext->channels != 2) {
1158 av_log(avccontext, AV_LOG_ERROR, "Current Libav Vorbis encoder only supports 2 channels.\n");
1162 if ((ret = create_vorbis_context(venc, avccontext)) < 0)
1165 if (avccontext->flags & CODEC_FLAG_QSCALE)
1166 venc->quality = avccontext->global_quality / (float)FF_QP2LAMBDA / 10.;
1168 venc->quality = 0.03;
1169 venc->quality *= venc->quality;
1171 if ((ret = put_main_header(venc, (uint8_t**)&avccontext->extradata)) < 0)
1173 avccontext->extradata_size = ret;
1175 avccontext->frame_size = 1 << (venc->log2_blocksize[0] - 1);
1177 avccontext->coded_frame = avcodec_alloc_frame();
1178 if (!avccontext->coded_frame) {
1179 ret = AVERROR(ENOMEM);
1185 vorbis_encode_close(avccontext);
1189 AVCodec ff_vorbis_encoder = {
1191 .type = AVMEDIA_TYPE_AUDIO,
1192 .id = CODEC_ID_VORBIS,
1193 .priv_data_size = sizeof(vorbis_enc_context),
1194 .init = vorbis_encode_init,
1195 .encode = vorbis_encode_frame,
1196 .close = vorbis_encode_close,
1197 .capabilities= CODEC_CAP_DELAY | CODEC_CAP_EXPERIMENTAL,
1198 .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
1199 .long_name = NULL_IF_CONFIG_SMALL("Vorbis"),