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 #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 void put_codeword(PutBitContext *pb, vorbis_enc_codebook *cb,
144 assert(entry < cb->nentries);
145 assert(cb->lens[entry]);
146 put_bits(pb, cb->lens[entry], cb->codewords[entry]);
149 static int cb_lookup_vals(int lookup, int dimentions, int entries)
152 return ff_vorbis_nth_root(entries, dimentions);
153 else if (lookup == 2)
154 return dimentions *entries;
158 static void ready_codebook(vorbis_enc_codebook *cb)
162 ff_vorbis_len2vlc(cb->lens, cb->codewords, cb->nentries);
165 cb->pow2 = cb->dimentions = NULL;
167 int vals = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries);
168 cb->dimentions = av_malloc(sizeof(float) * cb->nentries * cb->ndimentions);
169 cb->pow2 = av_mallocz(sizeof(float) * cb->nentries);
170 for (i = 0; i < cb->nentries; i++) {
174 for (j = 0; j < cb->ndimentions; j++) {
177 off = (i / div) % vals; // lookup type 1
179 off = i * cb->ndimentions + j; // lookup type 2
181 cb->dimentions[i * cb->ndimentions + j] = last + cb->min + cb->quantlist[off] * cb->delta;
183 last = cb->dimentions[i * cb->ndimentions + j];
184 cb->pow2[i] += cb->dimentions[i * cb->ndimentions + j] * cb->dimentions[i * cb->ndimentions + j];
192 static void ready_residue(vorbis_enc_residue *rc, vorbis_enc_context *venc)
195 assert(rc->type == 2);
196 rc->maxes = av_mallocz(sizeof(float[2]) * rc->classifications);
197 for (i = 0; i < rc->classifications; i++) {
199 vorbis_enc_codebook * cb;
200 for (j = 0; j < 8; j++)
201 if (rc->books[i][j] != -1)
205 cb = &venc->codebooks[rc->books[i][j]];
206 assert(cb->ndimentions >= 2);
209 for (j = 0; j < cb->nentries; j++) {
213 a = fabs(cb->dimentions[j * cb->ndimentions]);
214 if (a > rc->maxes[i][0])
216 a = fabs(cb->dimentions[j * cb->ndimentions + 1]);
217 if (a > rc->maxes[i][1])
222 for (i = 0; i < rc->classifications; i++) {
223 rc->maxes[i][0] += 0.8;
224 rc->maxes[i][1] += 0.8;
228 static void create_vorbis_context(vorbis_enc_context *venc,
229 AVCodecContext *avccontext)
231 vorbis_enc_floor *fc;
232 vorbis_enc_residue *rc;
233 vorbis_enc_mapping *mc;
236 venc->channels = avccontext->channels;
237 venc->sample_rate = avccontext->sample_rate;
238 venc->log2_blocksize[0] = venc->log2_blocksize[1] = 11;
240 venc->ncodebooks = FF_ARRAY_ELEMS(cvectors);
241 venc->codebooks = av_malloc(sizeof(vorbis_enc_codebook) * venc->ncodebooks);
243 // codebook 0..14 - floor1 book, values 0..255
244 // codebook 15 residue masterbook
245 // codebook 16..29 residue
246 for (book = 0; book < venc->ncodebooks; book++) {
247 vorbis_enc_codebook *cb = &venc->codebooks[book];
249 cb->ndimentions = cvectors[book].dim;
250 cb->nentries = cvectors[book].real_len;
251 cb->min = cvectors[book].min;
252 cb->delta = cvectors[book].delta;
253 cb->lookup = cvectors[book].lookup;
256 cb->lens = av_malloc(sizeof(uint8_t) * cb->nentries);
257 cb->codewords = av_malloc(sizeof(uint32_t) * cb->nentries);
258 memcpy(cb->lens, cvectors[book].clens, cvectors[book].len);
259 memset(cb->lens + cvectors[book].len, 0, cb->nentries - cvectors[book].len);
262 vals = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries);
263 cb->quantlist = av_malloc(sizeof(int) * vals);
264 for (i = 0; i < vals; i++)
265 cb->quantlist[i] = cvectors[book].quant[i];
267 cb->quantlist = NULL;
273 venc->floors = av_malloc(sizeof(vorbis_enc_floor) * venc->nfloors);
276 fc = &venc->floors[0];
277 fc->partitions = NUM_FLOOR_PARTITIONS;
278 fc->partition_to_class = av_malloc(sizeof(int) * fc->partitions);
280 for (i = 0; i < fc->partitions; i++) {
281 static const int a[] = {0, 1, 2, 2, 3, 3, 4, 4};
282 fc->partition_to_class[i] = a[i];
283 fc->nclasses = FFMAX(fc->nclasses, fc->partition_to_class[i]);
286 fc->classes = av_malloc(sizeof(vorbis_enc_floor_class) * fc->nclasses);
287 for (i = 0; i < fc->nclasses; i++) {
288 vorbis_enc_floor_class * c = &fc->classes[i];
290 c->dim = floor_classes[i].dim;
291 c->subclass = floor_classes[i].subclass;
292 c->masterbook = floor_classes[i].masterbook;
293 books = (1 << c->subclass);
294 c->books = av_malloc(sizeof(int) * books);
295 for (j = 0; j < books; j++)
296 c->books[j] = floor_classes[i].nbooks[j];
299 fc->rangebits = venc->log2_blocksize[0] - 1;
302 for (i = 0; i < fc->partitions; i++)
303 fc->values += fc->classes[fc->partition_to_class[i]].dim;
305 fc->list = av_malloc(sizeof(vorbis_floor1_entry) * fc->values);
307 fc->list[1].x = 1 << fc->rangebits;
308 for (i = 2; i < fc->values; i++) {
309 static const int a[] = {
310 93, 23,372, 6, 46,186,750, 14, 33, 65,
311 130,260,556, 3, 10, 18, 28, 39, 55, 79,
312 111,158,220,312,464,650,850
314 fc->list[i].x = a[i - 2];
316 ff_vorbis_ready_floor1_list(fc->list, fc->values);
319 venc->residues = av_malloc(sizeof(vorbis_enc_residue) * venc->nresidues);
322 rc = &venc->residues[0];
326 rc->partition_size = 32;
327 rc->classifications = 10;
329 rc->books = av_malloc(sizeof(*rc->books) * rc->classifications);
331 static const int8_t a[10][8] = {
332 { -1, -1, -1, -1, -1, -1, -1, -1, },
333 { -1, -1, 16, -1, -1, -1, -1, -1, },
334 { -1, -1, 17, -1, -1, -1, -1, -1, },
335 { -1, -1, 18, -1, -1, -1, -1, -1, },
336 { -1, -1, 19, -1, -1, -1, -1, -1, },
337 { -1, -1, 20, -1, -1, -1, -1, -1, },
338 { -1, -1, 21, -1, -1, -1, -1, -1, },
339 { 22, 23, -1, -1, -1, -1, -1, -1, },
340 { 24, 25, -1, -1, -1, -1, -1, -1, },
341 { 26, 27, 28, -1, -1, -1, -1, -1, },
343 memcpy(rc->books, a, sizeof a);
345 ready_residue(rc, venc);
348 venc->mappings = av_malloc(sizeof(vorbis_enc_mapping) * venc->nmappings);
351 mc = &venc->mappings[0];
353 mc->mux = av_malloc(sizeof(int) * venc->channels);
354 for (i = 0; i < venc->channels; i++)
356 mc->floor = av_malloc(sizeof(int) * mc->submaps);
357 mc->residue = av_malloc(sizeof(int) * mc->submaps);
358 for (i = 0; i < mc->submaps; i++) {
362 mc->coupling_steps = venc->channels == 2 ? 1 : 0;
363 mc->magnitude = av_malloc(sizeof(int) * mc->coupling_steps);
364 mc->angle = av_malloc(sizeof(int) * mc->coupling_steps);
365 if (mc->coupling_steps) {
366 mc->magnitude[0] = 0;
371 venc->modes = av_malloc(sizeof(vorbis_enc_mode) * venc->nmodes);
374 venc->modes[0].blockflag = 0;
375 venc->modes[0].mapping = 0;
377 venc->have_saved = 0;
378 venc->saved = av_malloc(sizeof(float) * venc->channels * (1 << venc->log2_blocksize[1]) / 2);
379 venc->samples = av_malloc(sizeof(float) * venc->channels * (1 << venc->log2_blocksize[1]));
380 venc->floor = av_malloc(sizeof(float) * venc->channels * (1 << venc->log2_blocksize[1]) / 2);
381 venc->coeffs = av_malloc(sizeof(float) * venc->channels * (1 << venc->log2_blocksize[1]) / 2);
383 venc->win[0] = ff_vorbis_vwin[venc->log2_blocksize[0] - 6];
384 venc->win[1] = ff_vorbis_vwin[venc->log2_blocksize[1] - 6];
386 ff_mdct_init(&venc->mdct[0], venc->log2_blocksize[0], 0, 1.0);
387 ff_mdct_init(&venc->mdct[1], venc->log2_blocksize[1], 0, 1.0);
390 static void put_float(PutBitContext *pb, float f)
394 mant = (int)ldexp(frexp(f, &exp), 20);
400 res |= mant | (exp << 21);
404 static void put_codebook_header(PutBitContext *pb, vorbis_enc_codebook *cb)
409 put_bits(pb, 24, 0x564342); //magic
410 put_bits(pb, 16, cb->ndimentions);
411 put_bits(pb, 24, cb->nentries);
413 for (i = 1; i < cb->nentries; i++)
414 if (cb->lens[i] < cb->lens[i-1])
416 if (i == cb->nentries)
419 put_bits(pb, 1, ordered);
421 int len = cb->lens[0];
422 put_bits(pb, 5, len - 1);
424 while (i < cb->nentries) {
426 for (j = 0; j+i < cb->nentries; j++)
427 if (cb->lens[j+i] != len)
429 put_bits(pb, ilog(cb->nentries - i), j);
435 for (i = 0; i < cb->nentries; i++)
438 if (i != cb->nentries)
440 put_bits(pb, 1, sparse);
442 for (i = 0; i < cb->nentries; i++) {
444 put_bits(pb, 1, !!cb->lens[i]);
446 put_bits(pb, 5, cb->lens[i] - 1);
450 put_bits(pb, 4, cb->lookup);
452 int tmp = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries);
453 int bits = ilog(cb->quantlist[0]);
455 for (i = 1; i < tmp; i++)
456 bits = FFMAX(bits, ilog(cb->quantlist[i]));
458 put_float(pb, cb->min);
459 put_float(pb, cb->delta);
461 put_bits(pb, 4, bits - 1);
462 put_bits(pb, 1, cb->seq_p);
464 for (i = 0; i < tmp; i++)
465 put_bits(pb, bits, cb->quantlist[i]);
469 static void put_floor_header(PutBitContext *pb, vorbis_enc_floor *fc)
473 put_bits(pb, 16, 1); // type, only floor1 is supported
475 put_bits(pb, 5, fc->partitions);
477 for (i = 0; i < fc->partitions; i++)
478 put_bits(pb, 4, fc->partition_to_class[i]);
480 for (i = 0; i < fc->nclasses; i++) {
483 put_bits(pb, 3, fc->classes[i].dim - 1);
484 put_bits(pb, 2, fc->classes[i].subclass);
486 if (fc->classes[i].subclass)
487 put_bits(pb, 8, fc->classes[i].masterbook);
489 books = (1 << fc->classes[i].subclass);
491 for (j = 0; j < books; j++)
492 put_bits(pb, 8, fc->classes[i].books[j] + 1);
495 put_bits(pb, 2, fc->multiplier - 1);
496 put_bits(pb, 4, fc->rangebits);
498 for (i = 2; i < fc->values; i++)
499 put_bits(pb, fc->rangebits, fc->list[i].x);
502 static void put_residue_header(PutBitContext *pb, vorbis_enc_residue *rc)
506 put_bits(pb, 16, rc->type);
508 put_bits(pb, 24, rc->begin);
509 put_bits(pb, 24, rc->end);
510 put_bits(pb, 24, rc->partition_size - 1);
511 put_bits(pb, 6, rc->classifications - 1);
512 put_bits(pb, 8, rc->classbook);
514 for (i = 0; i < rc->classifications; i++) {
516 for (j = 0; j < 8; j++)
517 tmp |= (rc->books[i][j] != -1) << j;
519 put_bits(pb, 3, tmp & 7);
520 put_bits(pb, 1, tmp > 7);
523 put_bits(pb, 5, tmp >> 3);
526 for (i = 0; i < rc->classifications; i++) {
528 for (j = 0; j < 8; j++)
529 if (rc->books[i][j] != -1)
530 put_bits(pb, 8, rc->books[i][j]);
534 static int put_main_header(vorbis_enc_context *venc, uint8_t **out)
538 uint8_t buffer[50000] = {0}, *p = buffer;
539 int buffer_len = sizeof buffer;
542 // identification header
543 init_put_bits(&pb, p, buffer_len);
544 put_bits(&pb, 8, 1); //magic
545 for (i = 0; "vorbis"[i]; i++)
546 put_bits(&pb, 8, "vorbis"[i]);
547 put_bits32(&pb, 0); // version
548 put_bits(&pb, 8, venc->channels);
549 put_bits32(&pb, venc->sample_rate);
550 put_bits32(&pb, 0); // bitrate
551 put_bits32(&pb, 0); // bitrate
552 put_bits32(&pb, 0); // bitrate
553 put_bits(&pb, 4, venc->log2_blocksize[0]);
554 put_bits(&pb, 4, venc->log2_blocksize[1]);
555 put_bits(&pb, 1, 1); // framing
558 hlens[0] = put_bits_count(&pb) >> 3;
559 buffer_len -= hlens[0];
563 init_put_bits(&pb, p, buffer_len);
564 put_bits(&pb, 8, 3); //magic
565 for (i = 0; "vorbis"[i]; i++)
566 put_bits(&pb, 8, "vorbis"[i]);
567 put_bits32(&pb, 0); // vendor length TODO
568 put_bits32(&pb, 0); // amount of comments
569 put_bits(&pb, 1, 1); // framing
572 hlens[1] = put_bits_count(&pb) >> 3;
573 buffer_len -= hlens[1];
577 init_put_bits(&pb, p, buffer_len);
578 put_bits(&pb, 8, 5); //magic
579 for (i = 0; "vorbis"[i]; i++)
580 put_bits(&pb, 8, "vorbis"[i]);
583 put_bits(&pb, 8, venc->ncodebooks - 1);
584 for (i = 0; i < venc->ncodebooks; i++)
585 put_codebook_header(&pb, &venc->codebooks[i]);
587 // time domain, reserved, zero
589 put_bits(&pb, 16, 0);
592 put_bits(&pb, 6, venc->nfloors - 1);
593 for (i = 0; i < venc->nfloors; i++)
594 put_floor_header(&pb, &venc->floors[i]);
597 put_bits(&pb, 6, venc->nresidues - 1);
598 for (i = 0; i < venc->nresidues; i++)
599 put_residue_header(&pb, &venc->residues[i]);
602 put_bits(&pb, 6, venc->nmappings - 1);
603 for (i = 0; i < venc->nmappings; i++) {
604 vorbis_enc_mapping *mc = &venc->mappings[i];
606 put_bits(&pb, 16, 0); // mapping type
608 put_bits(&pb, 1, mc->submaps > 1);
610 put_bits(&pb, 4, mc->submaps - 1);
612 put_bits(&pb, 1, !!mc->coupling_steps);
613 if (mc->coupling_steps) {
614 put_bits(&pb, 8, mc->coupling_steps - 1);
615 for (j = 0; j < mc->coupling_steps; j++) {
616 put_bits(&pb, ilog(venc->channels - 1), mc->magnitude[j]);
617 put_bits(&pb, ilog(venc->channels - 1), mc->angle[j]);
621 put_bits(&pb, 2, 0); // reserved
624 for (j = 0; j < venc->channels; j++)
625 put_bits(&pb, 4, mc->mux[j]);
627 for (j = 0; j < mc->submaps; j++) {
628 put_bits(&pb, 8, 0); // reserved time configuration
629 put_bits(&pb, 8, mc->floor[j]);
630 put_bits(&pb, 8, mc->residue[j]);
635 put_bits(&pb, 6, venc->nmodes - 1);
636 for (i = 0; i < venc->nmodes; i++) {
637 put_bits(&pb, 1, venc->modes[i].blockflag);
638 put_bits(&pb, 16, 0); // reserved window type
639 put_bits(&pb, 16, 0); // reserved transform type
640 put_bits(&pb, 8, venc->modes[i].mapping);
643 put_bits(&pb, 1, 1); // framing
646 hlens[2] = put_bits_count(&pb) >> 3;
648 len = hlens[0] + hlens[1] + hlens[2];
649 p = *out = av_mallocz(64 + len + len/255);
652 p += av_xiphlacing(p, hlens[0]);
653 p += av_xiphlacing(p, hlens[1]);
655 for (i = 0; i < 3; i++) {
656 memcpy(p, buffer + buffer_len, hlens[i]);
658 buffer_len += hlens[i];
664 static float get_floor_average(vorbis_enc_floor * fc, float *coeffs, int i)
666 int begin = fc->list[fc->list[FFMAX(i-1, 0)].sort].x;
667 int end = fc->list[fc->list[FFMIN(i+1, fc->values - 1)].sort].x;
671 for (j = begin; j < end; j++)
672 average += fabs(coeffs[j]);
673 return average / (end - begin);
676 static void floor_fit(vorbis_enc_context *venc, vorbis_enc_floor *fc,
677 float *coeffs, uint16_t *posts, int samples)
679 int range = 255 / fc->multiplier + 1;
681 float tot_average = 0.;
682 float averages[MAX_FLOOR_VALUES];
683 for (i = 0; i < fc->values; i++) {
684 averages[i] = get_floor_average(fc, coeffs, i);
685 tot_average += averages[i];
687 tot_average /= fc->values;
688 tot_average /= venc->quality;
690 for (i = 0; i < fc->values; i++) {
691 int position = fc->list[fc->list[i].sort].x;
692 float average = averages[i];
695 average = sqrt(tot_average * average) * pow(1.25f, position*0.005f); // MAGIC!
696 for (j = 0; j < range - 1; j++)
697 if (ff_vorbis_floor1_inverse_db_table[j * fc->multiplier] > average)
699 posts[fc->list[i].sort] = j;
703 static int render_point(int x0, int y0, int x1, int y1, int x)
705 return y0 + (x - x0) * (y1 - y0) / (x1 - x0);
708 static void floor_encode(vorbis_enc_context *venc, vorbis_enc_floor *fc,
709 PutBitContext *pb, uint16_t *posts,
710 float *floor, int samples)
712 int range = 255 / fc->multiplier + 1;
713 int coded[MAX_FLOOR_VALUES]; // first 2 values are unused
716 put_bits(pb, 1, 1); // non zero
717 put_bits(pb, ilog(range - 1), posts[0]);
718 put_bits(pb, ilog(range - 1), posts[1]);
719 coded[0] = coded[1] = 1;
721 for (i = 2; i < fc->values; i++) {
722 int predicted = render_point(fc->list[fc->list[i].low].x,
723 posts[fc->list[i].low],
724 fc->list[fc->list[i].high].x,
725 posts[fc->list[i].high],
727 int highroom = range - predicted;
728 int lowroom = predicted;
729 int room = FFMIN(highroom, lowroom);
730 if (predicted == posts[i]) {
731 coded[i] = 0; // must be used later as flag!
734 if (!coded[fc->list[i].low ])
735 coded[fc->list[i].low ] = -1;
736 if (!coded[fc->list[i].high])
737 coded[fc->list[i].high] = -1;
739 if (posts[i] > predicted) {
740 if (posts[i] - predicted > room)
741 coded[i] = posts[i] - predicted + lowroom;
743 coded[i] = (posts[i] - predicted) << 1;
745 if (predicted - posts[i] > room)
746 coded[i] = predicted - posts[i] + highroom - 1;
748 coded[i] = ((predicted - posts[i]) << 1) - 1;
753 for (i = 0; i < fc->partitions; i++) {
754 vorbis_enc_floor_class * c = &fc->classes[fc->partition_to_class[i]];
755 int k, cval = 0, csub = 1<<c->subclass;
757 vorbis_enc_codebook * book = &venc->codebooks[c->masterbook];
759 for (k = 0; k < c->dim; k++) {
761 for (l = 0; l < csub; l++) {
763 if (c->books[l] != -1)
764 maxval = venc->codebooks[c->books[l]].nentries;
765 // coded could be -1, but this still works, cause that is 0
766 if (coded[counter + k] < maxval)
771 cshift += c->subclass;
773 put_codeword(pb, book, cval);
775 for (k = 0; k < c->dim; k++) {
776 int book = c->books[cval & (csub-1)];
777 int entry = coded[counter++];
778 cval >>= c->subclass;
783 put_codeword(pb, &venc->codebooks[book], entry);
787 ff_vorbis_floor1_render_list(fc->list, fc->values, posts, coded,
788 fc->multiplier, floor, samples);
791 static float *put_vector(vorbis_enc_codebook *book, PutBitContext *pb,
795 float distance = FLT_MAX;
796 assert(book->dimentions);
797 for (i = 0; i < book->nentries; i++) {
798 float * vec = book->dimentions + i * book->ndimentions, d = book->pow2[i];
802 for (j = 0; j < book->ndimentions; j++)
803 d -= vec[j] * num[j];
809 put_codeword(pb, book, entry);
810 return &book->dimentions[entry * book->ndimentions];
813 static void residue_encode(vorbis_enc_context *venc, vorbis_enc_residue *rc,
814 PutBitContext *pb, float *coeffs, int samples,
817 int pass, i, j, p, k;
818 int psize = rc->partition_size;
819 int partitions = (rc->end - rc->begin) / psize;
820 int channels = (rc->type == 2) ? 1 : real_ch;
821 int classes[MAX_CHANNELS][NUM_RESIDUE_PARTITIONS];
822 int classwords = venc->codebooks[rc->classbook].ndimentions;
824 assert(rc->type == 2);
825 assert(real_ch == 2);
826 for (p = 0; p < partitions; p++) {
827 float max1 = 0., max2 = 0.;
828 int s = rc->begin + p * psize;
829 for (k = s; k < s + psize; k += 2) {
830 max1 = FFMAX(max1, fabs(coeffs[ k / real_ch]));
831 max2 = FFMAX(max2, fabs(coeffs[samples + k / real_ch]));
834 for (i = 0; i < rc->classifications - 1; i++)
835 if (max1 < rc->maxes[i][0] && max2 < rc->maxes[i][1])
840 for (pass = 0; pass < 8; pass++) {
842 while (p < partitions) {
844 for (j = 0; j < channels; j++) {
845 vorbis_enc_codebook * book = &venc->codebooks[rc->classbook];
847 for (i = 0; i < classwords; i++) {
848 entry *= rc->classifications;
849 entry += classes[j][p + i];
851 put_codeword(pb, book, entry);
853 for (i = 0; i < classwords && p < partitions; i++, p++) {
854 for (j = 0; j < channels; j++) {
855 int nbook = rc->books[classes[j][p]][pass];
856 vorbis_enc_codebook * book = &venc->codebooks[nbook];
857 float *buf = coeffs + samples*j + rc->begin + p*psize;
861 assert(rc->type == 0 || rc->type == 2);
862 assert(!(psize % book->ndimentions));
865 for (k = 0; k < psize; k += book->ndimentions) {
866 float *a = put_vector(book, pb, &buf[k]);
868 for (l = 0; l < book->ndimentions; l++)
872 int s = rc->begin + p * psize, a1, b1;
873 a1 = (s % real_ch) * samples;
875 s = real_ch * samples;
876 for (k = 0; k < psize; k += book->ndimentions) {
877 int dim, a2 = a1, b2 = b1;
878 float vec[MAX_CODEBOOK_DIM], *pv = vec;
879 for (dim = book->ndimentions; dim--; ) {
880 *pv++ = coeffs[a2 + b2];
881 if ((a2 += samples) == s) {
886 pv = put_vector(book, pb, vec);
887 for (dim = book->ndimentions; dim--; ) {
888 coeffs[a1 + b1] -= *pv++;
889 if ((a1 += samples) == s) {
902 static int apply_window_and_mdct(vorbis_enc_context *venc, const signed short *audio,
906 const float * win = venc->win[0];
907 int window_len = 1 << (venc->log2_blocksize[0] - 1);
908 float n = (float)(1 << venc->log2_blocksize[0]) / 4.;
911 if (!venc->have_saved && !samples)
914 if (venc->have_saved) {
915 for (channel = 0; channel < venc->channels; channel++)
916 memcpy(venc->samples + channel * window_len * 2,
917 venc->saved + channel * window_len, sizeof(float) * window_len);
919 for (channel = 0; channel < venc->channels; channel++)
920 memset(venc->samples + channel * window_len * 2, 0,
921 sizeof(float) * window_len);
925 for (channel = 0; channel < venc->channels; channel++) {
926 float * offset = venc->samples + channel*window_len*2 + window_len;
928 for (i = 0; i < samples; i++, j += venc->channels)
929 offset[i] = audio[j] / 32768. / n * win[window_len - i - 1];
932 for (channel = 0; channel < venc->channels; channel++)
933 memset(venc->samples + channel * window_len * 2 + window_len,
934 0, sizeof(float) * window_len);
937 for (channel = 0; channel < venc->channels; channel++)
938 venc->mdct[0].mdct_calc(&venc->mdct[0], venc->coeffs + channel * window_len,
939 venc->samples + channel * window_len * 2);
942 for (channel = 0; channel < venc->channels; channel++) {
943 float *offset = venc->saved + channel * window_len;
945 for (i = 0; i < samples; i++, j += venc->channels)
946 offset[i] = audio[j] / 32768. / n * win[i];
948 venc->have_saved = 1;
950 venc->have_saved = 0;
955 static av_cold int vorbis_encode_init(AVCodecContext *avccontext)
957 vorbis_enc_context *venc = avccontext->priv_data;
959 if (avccontext->channels != 2) {
960 av_log(avccontext, AV_LOG_ERROR, "Current FFmpeg Vorbis encoder only supports 2 channels.\n");
964 create_vorbis_context(venc, avccontext);
966 if (avccontext->flags & CODEC_FLAG_QSCALE)
967 venc->quality = avccontext->global_quality / (float)FF_QP2LAMBDA / 10.;
969 venc->quality = 0.03;
970 venc->quality *= venc->quality;
972 avccontext->extradata_size = put_main_header(venc, (uint8_t**)&avccontext->extradata);
974 avccontext->frame_size = 1 << (venc->log2_blocksize[0] - 1);
976 avccontext->coded_frame = avcodec_alloc_frame();
977 avccontext->coded_frame->key_frame = 1;
982 static int vorbis_encode_frame(AVCodecContext *avccontext,
983 unsigned char *packets,
984 int buf_size, void *data)
986 vorbis_enc_context *venc = avccontext->priv_data;
987 const signed short *audio = data;
988 int samples = data ? avccontext->frame_size : 0;
989 vorbis_enc_mode *mode;
990 vorbis_enc_mapping *mapping;
994 if (!apply_window_and_mdct(venc, audio, samples))
996 samples = 1 << (venc->log2_blocksize[0] - 1);
998 init_put_bits(&pb, packets, buf_size);
1000 put_bits(&pb, 1, 0); // magic bit
1002 put_bits(&pb, ilog(venc->nmodes - 1), 0); // 0 bits, the mode
1004 mode = &venc->modes[0];
1005 mapping = &venc->mappings[mode->mapping];
1006 if (mode->blockflag) {
1007 put_bits(&pb, 1, 0);
1008 put_bits(&pb, 1, 0);
1011 for (i = 0; i < venc->channels; i++) {
1012 vorbis_enc_floor *fc = &venc->floors[mapping->floor[mapping->mux[i]]];
1013 uint16_t posts[MAX_FLOOR_VALUES];
1014 floor_fit(venc, fc, &venc->coeffs[i * samples], posts, samples);
1015 floor_encode(venc, fc, &pb, posts, &venc->floor[i * samples], samples);
1018 for (i = 0; i < venc->channels * samples; i++)
1019 venc->coeffs[i] /= venc->floor[i];
1021 for (i = 0; i < mapping->coupling_steps; i++) {
1022 float *mag = venc->coeffs + mapping->magnitude[i] * samples;
1023 float *ang = venc->coeffs + mapping->angle[i] * samples;
1025 for (j = 0; j < samples; j++) {
1035 residue_encode(venc, &venc->residues[mapping->residue[mapping->mux[0]]],
1036 &pb, venc->coeffs, samples, venc->channels);
1038 avccontext->coded_frame->pts = venc->sample_count;
1039 venc->sample_count += avccontext->frame_size;
1040 flush_put_bits(&pb);
1041 return put_bits_count(&pb) >> 3;
1045 static av_cold int vorbis_encode_close(AVCodecContext *avccontext)
1047 vorbis_enc_context *venc = avccontext->priv_data;
1050 if (venc->codebooks)
1051 for (i = 0; i < venc->ncodebooks; i++) {
1052 av_freep(&venc->codebooks[i].lens);
1053 av_freep(&venc->codebooks[i].codewords);
1054 av_freep(&venc->codebooks[i].quantlist);
1055 av_freep(&venc->codebooks[i].dimentions);
1056 av_freep(&venc->codebooks[i].pow2);
1058 av_freep(&venc->codebooks);
1061 for (i = 0; i < venc->nfloors; i++) {
1063 if (venc->floors[i].classes)
1064 for (j = 0; j < venc->floors[i].nclasses; j++)
1065 av_freep(&venc->floors[i].classes[j].books);
1066 av_freep(&venc->floors[i].classes);
1067 av_freep(&venc->floors[i].partition_to_class);
1068 av_freep(&venc->floors[i].list);
1070 av_freep(&venc->floors);
1073 for (i = 0; i < venc->nresidues; i++) {
1074 av_freep(&venc->residues[i].books);
1075 av_freep(&venc->residues[i].maxes);
1077 av_freep(&venc->residues);
1080 for (i = 0; i < venc->nmappings; i++) {
1081 av_freep(&venc->mappings[i].mux);
1082 av_freep(&venc->mappings[i].floor);
1083 av_freep(&venc->mappings[i].residue);
1084 av_freep(&venc->mappings[i].magnitude);
1085 av_freep(&venc->mappings[i].angle);
1087 av_freep(&venc->mappings);
1089 av_freep(&venc->modes);
1091 av_freep(&venc->saved);
1092 av_freep(&venc->samples);
1093 av_freep(&venc->floor);
1094 av_freep(&venc->coeffs);
1096 ff_mdct_end(&venc->mdct[0]);
1097 ff_mdct_end(&venc->mdct[1]);
1099 av_freep(&avccontext->coded_frame);
1100 av_freep(&avccontext->extradata);
1105 AVCodec ff_vorbis_encoder = {
1107 .type = AVMEDIA_TYPE_AUDIO,
1108 .id = CODEC_ID_VORBIS,
1109 .priv_data_size = sizeof(vorbis_enc_context),
1110 .init = vorbis_encode_init,
1111 .encode = vorbis_encode_frame,
1112 .close = vorbis_encode_close,
1113 .capabilities= CODEC_CAP_DELAY | CODEC_CAP_EXPERIMENTAL,
1114 .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
1115 .long_name = NULL_IF_CONFIG_SMALL("Vorbis"),