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;
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,
143 av_assert2(entry >= 0);
144 av_assert2(entry < cb->nentries);
145 av_assert2(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 dimensions, int entries)
155 return ff_vorbis_nth_root(entries, dimensions);
156 else if (lookup == 2)
157 return dimensions *entries;
161 static int ready_codebook(vorbis_enc_codebook *cb)
165 ff_vorbis_len2vlc(cb->lens, cb->codewords, cb->nentries);
168 cb->pow2 = cb->dimensions = NULL;
170 int vals = cb_lookup_vals(cb->lookup, cb->ndimensions, cb->nentries);
171 cb->dimensions = av_malloc(sizeof(float) * cb->nentries * cb->ndimensions);
172 cb->pow2 = av_mallocz(sizeof(float) * cb->nentries);
173 if (!cb->dimensions || !cb->pow2)
174 return AVERROR(ENOMEM);
175 for (i = 0; i < cb->nentries; i++) {
179 for (j = 0; j < cb->ndimensions; j++) {
182 off = (i / div) % vals; // lookup type 1
184 off = i * cb->ndimensions + j; // lookup type 2
186 cb->dimensions[i * cb->ndimensions + j] = last + cb->min + cb->quantlist[off] * cb->delta;
188 last = cb->dimensions[i * cb->ndimensions + j];
189 cb->pow2[i] += cb->dimensions[i * cb->ndimensions + j] * cb->dimensions[i * cb->ndimensions + j];
198 static int ready_residue(vorbis_enc_residue *rc, vorbis_enc_context *venc)
201 av_assert0(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->ndimensions >= 2);
217 for (j = 0; j < cb->nentries; j++) {
221 a = fabs(cb->dimensions[j * cb->ndimensions]);
222 if (a > rc->maxes[i][0])
224 a = fabs(cb->dimensions[j * cb->ndimensions + 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 *avctx)
240 vorbis_enc_floor *fc;
241 vorbis_enc_residue *rc;
242 vorbis_enc_mapping *mc;
245 venc->channels = avctx->channels;
246 venc->sample_rate = avctx->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->ndimensions = 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->ndimensions, 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 if (ff_vorbis_ready_floor1_list(avctx, fc->list, fc->values))
346 venc->residues = av_malloc(sizeof(vorbis_enc_residue) * venc->nresidues);
348 return AVERROR(ENOMEM);
351 rc = &venc->residues[0];
355 rc->partition_size = 32;
356 rc->classifications = 10;
358 rc->books = av_malloc(sizeof(*rc->books) * rc->classifications);
360 return AVERROR(ENOMEM);
362 static const int8_t a[10][8] = {
363 { -1, -1, -1, -1, -1, -1, -1, -1, },
364 { -1, -1, 16, -1, -1, -1, -1, -1, },
365 { -1, -1, 17, -1, -1, -1, -1, -1, },
366 { -1, -1, 18, -1, -1, -1, -1, -1, },
367 { -1, -1, 19, -1, -1, -1, -1, -1, },
368 { -1, -1, 20, -1, -1, -1, -1, -1, },
369 { -1, -1, 21, -1, -1, -1, -1, -1, },
370 { 22, 23, -1, -1, -1, -1, -1, -1, },
371 { 24, 25, -1, -1, -1, -1, -1, -1, },
372 { 26, 27, 28, -1, -1, -1, -1, -1, },
374 memcpy(rc->books, a, sizeof a);
376 if ((ret = ready_residue(rc, venc)) < 0)
380 venc->mappings = av_malloc(sizeof(vorbis_enc_mapping) * venc->nmappings);
382 return AVERROR(ENOMEM);
385 mc = &venc->mappings[0];
387 mc->mux = av_malloc(sizeof(int) * venc->channels);
389 return AVERROR(ENOMEM);
390 for (i = 0; i < venc->channels; i++)
392 mc->floor = av_malloc(sizeof(int) * mc->submaps);
393 mc->residue = av_malloc(sizeof(int) * mc->submaps);
394 if (!mc->floor || !mc->residue)
395 return AVERROR(ENOMEM);
396 for (i = 0; i < mc->submaps; i++) {
400 mc->coupling_steps = venc->channels == 2 ? 1 : 0;
401 mc->magnitude = av_malloc(sizeof(int) * mc->coupling_steps);
402 mc->angle = av_malloc(sizeof(int) * mc->coupling_steps);
403 if (!mc->magnitude || !mc->angle)
404 return AVERROR(ENOMEM);
405 if (mc->coupling_steps) {
406 mc->magnitude[0] = 0;
411 venc->modes = av_malloc(sizeof(vorbis_enc_mode) * venc->nmodes);
413 return AVERROR(ENOMEM);
416 venc->modes[0].blockflag = 0;
417 venc->modes[0].mapping = 0;
419 venc->have_saved = 0;
420 venc->saved = av_malloc(sizeof(float) * venc->channels * (1 << venc->log2_blocksize[1]) / 2);
421 venc->samples = av_malloc(sizeof(float) * venc->channels * (1 << venc->log2_blocksize[1]));
422 venc->floor = av_malloc(sizeof(float) * venc->channels * (1 << venc->log2_blocksize[1]) / 2);
423 venc->coeffs = av_malloc(sizeof(float) * venc->channels * (1 << venc->log2_blocksize[1]) / 2);
424 if (!venc->saved || !venc->samples || !venc->floor || !venc->coeffs)
425 return AVERROR(ENOMEM);
427 venc->win[0] = ff_vorbis_vwin[venc->log2_blocksize[0] - 6];
428 venc->win[1] = ff_vorbis_vwin[venc->log2_blocksize[1] - 6];
430 if ((ret = ff_mdct_init(&venc->mdct[0], venc->log2_blocksize[0], 0, 1.0)) < 0)
432 if ((ret = ff_mdct_init(&venc->mdct[1], venc->log2_blocksize[1], 0, 1.0)) < 0)
438 static void put_float(PutBitContext *pb, float f)
442 mant = (int)ldexp(frexp(f, &exp), 20);
448 res |= mant | (exp << 21);
452 static void put_codebook_header(PutBitContext *pb, vorbis_enc_codebook *cb)
457 put_bits(pb, 24, 0x564342); //magic
458 put_bits(pb, 16, cb->ndimensions);
459 put_bits(pb, 24, cb->nentries);
461 for (i = 1; i < cb->nentries; i++)
462 if (cb->lens[i] < cb->lens[i-1])
464 if (i == cb->nentries)
467 put_bits(pb, 1, ordered);
469 int len = cb->lens[0];
470 put_bits(pb, 5, len - 1);
472 while (i < cb->nentries) {
474 for (j = 0; j+i < cb->nentries; j++)
475 if (cb->lens[j+i] != len)
477 put_bits(pb, ilog(cb->nentries - i), j);
483 for (i = 0; i < cb->nentries; i++)
486 if (i != cb->nentries)
488 put_bits(pb, 1, sparse);
490 for (i = 0; i < cb->nentries; i++) {
492 put_bits(pb, 1, !!cb->lens[i]);
494 put_bits(pb, 5, cb->lens[i] - 1);
498 put_bits(pb, 4, cb->lookup);
500 int tmp = cb_lookup_vals(cb->lookup, cb->ndimensions, cb->nentries);
501 int bits = ilog(cb->quantlist[0]);
503 for (i = 1; i < tmp; i++)
504 bits = FFMAX(bits, ilog(cb->quantlist[i]));
506 put_float(pb, cb->min);
507 put_float(pb, cb->delta);
509 put_bits(pb, 4, bits - 1);
510 put_bits(pb, 1, cb->seq_p);
512 for (i = 0; i < tmp; i++)
513 put_bits(pb, bits, cb->quantlist[i]);
517 static void put_floor_header(PutBitContext *pb, vorbis_enc_floor *fc)
521 put_bits(pb, 16, 1); // type, only floor1 is supported
523 put_bits(pb, 5, fc->partitions);
525 for (i = 0; i < fc->partitions; i++)
526 put_bits(pb, 4, fc->partition_to_class[i]);
528 for (i = 0; i < fc->nclasses; i++) {
531 put_bits(pb, 3, fc->classes[i].dim - 1);
532 put_bits(pb, 2, fc->classes[i].subclass);
534 if (fc->classes[i].subclass)
535 put_bits(pb, 8, fc->classes[i].masterbook);
537 books = (1 << fc->classes[i].subclass);
539 for (j = 0; j < books; j++)
540 put_bits(pb, 8, fc->classes[i].books[j] + 1);
543 put_bits(pb, 2, fc->multiplier - 1);
544 put_bits(pb, 4, fc->rangebits);
546 for (i = 2; i < fc->values; i++)
547 put_bits(pb, fc->rangebits, fc->list[i].x);
550 static void put_residue_header(PutBitContext *pb, vorbis_enc_residue *rc)
554 put_bits(pb, 16, rc->type);
556 put_bits(pb, 24, rc->begin);
557 put_bits(pb, 24, rc->end);
558 put_bits(pb, 24, rc->partition_size - 1);
559 put_bits(pb, 6, rc->classifications - 1);
560 put_bits(pb, 8, rc->classbook);
562 for (i = 0; i < rc->classifications; i++) {
564 for (j = 0; j < 8; j++)
565 tmp |= (rc->books[i][j] != -1) << j;
567 put_bits(pb, 3, tmp & 7);
568 put_bits(pb, 1, tmp > 7);
571 put_bits(pb, 5, tmp >> 3);
574 for (i = 0; i < rc->classifications; i++) {
576 for (j = 0; j < 8; j++)
577 if (rc->books[i][j] != -1)
578 put_bits(pb, 8, rc->books[i][j]);
582 static int put_main_header(vorbis_enc_context *venc, uint8_t **out)
586 uint8_t buffer[50000] = {0}, *p = buffer;
587 int buffer_len = sizeof buffer;
590 // identification header
591 init_put_bits(&pb, p, buffer_len);
592 put_bits(&pb, 8, 1); //magic
593 for (i = 0; "vorbis"[i]; i++)
594 put_bits(&pb, 8, "vorbis"[i]);
595 put_bits32(&pb, 0); // version
596 put_bits(&pb, 8, venc->channels);
597 put_bits32(&pb, venc->sample_rate);
598 put_bits32(&pb, 0); // bitrate
599 put_bits32(&pb, 0); // bitrate
600 put_bits32(&pb, 0); // bitrate
601 put_bits(&pb, 4, venc->log2_blocksize[0]);
602 put_bits(&pb, 4, venc->log2_blocksize[1]);
603 put_bits(&pb, 1, 1); // framing
606 hlens[0] = put_bits_count(&pb) >> 3;
607 buffer_len -= hlens[0];
611 init_put_bits(&pb, p, buffer_len);
612 put_bits(&pb, 8, 3); //magic
613 for (i = 0; "vorbis"[i]; i++)
614 put_bits(&pb, 8, "vorbis"[i]);
615 put_bits32(&pb, 0); // vendor length TODO
616 put_bits32(&pb, 0); // amount of comments
617 put_bits(&pb, 1, 1); // framing
620 hlens[1] = put_bits_count(&pb) >> 3;
621 buffer_len -= hlens[1];
625 init_put_bits(&pb, p, buffer_len);
626 put_bits(&pb, 8, 5); //magic
627 for (i = 0; "vorbis"[i]; i++)
628 put_bits(&pb, 8, "vorbis"[i]);
631 put_bits(&pb, 8, venc->ncodebooks - 1);
632 for (i = 0; i < venc->ncodebooks; i++)
633 put_codebook_header(&pb, &venc->codebooks[i]);
635 // time domain, reserved, zero
637 put_bits(&pb, 16, 0);
640 put_bits(&pb, 6, venc->nfloors - 1);
641 for (i = 0; i < venc->nfloors; i++)
642 put_floor_header(&pb, &venc->floors[i]);
645 put_bits(&pb, 6, venc->nresidues - 1);
646 for (i = 0; i < venc->nresidues; i++)
647 put_residue_header(&pb, &venc->residues[i]);
650 put_bits(&pb, 6, venc->nmappings - 1);
651 for (i = 0; i < venc->nmappings; i++) {
652 vorbis_enc_mapping *mc = &venc->mappings[i];
654 put_bits(&pb, 16, 0); // mapping type
656 put_bits(&pb, 1, mc->submaps > 1);
658 put_bits(&pb, 4, mc->submaps - 1);
660 put_bits(&pb, 1, !!mc->coupling_steps);
661 if (mc->coupling_steps) {
662 put_bits(&pb, 8, mc->coupling_steps - 1);
663 for (j = 0; j < mc->coupling_steps; j++) {
664 put_bits(&pb, ilog(venc->channels - 1), mc->magnitude[j]);
665 put_bits(&pb, ilog(venc->channels - 1), mc->angle[j]);
669 put_bits(&pb, 2, 0); // reserved
672 for (j = 0; j < venc->channels; j++)
673 put_bits(&pb, 4, mc->mux[j]);
675 for (j = 0; j < mc->submaps; j++) {
676 put_bits(&pb, 8, 0); // reserved time configuration
677 put_bits(&pb, 8, mc->floor[j]);
678 put_bits(&pb, 8, mc->residue[j]);
683 put_bits(&pb, 6, venc->nmodes - 1);
684 for (i = 0; i < venc->nmodes; i++) {
685 put_bits(&pb, 1, venc->modes[i].blockflag);
686 put_bits(&pb, 16, 0); // reserved window type
687 put_bits(&pb, 16, 0); // reserved transform type
688 put_bits(&pb, 8, venc->modes[i].mapping);
691 put_bits(&pb, 1, 1); // framing
694 hlens[2] = put_bits_count(&pb) >> 3;
696 len = hlens[0] + hlens[1] + hlens[2];
697 p = *out = av_mallocz(64 + len + len/255);
699 return AVERROR(ENOMEM);
702 p += av_xiphlacing(p, hlens[0]);
703 p += av_xiphlacing(p, hlens[1]);
705 for (i = 0; i < 3; i++) {
706 memcpy(p, buffer + buffer_len, hlens[i]);
708 buffer_len += hlens[i];
714 static float get_floor_average(vorbis_enc_floor * fc, float *coeffs, int i)
716 int begin = fc->list[fc->list[FFMAX(i-1, 0)].sort].x;
717 int end = fc->list[fc->list[FFMIN(i+1, fc->values - 1)].sort].x;
721 for (j = begin; j < end; j++)
722 average += fabs(coeffs[j]);
723 return average / (end - begin);
726 static void floor_fit(vorbis_enc_context *venc, vorbis_enc_floor *fc,
727 float *coeffs, uint16_t *posts, int samples)
729 int range = 255 / fc->multiplier + 1;
731 float tot_average = 0.0;
732 float averages[MAX_FLOOR_VALUES];
733 for (i = 0; i < fc->values; i++) {
734 averages[i] = get_floor_average(fc, coeffs, i);
735 tot_average += averages[i];
737 tot_average /= fc->values;
738 tot_average /= venc->quality;
740 for (i = 0; i < fc->values; i++) {
741 int position = fc->list[fc->list[i].sort].x;
742 float average = averages[i];
745 average = sqrt(tot_average * average) * pow(1.25f, position*0.005f); // MAGIC!
746 for (j = 0; j < range - 1; j++)
747 if (ff_vorbis_floor1_inverse_db_table[j * fc->multiplier] > average)
749 posts[fc->list[i].sort] = j;
753 static int render_point(int x0, int y0, int x1, int y1, int x)
755 return y0 + (x - x0) * (y1 - y0) / (x1 - x0);
758 static int floor_encode(vorbis_enc_context *venc, vorbis_enc_floor *fc,
759 PutBitContext *pb, uint16_t *posts,
760 float *floor, int samples)
762 int range = 255 / fc->multiplier + 1;
763 int coded[MAX_FLOOR_VALUES]; // first 2 values are unused
766 if (pb->size_in_bits - put_bits_count(pb) < 1 + 2 * ilog(range - 1))
767 return AVERROR(EINVAL);
768 put_bits(pb, 1, 1); // non zero
769 put_bits(pb, ilog(range - 1), posts[0]);
770 put_bits(pb, ilog(range - 1), posts[1]);
771 coded[0] = coded[1] = 1;
773 for (i = 2; i < fc->values; i++) {
774 int predicted = render_point(fc->list[fc->list[i].low].x,
775 posts[fc->list[i].low],
776 fc->list[fc->list[i].high].x,
777 posts[fc->list[i].high],
779 int highroom = range - predicted;
780 int lowroom = predicted;
781 int room = FFMIN(highroom, lowroom);
782 if (predicted == posts[i]) {
783 coded[i] = 0; // must be used later as flag!
786 if (!coded[fc->list[i].low ])
787 coded[fc->list[i].low ] = -1;
788 if (!coded[fc->list[i].high])
789 coded[fc->list[i].high] = -1;
791 if (posts[i] > predicted) {
792 if (posts[i] - predicted > room)
793 coded[i] = posts[i] - predicted + lowroom;
795 coded[i] = (posts[i] - predicted) << 1;
797 if (predicted - posts[i] > room)
798 coded[i] = predicted - posts[i] + highroom - 1;
800 coded[i] = ((predicted - posts[i]) << 1) - 1;
805 for (i = 0; i < fc->partitions; i++) {
806 vorbis_enc_floor_class * c = &fc->classes[fc->partition_to_class[i]];
807 int k, cval = 0, csub = 1<<c->subclass;
809 vorbis_enc_codebook * book = &venc->codebooks[c->masterbook];
811 for (k = 0; k < c->dim; k++) {
813 for (l = 0; l < csub; l++) {
815 if (c->books[l] != -1)
816 maxval = venc->codebooks[c->books[l]].nentries;
817 // coded could be -1, but this still works, cause that is 0
818 if (coded[counter + k] < maxval)
823 cshift += c->subclass;
825 if (put_codeword(pb, book, cval))
826 return AVERROR(EINVAL);
828 for (k = 0; k < c->dim; k++) {
829 int book = c->books[cval & (csub-1)];
830 int entry = coded[counter++];
831 cval >>= c->subclass;
836 if (put_codeword(pb, &venc->codebooks[book], entry))
837 return AVERROR(EINVAL);
841 ff_vorbis_floor1_render_list(fc->list, fc->values, posts, coded,
842 fc->multiplier, floor, samples);
847 static float *put_vector(vorbis_enc_codebook *book, PutBitContext *pb,
851 float distance = FLT_MAX;
852 assert(book->dimensions);
853 for (i = 0; i < book->nentries; i++) {
854 float * vec = book->dimensions + i * book->ndimensions, d = book->pow2[i];
858 for (j = 0; j < book->ndimensions; j++)
859 d -= vec[j] * num[j];
865 if (put_codeword(pb, book, entry))
867 return &book->dimensions[entry * book->ndimensions];
870 static int residue_encode(vorbis_enc_context *venc, vorbis_enc_residue *rc,
871 PutBitContext *pb, float *coeffs, int samples,
874 int pass, i, j, p, k;
875 int psize = rc->partition_size;
876 int partitions = (rc->end - rc->begin) / psize;
877 int channels = (rc->type == 2) ? 1 : real_ch;
878 int classes[MAX_CHANNELS][NUM_RESIDUE_PARTITIONS];
879 int classwords = venc->codebooks[rc->classbook].ndimensions;
881 av_assert0(rc->type == 2);
882 av_assert0(real_ch == 2);
883 for (p = 0; p < partitions; p++) {
884 float max1 = 0.0, max2 = 0.0;
885 int s = rc->begin + p * psize;
886 for (k = s; k < s + psize; k += 2) {
887 max1 = FFMAX(max1, fabs(coeffs[ k / real_ch]));
888 max2 = FFMAX(max2, fabs(coeffs[samples + k / real_ch]));
891 for (i = 0; i < rc->classifications - 1; i++)
892 if (max1 < rc->maxes[i][0] && max2 < rc->maxes[i][1])
897 for (pass = 0; pass < 8; pass++) {
899 while (p < partitions) {
901 for (j = 0; j < channels; j++) {
902 vorbis_enc_codebook * book = &venc->codebooks[rc->classbook];
904 for (i = 0; i < classwords; i++) {
905 entry *= rc->classifications;
906 entry += classes[j][p + i];
908 if (put_codeword(pb, book, entry))
909 return AVERROR(EINVAL);
911 for (i = 0; i < classwords && p < partitions; i++, p++) {
912 for (j = 0; j < channels; j++) {
913 int nbook = rc->books[classes[j][p]][pass];
914 vorbis_enc_codebook * book = &venc->codebooks[nbook];
915 float *buf = coeffs + samples*j + rc->begin + p*psize;
919 assert(rc->type == 0 || rc->type == 2);
920 assert(!(psize % book->ndimensions));
923 for (k = 0; k < psize; k += book->ndimensions) {
925 float *a = put_vector(book, pb, &buf[k]);
927 return AVERROR(EINVAL);
928 for (l = 0; l < book->ndimensions; l++)
932 int s = rc->begin + p * psize, a1, b1;
933 a1 = (s % real_ch) * samples;
935 s = real_ch * samples;
936 for (k = 0; k < psize; k += book->ndimensions) {
937 int dim, a2 = a1, b2 = b1;
938 float vec[MAX_CODEBOOK_DIM], *pv = vec;
939 for (dim = book->ndimensions; dim--; ) {
940 *pv++ = coeffs[a2 + b2];
941 if ((a2 += samples) == s) {
946 pv = put_vector(book, pb, vec);
948 return AVERROR(EINVAL);
949 for (dim = book->ndimensions; dim--; ) {
950 coeffs[a1 + b1] -= *pv++;
951 if ((a1 += samples) == s) {
965 static int apply_window_and_mdct(vorbis_enc_context *venc,
966 float **audio, int samples)
969 const float * win = venc->win[0];
970 int window_len = 1 << (venc->log2_blocksize[0] - 1);
971 float n = (float)(1 << venc->log2_blocksize[0]) / 4.0;
974 if (!venc->have_saved && !samples)
977 if (venc->have_saved) {
978 for (channel = 0; channel < venc->channels; channel++)
979 memcpy(venc->samples + channel * window_len * 2,
980 venc->saved + channel * window_len, sizeof(float) * window_len);
982 for (channel = 0; channel < venc->channels; channel++)
983 memset(venc->samples + channel * window_len * 2, 0,
984 sizeof(float) * window_len);
988 for (channel = 0; channel < venc->channels; channel++) {
989 float * offset = venc->samples + channel*window_len*2 + window_len;
990 for (i = 0; i < samples; i++)
991 offset[i] = audio[channel][i] / 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;
1006 for (i = 0; i < samples; i++)
1007 offset[i] = audio[channel][i] / n * win[i];
1009 venc->have_saved = 1;
1011 venc->have_saved = 0;
1016 static int vorbis_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
1017 const AVFrame *frame, int *got_packet_ptr)
1019 vorbis_enc_context *venc = avctx->priv_data;
1020 float **audio = frame ? (float **)frame->extended_data : NULL;
1021 int samples = frame ? frame->nb_samples : 0;
1022 vorbis_enc_mode *mode;
1023 vorbis_enc_mapping *mapping;
1027 if (!apply_window_and_mdct(venc, audio, samples))
1029 samples = 1 << (venc->log2_blocksize[0] - 1);
1031 if ((ret = ff_alloc_packet2(avctx, avpkt, 8192)) < 0)
1034 init_put_bits(&pb, avpkt->data, avpkt->size);
1036 if (pb.size_in_bits - put_bits_count(&pb) < 1 + ilog(venc->nmodes - 1)) {
1037 av_log(avctx, 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(avctx, 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(avctx, AV_LOG_ERROR, "output buffer is too small\n");
1082 return AVERROR(EINVAL);
1085 flush_put_bits(&pb);
1086 avpkt->size = put_bits_count(&pb) >> 3;
1088 avpkt->duration = ff_samples_to_time_base(avctx, avctx->frame_size);
1090 if (frame->pts != AV_NOPTS_VALUE)
1091 avpkt->pts = ff_samples_to_time_base(avctx, frame->pts);
1093 avpkt->pts = venc->next_pts;
1094 if (avpkt->pts != AV_NOPTS_VALUE)
1095 venc->next_pts = avpkt->pts + avpkt->duration;
1097 *got_packet_ptr = 1;
1102 static av_cold int vorbis_encode_close(AVCodecContext *avctx)
1104 vorbis_enc_context *venc = avctx->priv_data;
1107 if (venc->codebooks)
1108 for (i = 0; i < venc->ncodebooks; i++) {
1109 av_freep(&venc->codebooks[i].lens);
1110 av_freep(&venc->codebooks[i].codewords);
1111 av_freep(&venc->codebooks[i].quantlist);
1112 av_freep(&venc->codebooks[i].dimensions);
1113 av_freep(&venc->codebooks[i].pow2);
1115 av_freep(&venc->codebooks);
1118 for (i = 0; i < venc->nfloors; i++) {
1120 if (venc->floors[i].classes)
1121 for (j = 0; j < venc->floors[i].nclasses; j++)
1122 av_freep(&venc->floors[i].classes[j].books);
1123 av_freep(&venc->floors[i].classes);
1124 av_freep(&venc->floors[i].partition_to_class);
1125 av_freep(&venc->floors[i].list);
1127 av_freep(&venc->floors);
1130 for (i = 0; i < venc->nresidues; i++) {
1131 av_freep(&venc->residues[i].books);
1132 av_freep(&venc->residues[i].maxes);
1134 av_freep(&venc->residues);
1137 for (i = 0; i < venc->nmappings; i++) {
1138 av_freep(&venc->mappings[i].mux);
1139 av_freep(&venc->mappings[i].floor);
1140 av_freep(&venc->mappings[i].residue);
1141 av_freep(&venc->mappings[i].magnitude);
1142 av_freep(&venc->mappings[i].angle);
1144 av_freep(&venc->mappings);
1146 av_freep(&venc->modes);
1148 av_freep(&venc->saved);
1149 av_freep(&venc->samples);
1150 av_freep(&venc->floor);
1151 av_freep(&venc->coeffs);
1153 ff_mdct_end(&venc->mdct[0]);
1154 ff_mdct_end(&venc->mdct[1]);
1156 av_freep(&avctx->extradata);
1161 static av_cold int vorbis_encode_init(AVCodecContext *avctx)
1163 vorbis_enc_context *venc = avctx->priv_data;
1166 if (avctx->channels != 2) {
1167 av_log(avctx, AV_LOG_ERROR, "Current FFmpeg Vorbis encoder only supports 2 channels.\n");
1171 if ((ret = create_vorbis_context(venc, avctx)) < 0)
1174 avctx->bit_rate = 0;
1175 if (avctx->flags & CODEC_FLAG_QSCALE)
1176 venc->quality = avctx->global_quality / (float)FF_QP2LAMBDA;
1179 venc->quality *= venc->quality;
1181 if ((ret = put_main_header(venc, (uint8_t**)&avctx->extradata)) < 0)
1183 avctx->extradata_size = ret;
1185 avctx->frame_size = 1 << (venc->log2_blocksize[0] - 1);
1189 vorbis_encode_close(avctx);
1193 AVCodec ff_vorbis_encoder = {
1195 .long_name = NULL_IF_CONFIG_SMALL("Vorbis"),
1196 .type = AVMEDIA_TYPE_AUDIO,
1197 .id = AV_CODEC_ID_VORBIS,
1198 .priv_data_size = sizeof(vorbis_enc_context),
1199 .init = vorbis_encode_init,
1200 .encode2 = vorbis_encode_frame,
1201 .close = vorbis_encode_close,
1202 .capabilities = CODEC_CAP_DELAY | CODEC_CAP_EXPERIMENTAL,
1203 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLTP,
1204 AV_SAMPLE_FMT_NONE },