2 * copyright (c) 2006 Oded Shimon <ods15@ods15.dyndns.org>
4 * This file is part of FFmpeg.
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.
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.
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
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 static inline void put_codeword(PutBitContext *pb, vorbis_enc_codebook *cb,
133 assert(entry < cb->nentries);
134 assert(cb->lens[entry]);
135 put_bits(pb, cb->lens[entry], cb->codewords[entry]);
138 static int cb_lookup_vals(int lookup, int dimentions, int entries)
141 return ff_vorbis_nth_root(entries, dimentions);
142 else if (lookup == 2)
143 return dimentions *entries;
147 static void ready_codebook(vorbis_enc_codebook *cb)
151 ff_vorbis_len2vlc(cb->lens, cb->codewords, cb->nentries);
154 cb->pow2 = cb->dimentions = NULL;
156 int vals = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries);
157 cb->dimentions = av_malloc(sizeof(float) * cb->nentries * cb->ndimentions);
158 cb->pow2 = av_mallocz(sizeof(float) * cb->nentries);
159 for (i = 0; i < cb->nentries; i++) {
163 for (j = 0; j < cb->ndimentions; j++) {
166 off = (i / div) % vals; // lookup type 1
168 off = i * cb->ndimentions + j; // lookup type 2
170 cb->dimentions[i * cb->ndimentions + j] = last + cb->min + cb->quantlist[off] * cb->delta;
172 last = cb->dimentions[i * cb->ndimentions + j];
173 cb->pow2[i] += cb->dimentions[i * cb->ndimentions + j] * cb->dimentions[i * cb->ndimentions + j];
181 static void ready_residue(vorbis_enc_residue *rc, vorbis_enc_context *venc)
184 assert(rc->type == 2);
185 rc->maxes = av_mallocz(sizeof(float[2]) * rc->classifications);
186 for (i = 0; i < rc->classifications; i++) {
188 vorbis_enc_codebook * cb;
189 for (j = 0; j < 8; j++)
190 if (rc->books[i][j] != -1)
194 cb = &venc->codebooks[rc->books[i][j]];
195 assert(cb->ndimentions >= 2);
198 for (j = 0; j < cb->nentries; j++) {
202 a = fabs(cb->dimentions[j * cb->ndimentions]);
203 if (a > rc->maxes[i][0])
205 a = fabs(cb->dimentions[j * cb->ndimentions + 1]);
206 if (a > rc->maxes[i][1])
211 for (i = 0; i < rc->classifications; i++) {
212 rc->maxes[i][0] += 0.8;
213 rc->maxes[i][1] += 0.8;
217 static void create_vorbis_context(vorbis_enc_context *venc,
218 AVCodecContext *avccontext)
220 vorbis_enc_floor *fc;
221 vorbis_enc_residue *rc;
222 vorbis_enc_mapping *mc;
225 venc->channels = avccontext->channels;
226 venc->sample_rate = avccontext->sample_rate;
227 venc->log2_blocksize[0] = venc->log2_blocksize[1] = 11;
229 venc->ncodebooks = FF_ARRAY_ELEMS(cvectors);
230 venc->codebooks = av_malloc(sizeof(vorbis_enc_codebook) * venc->ncodebooks);
232 // codebook 0..14 - floor1 book, values 0..255
233 // codebook 15 residue masterbook
234 // codebook 16..29 residue
235 for (book = 0; book < venc->ncodebooks; book++) {
236 vorbis_enc_codebook *cb = &venc->codebooks[book];
238 cb->ndimentions = cvectors[book].dim;
239 cb->nentries = cvectors[book].real_len;
240 cb->min = cvectors[book].min;
241 cb->delta = cvectors[book].delta;
242 cb->lookup = cvectors[book].lookup;
245 cb->lens = av_malloc(sizeof(uint8_t) * cb->nentries);
246 cb->codewords = av_malloc(sizeof(uint32_t) * cb->nentries);
247 memcpy(cb->lens, cvectors[book].clens, cvectors[book].len);
248 memset(cb->lens + cvectors[book].len, 0, cb->nentries - cvectors[book].len);
251 vals = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries);
252 cb->quantlist = av_malloc(sizeof(int) * vals);
253 for (i = 0; i < vals; i++)
254 cb->quantlist[i] = cvectors[book].quant[i];
256 cb->quantlist = NULL;
262 venc->floors = av_malloc(sizeof(vorbis_enc_floor) * venc->nfloors);
265 fc = &venc->floors[0];
267 fc->partition_to_class = av_malloc(sizeof(int) * fc->partitions);
269 for (i = 0; i < fc->partitions; i++) {
270 static const int a[] = {0, 1, 2, 2, 3, 3, 4, 4};
271 fc->partition_to_class[i] = a[i];
272 fc->nclasses = FFMAX(fc->nclasses, fc->partition_to_class[i]);
275 fc->classes = av_malloc(sizeof(vorbis_enc_floor_class) * fc->nclasses);
276 for (i = 0; i < fc->nclasses; i++) {
277 vorbis_enc_floor_class * c = &fc->classes[i];
279 c->dim = floor_classes[i].dim;
280 c->subclass = floor_classes[i].subclass;
281 c->masterbook = floor_classes[i].masterbook;
282 books = (1 << c->subclass);
283 c->books = av_malloc(sizeof(int) * books);
284 for (j = 0; j < books; j++)
285 c->books[j] = floor_classes[i].nbooks[j];
288 fc->rangebits = venc->log2_blocksize[0] - 1;
291 for (i = 0; i < fc->partitions; i++)
292 fc->values += fc->classes[fc->partition_to_class[i]].dim;
294 fc->list = av_malloc(sizeof(vorbis_floor1_entry) * fc->values);
296 fc->list[1].x = 1 << fc->rangebits;
297 for (i = 2; i < fc->values; i++) {
298 static const int a[] = {
299 93, 23,372, 6, 46,186,750, 14, 33, 65,
300 130,260,556, 3, 10, 18, 28, 39, 55, 79,
301 111,158,220,312,464,650,850
303 fc->list[i].x = a[i - 2];
305 ff_vorbis_ready_floor1_list(fc->list, fc->values);
308 venc->residues = av_malloc(sizeof(vorbis_enc_residue) * venc->nresidues);
311 rc = &venc->residues[0];
315 rc->partition_size = 32;
316 rc->classifications = 10;
318 rc->books = av_malloc(sizeof(*rc->books) * rc->classifications);
320 static const int8_t a[10][8] = {
321 { -1, -1, -1, -1, -1, -1, -1, -1, },
322 { -1, -1, 16, -1, -1, -1, -1, -1, },
323 { -1, -1, 17, -1, -1, -1, -1, -1, },
324 { -1, -1, 18, -1, -1, -1, -1, -1, },
325 { -1, -1, 19, -1, -1, -1, -1, -1, },
326 { -1, -1, 20, -1, -1, -1, -1, -1, },
327 { -1, -1, 21, -1, -1, -1, -1, -1, },
328 { 22, 23, -1, -1, -1, -1, -1, -1, },
329 { 24, 25, -1, -1, -1, -1, -1, -1, },
330 { 26, 27, 28, -1, -1, -1, -1, -1, },
332 memcpy(rc->books, a, sizeof a);
334 ready_residue(rc, venc);
337 venc->mappings = av_malloc(sizeof(vorbis_enc_mapping) * venc->nmappings);
340 mc = &venc->mappings[0];
342 mc->mux = av_malloc(sizeof(int) * venc->channels);
343 for (i = 0; i < venc->channels; i++)
345 mc->floor = av_malloc(sizeof(int) * mc->submaps);
346 mc->residue = av_malloc(sizeof(int) * mc->submaps);
347 for (i = 0; i < mc->submaps; i++) {
351 mc->coupling_steps = venc->channels == 2 ? 1 : 0;
352 mc->magnitude = av_malloc(sizeof(int) * mc->coupling_steps);
353 mc->angle = av_malloc(sizeof(int) * mc->coupling_steps);
354 if (mc->coupling_steps) {
355 mc->magnitude[0] = 0;
360 venc->modes = av_malloc(sizeof(vorbis_enc_mode) * venc->nmodes);
363 venc->modes[0].blockflag = 0;
364 venc->modes[0].mapping = 0;
366 venc->have_saved = 0;
367 venc->saved = av_malloc(sizeof(float) * venc->channels * (1 << venc->log2_blocksize[1]) / 2);
368 venc->samples = av_malloc(sizeof(float) * venc->channels * (1 << venc->log2_blocksize[1]));
369 venc->floor = av_malloc(sizeof(float) * venc->channels * (1 << venc->log2_blocksize[1]) / 2);
370 venc->coeffs = av_malloc(sizeof(float) * venc->channels * (1 << venc->log2_blocksize[1]) / 2);
372 venc->win[0] = ff_vorbis_vwin[venc->log2_blocksize[0] - 6];
373 venc->win[1] = ff_vorbis_vwin[venc->log2_blocksize[1] - 6];
375 ff_mdct_init(&venc->mdct[0], venc->log2_blocksize[0], 0, 1.0);
376 ff_mdct_init(&venc->mdct[1], venc->log2_blocksize[1], 0, 1.0);
379 static void put_float(PutBitContext *pb, float f)
383 mant = (int)ldexp(frexp(f, &exp), 20);
389 res |= mant | (exp << 21);
393 static void put_codebook_header(PutBitContext *pb, vorbis_enc_codebook *cb)
398 put_bits(pb, 24, 0x564342); //magic
399 put_bits(pb, 16, cb->ndimentions);
400 put_bits(pb, 24, cb->nentries);
402 for (i = 1; i < cb->nentries; i++)
403 if (cb->lens[i] < cb->lens[i-1])
405 if (i == cb->nentries)
408 put_bits(pb, 1, ordered);
410 int len = cb->lens[0];
411 put_bits(pb, 5, len - 1);
413 while (i < cb->nentries) {
415 for (j = 0; j+i < cb->nentries; j++)
416 if (cb->lens[j+i] != len)
418 put_bits(pb, ilog(cb->nentries - i), j);
424 for (i = 0; i < cb->nentries; i++)
427 if (i != cb->nentries)
429 put_bits(pb, 1, sparse);
431 for (i = 0; i < cb->nentries; i++) {
433 put_bits(pb, 1, !!cb->lens[i]);
435 put_bits(pb, 5, cb->lens[i] - 1);
439 put_bits(pb, 4, cb->lookup);
441 int tmp = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries);
442 int bits = ilog(cb->quantlist[0]);
444 for (i = 1; i < tmp; i++)
445 bits = FFMAX(bits, ilog(cb->quantlist[i]));
447 put_float(pb, cb->min);
448 put_float(pb, cb->delta);
450 put_bits(pb, 4, bits - 1);
451 put_bits(pb, 1, cb->seq_p);
453 for (i = 0; i < tmp; i++)
454 put_bits(pb, bits, cb->quantlist[i]);
458 static void put_floor_header(PutBitContext *pb, vorbis_enc_floor *fc)
462 put_bits(pb, 16, 1); // type, only floor1 is supported
464 put_bits(pb, 5, fc->partitions);
466 for (i = 0; i < fc->partitions; i++)
467 put_bits(pb, 4, fc->partition_to_class[i]);
469 for (i = 0; i < fc->nclasses; i++) {
472 put_bits(pb, 3, fc->classes[i].dim - 1);
473 put_bits(pb, 2, fc->classes[i].subclass);
475 if (fc->classes[i].subclass)
476 put_bits(pb, 8, fc->classes[i].masterbook);
478 books = (1 << fc->classes[i].subclass);
480 for (j = 0; j < books; j++)
481 put_bits(pb, 8, fc->classes[i].books[j] + 1);
484 put_bits(pb, 2, fc->multiplier - 1);
485 put_bits(pb, 4, fc->rangebits);
487 for (i = 2; i < fc->values; i++)
488 put_bits(pb, fc->rangebits, fc->list[i].x);
491 static void put_residue_header(PutBitContext *pb, vorbis_enc_residue *rc)
495 put_bits(pb, 16, rc->type);
497 put_bits(pb, 24, rc->begin);
498 put_bits(pb, 24, rc->end);
499 put_bits(pb, 24, rc->partition_size - 1);
500 put_bits(pb, 6, rc->classifications - 1);
501 put_bits(pb, 8, rc->classbook);
503 for (i = 0; i < rc->classifications; i++) {
505 for (j = 0; j < 8; j++)
506 tmp |= (rc->books[i][j] != -1) << j;
508 put_bits(pb, 3, tmp & 7);
509 put_bits(pb, 1, tmp > 7);
512 put_bits(pb, 5, tmp >> 3);
515 for (i = 0; i < rc->classifications; i++) {
517 for (j = 0; j < 8; j++)
518 if (rc->books[i][j] != -1)
519 put_bits(pb, 8, rc->books[i][j]);
523 static int put_main_header(vorbis_enc_context *venc, uint8_t **out)
527 uint8_t buffer[50000] = {0}, *p = buffer;
528 int buffer_len = sizeof buffer;
531 // identification header
532 init_put_bits(&pb, p, buffer_len);
533 put_bits(&pb, 8, 1); //magic
534 for (i = 0; "vorbis"[i]; i++)
535 put_bits(&pb, 8, "vorbis"[i]);
536 put_bits32(&pb, 0); // version
537 put_bits(&pb, 8, venc->channels);
538 put_bits32(&pb, venc->sample_rate);
539 put_bits32(&pb, 0); // bitrate
540 put_bits32(&pb, 0); // bitrate
541 put_bits32(&pb, 0); // bitrate
542 put_bits(&pb, 4, venc->log2_blocksize[0]);
543 put_bits(&pb, 4, venc->log2_blocksize[1]);
544 put_bits(&pb, 1, 1); // framing
547 hlens[0] = put_bits_count(&pb) >> 3;
548 buffer_len -= hlens[0];
552 init_put_bits(&pb, p, buffer_len);
553 put_bits(&pb, 8, 3); //magic
554 for (i = 0; "vorbis"[i]; i++)
555 put_bits(&pb, 8, "vorbis"[i]);
556 put_bits32(&pb, 0); // vendor length TODO
557 put_bits32(&pb, 0); // amount of comments
558 put_bits(&pb, 1, 1); // framing
561 hlens[1] = put_bits_count(&pb) >> 3;
562 buffer_len -= hlens[1];
566 init_put_bits(&pb, p, buffer_len);
567 put_bits(&pb, 8, 5); //magic
568 for (i = 0; "vorbis"[i]; i++)
569 put_bits(&pb, 8, "vorbis"[i]);
572 put_bits(&pb, 8, venc->ncodebooks - 1);
573 for (i = 0; i < venc->ncodebooks; i++)
574 put_codebook_header(&pb, &venc->codebooks[i]);
576 // time domain, reserved, zero
578 put_bits(&pb, 16, 0);
581 put_bits(&pb, 6, venc->nfloors - 1);
582 for (i = 0; i < venc->nfloors; i++)
583 put_floor_header(&pb, &venc->floors[i]);
586 put_bits(&pb, 6, venc->nresidues - 1);
587 for (i = 0; i < venc->nresidues; i++)
588 put_residue_header(&pb, &venc->residues[i]);
591 put_bits(&pb, 6, venc->nmappings - 1);
592 for (i = 0; i < venc->nmappings; i++) {
593 vorbis_enc_mapping *mc = &venc->mappings[i];
595 put_bits(&pb, 16, 0); // mapping type
597 put_bits(&pb, 1, mc->submaps > 1);
599 put_bits(&pb, 4, mc->submaps - 1);
601 put_bits(&pb, 1, !!mc->coupling_steps);
602 if (mc->coupling_steps) {
603 put_bits(&pb, 8, mc->coupling_steps - 1);
604 for (j = 0; j < mc->coupling_steps; j++) {
605 put_bits(&pb, ilog(venc->channels - 1), mc->magnitude[j]);
606 put_bits(&pb, ilog(venc->channels - 1), mc->angle[j]);
610 put_bits(&pb, 2, 0); // reserved
613 for (j = 0; j < venc->channels; j++)
614 put_bits(&pb, 4, mc->mux[j]);
616 for (j = 0; j < mc->submaps; j++) {
617 put_bits(&pb, 8, 0); // reserved time configuration
618 put_bits(&pb, 8, mc->floor[j]);
619 put_bits(&pb, 8, mc->residue[j]);
624 put_bits(&pb, 6, venc->nmodes - 1);
625 for (i = 0; i < venc->nmodes; i++) {
626 put_bits(&pb, 1, venc->modes[i].blockflag);
627 put_bits(&pb, 16, 0); // reserved window type
628 put_bits(&pb, 16, 0); // reserved transform type
629 put_bits(&pb, 8, venc->modes[i].mapping);
632 put_bits(&pb, 1, 1); // framing
635 hlens[2] = put_bits_count(&pb) >> 3;
637 len = hlens[0] + hlens[1] + hlens[2];
638 p = *out = av_mallocz(64 + len + len/255);
641 p += av_xiphlacing(p, hlens[0]);
642 p += av_xiphlacing(p, hlens[1]);
644 for (i = 0; i < 3; i++) {
645 memcpy(p, buffer + buffer_len, hlens[i]);
647 buffer_len += hlens[i];
653 static float get_floor_average(vorbis_enc_floor * fc, float *coeffs, int i)
655 int begin = fc->list[fc->list[FFMAX(i-1, 0)].sort].x;
656 int end = fc->list[fc->list[FFMIN(i+1, fc->values - 1)].sort].x;
660 for (j = begin; j < end; j++)
661 average += fabs(coeffs[j]);
662 return average / (end - begin);
665 static void floor_fit(vorbis_enc_context *venc, vorbis_enc_floor *fc,
666 float *coeffs, uint_fast16_t *posts, int samples)
668 int range = 255 / fc->multiplier + 1;
670 float tot_average = 0.;
671 float averages[fc->values];
672 for (i = 0; i < fc->values; i++) {
673 averages[i] = get_floor_average(fc, coeffs, i);
674 tot_average += averages[i];
676 tot_average /= fc->values;
677 tot_average /= venc->quality;
679 for (i = 0; i < fc->values; i++) {
680 int position = fc->list[fc->list[i].sort].x;
681 float average = averages[i];
684 average = sqrt(tot_average * average) * pow(1.25f, position*0.005f); // MAGIC!
685 for (j = 0; j < range - 1; j++)
686 if (ff_vorbis_floor1_inverse_db_table[j * fc->multiplier] > average)
688 posts[fc->list[i].sort] = j;
692 static int render_point(int x0, int y0, int x1, int y1, int x)
694 return y0 + (x - x0) * (y1 - y0) / (x1 - x0);
697 static void floor_encode(vorbis_enc_context *venc, vorbis_enc_floor *fc,
698 PutBitContext *pb, uint_fast16_t *posts,
699 float *floor, int samples)
701 int range = 255 / fc->multiplier + 1;
702 int coded[fc->values]; // first 2 values are unused
705 put_bits(pb, 1, 1); // non zero
706 put_bits(pb, ilog(range - 1), posts[0]);
707 put_bits(pb, ilog(range - 1), posts[1]);
708 coded[0] = coded[1] = 1;
710 for (i = 2; i < fc->values; i++) {
711 int predicted = render_point(fc->list[fc->list[i].low].x,
712 posts[fc->list[i].low],
713 fc->list[fc->list[i].high].x,
714 posts[fc->list[i].high],
716 int highroom = range - predicted;
717 int lowroom = predicted;
718 int room = FFMIN(highroom, lowroom);
719 if (predicted == posts[i]) {
720 coded[i] = 0; // must be used later as flag!
723 if (!coded[fc->list[i].low ])
724 coded[fc->list[i].low ] = -1;
725 if (!coded[fc->list[i].high])
726 coded[fc->list[i].high] = -1;
728 if (posts[i] > predicted) {
729 if (posts[i] - predicted > room)
730 coded[i] = posts[i] - predicted + lowroom;
732 coded[i] = (posts[i] - predicted) << 1;
734 if (predicted - posts[i] > room)
735 coded[i] = predicted - posts[i] + highroom - 1;
737 coded[i] = ((predicted - posts[i]) << 1) - 1;
742 for (i = 0; i < fc->partitions; i++) {
743 vorbis_enc_floor_class * c = &fc->classes[fc->partition_to_class[i]];
744 int k, cval = 0, csub = 1<<c->subclass;
746 vorbis_enc_codebook * book = &venc->codebooks[c->masterbook];
748 for (k = 0; k < c->dim; k++) {
750 for (l = 0; l < csub; l++) {
752 if (c->books[l] != -1)
753 maxval = venc->codebooks[c->books[l]].nentries;
754 // coded could be -1, but this still works, cause that is 0
755 if (coded[counter + k] < maxval)
760 cshift += c->subclass;
762 put_codeword(pb, book, cval);
764 for (k = 0; k < c->dim; k++) {
765 int book = c->books[cval & (csub-1)];
766 int entry = coded[counter++];
767 cval >>= c->subclass;
772 put_codeword(pb, &venc->codebooks[book], entry);
776 ff_vorbis_floor1_render_list(fc->list, fc->values, posts, coded,
777 fc->multiplier, floor, samples);
780 static float *put_vector(vorbis_enc_codebook *book, PutBitContext *pb,
784 float distance = FLT_MAX;
785 assert(book->dimentions);
786 for (i = 0; i < book->nentries; i++) {
787 float * vec = book->dimentions + i * book->ndimentions, d = book->pow2[i];
791 for (j = 0; j < book->ndimentions; j++)
792 d -= vec[j] * num[j];
798 put_codeword(pb, book, entry);
799 return &book->dimentions[entry * book->ndimentions];
802 static void residue_encode(vorbis_enc_context *venc, vorbis_enc_residue *rc,
803 PutBitContext *pb, float *coeffs, int samples,
806 int pass, i, j, p, k;
807 int psize = rc->partition_size;
808 int partitions = (rc->end - rc->begin) / psize;
809 int channels = (rc->type == 2) ? 1 : real_ch;
810 int classes[channels][partitions];
811 int classwords = venc->codebooks[rc->classbook].ndimentions;
813 assert(rc->type == 2);
814 assert(real_ch == 2);
815 for (p = 0; p < partitions; p++) {
816 float max1 = 0., max2 = 0.;
817 int s = rc->begin + p * psize;
818 for (k = s; k < s + psize; k += 2) {
819 max1 = FFMAX(max1, fabs(coeffs[ k / real_ch]));
820 max2 = FFMAX(max2, fabs(coeffs[samples + k / real_ch]));
823 for (i = 0; i < rc->classifications - 1; i++)
824 if (max1 < rc->maxes[i][0] && max2 < rc->maxes[i][1])
829 for (pass = 0; pass < 8; pass++) {
831 while (p < partitions) {
833 for (j = 0; j < channels; j++) {
834 vorbis_enc_codebook * book = &venc->codebooks[rc->classbook];
836 for (i = 0; i < classwords; i++) {
837 entry *= rc->classifications;
838 entry += classes[j][p + i];
840 put_codeword(pb, book, entry);
842 for (i = 0; i < classwords && p < partitions; i++, p++) {
843 for (j = 0; j < channels; j++) {
844 int nbook = rc->books[classes[j][p]][pass];
845 vorbis_enc_codebook * book = &venc->codebooks[nbook];
846 float *buf = coeffs + samples*j + rc->begin + p*psize;
850 assert(rc->type == 0 || rc->type == 2);
851 assert(!(psize % book->ndimentions));
854 for (k = 0; k < psize; k += book->ndimentions) {
855 float *a = put_vector(book, pb, &buf[k]);
857 for (l = 0; l < book->ndimentions; l++)
861 int s = rc->begin + p * psize, a1, b1;
862 a1 = (s % real_ch) * samples;
864 s = real_ch * samples;
865 for (k = 0; k < psize; k += book->ndimentions) {
866 int dim, a2 = a1, b2 = b1;
867 float vec[book->ndimentions], *pv = vec;
868 for (dim = book->ndimentions; dim--; ) {
869 *pv++ = coeffs[a2 + b2];
870 if ((a2 += samples) == s) {
875 pv = put_vector(book, pb, vec);
876 for (dim = book->ndimentions; dim--; ) {
877 coeffs[a1 + b1] -= *pv++;
878 if ((a1 += samples) == s) {
891 static int apply_window_and_mdct(vorbis_enc_context *venc, signed short *audio,
895 const float * win = venc->win[0];
896 int window_len = 1 << (venc->log2_blocksize[0] - 1);
897 float n = (float)(1 << venc->log2_blocksize[0]) / 4.;
900 if (!venc->have_saved && !samples)
903 if (venc->have_saved) {
904 for (channel = 0; channel < venc->channels; channel++)
905 memcpy(venc->samples + channel * window_len * 2,
906 venc->saved + channel * window_len, sizeof(float) * window_len);
908 for (channel = 0; channel < venc->channels; channel++)
909 memset(venc->samples + channel * window_len * 2, 0,
910 sizeof(float) * window_len);
914 for (channel = 0; channel < venc->channels; channel++) {
915 float * offset = venc->samples + channel*window_len*2 + window_len;
917 for (i = 0; i < samples; i++, j += venc->channels)
918 offset[i] = audio[j] / 32768. / n * win[window_len - i - 1];
921 for (channel = 0; channel < venc->channels; channel++)
922 memset(venc->samples + channel * window_len * 2 + window_len,
923 0, sizeof(float) * window_len);
926 for (channel = 0; channel < venc->channels; channel++)
927 ff_mdct_calc(&venc->mdct[0], venc->coeffs + channel * window_len,
928 venc->samples + channel * window_len * 2);
931 for (channel = 0; channel < venc->channels; channel++) {
932 float *offset = venc->saved + channel * window_len;
934 for (i = 0; i < samples; i++, j += venc->channels)
935 offset[i] = audio[j] / 32768. / n * win[i];
937 venc->have_saved = 1;
939 venc->have_saved = 0;
944 static av_cold int vorbis_encode_init(AVCodecContext *avccontext)
946 vorbis_enc_context *venc = avccontext->priv_data;
948 if (avccontext->channels != 2) {
949 av_log(avccontext, AV_LOG_ERROR, "Current FFmpeg Vorbis encoder only supports 2 channels.\n");
953 create_vorbis_context(venc, avccontext);
955 if (avccontext->flags & CODEC_FLAG_QSCALE)
956 venc->quality = avccontext->global_quality / (float)FF_QP2LAMBDA / 10.;
958 venc->quality = 0.03;
959 venc->quality *= venc->quality;
961 avccontext->extradata_size = put_main_header(venc, (uint8_t**)&avccontext->extradata);
963 avccontext->frame_size = 1 << (venc->log2_blocksize[0] - 1);
965 avccontext->coded_frame = avcodec_alloc_frame();
966 avccontext->coded_frame->key_frame = 1;
971 static int vorbis_encode_frame(AVCodecContext *avccontext,
972 unsigned char *packets,
973 int buf_size, void *data)
975 vorbis_enc_context *venc = avccontext->priv_data;
976 signed short *audio = data;
977 int samples = data ? avccontext->frame_size : 0;
978 vorbis_enc_mode *mode;
979 vorbis_enc_mapping *mapping;
983 if (!apply_window_and_mdct(venc, audio, samples))
985 samples = 1 << (venc->log2_blocksize[0] - 1);
987 init_put_bits(&pb, packets, buf_size);
989 put_bits(&pb, 1, 0); // magic bit
991 put_bits(&pb, ilog(venc->nmodes - 1), 0); // 0 bits, the mode
993 mode = &venc->modes[0];
994 mapping = &venc->mappings[mode->mapping];
995 if (mode->blockflag) {
1000 for (i = 0; i < venc->channels; i++) {
1001 vorbis_enc_floor *fc = &venc->floors[mapping->floor[mapping->mux[i]]];
1002 uint_fast16_t posts[fc->values];
1003 floor_fit(venc, fc, &venc->coeffs[i * samples], posts, samples);
1004 floor_encode(venc, fc, &pb, posts, &venc->floor[i * samples], samples);
1007 for (i = 0; i < venc->channels * samples; i++)
1008 venc->coeffs[i] /= venc->floor[i];
1010 for (i = 0; i < mapping->coupling_steps; i++) {
1011 float *mag = venc->coeffs + mapping->magnitude[i] * samples;
1012 float *ang = venc->coeffs + mapping->angle[i] * samples;
1014 for (j = 0; j < samples; j++) {
1024 residue_encode(venc, &venc->residues[mapping->residue[mapping->mux[0]]],
1025 &pb, venc->coeffs, samples, venc->channels);
1027 avccontext->coded_frame->pts = venc->sample_count;
1028 venc->sample_count += avccontext->frame_size;
1029 flush_put_bits(&pb);
1030 return put_bits_count(&pb) >> 3;
1034 static av_cold int vorbis_encode_close(AVCodecContext *avccontext)
1036 vorbis_enc_context *venc = avccontext->priv_data;
1039 if (venc->codebooks)
1040 for (i = 0; i < venc->ncodebooks; i++) {
1041 av_freep(&venc->codebooks[i].lens);
1042 av_freep(&venc->codebooks[i].codewords);
1043 av_freep(&venc->codebooks[i].quantlist);
1044 av_freep(&venc->codebooks[i].dimentions);
1045 av_freep(&venc->codebooks[i].pow2);
1047 av_freep(&venc->codebooks);
1050 for (i = 0; i < venc->nfloors; i++) {
1052 if (venc->floors[i].classes)
1053 for (j = 0; j < venc->floors[i].nclasses; j++)
1054 av_freep(&venc->floors[i].classes[j].books);
1055 av_freep(&venc->floors[i].classes);
1056 av_freep(&venc->floors[i].partition_to_class);
1057 av_freep(&venc->floors[i].list);
1059 av_freep(&venc->floors);
1062 for (i = 0; i < venc->nresidues; i++) {
1063 av_freep(&venc->residues[i].books);
1064 av_freep(&venc->residues[i].maxes);
1066 av_freep(&venc->residues);
1069 for (i = 0; i < venc->nmappings; i++) {
1070 av_freep(&venc->mappings[i].mux);
1071 av_freep(&venc->mappings[i].floor);
1072 av_freep(&venc->mappings[i].residue);
1073 av_freep(&venc->mappings[i].magnitude);
1074 av_freep(&venc->mappings[i].angle);
1076 av_freep(&venc->mappings);
1078 av_freep(&venc->modes);
1080 av_freep(&venc->saved);
1081 av_freep(&venc->samples);
1082 av_freep(&venc->floor);
1083 av_freep(&venc->coeffs);
1085 ff_mdct_end(&venc->mdct[0]);
1086 ff_mdct_end(&venc->mdct[1]);
1088 av_freep(&avccontext->coded_frame);
1089 av_freep(&avccontext->extradata);
1094 AVCodec vorbis_encoder = {
1098 sizeof(vorbis_enc_context),
1100 vorbis_encode_frame,
1101 vorbis_encode_close,
1102 .capabilities= CODEC_CAP_DELAY | CODEC_CAP_EXPERIMENTAL,
1103 .sample_fmts = (const enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
1104 .long_name = NULL_IF_CONFIG_SMALL("Vorbis"),