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>
28 #include "libavutil/float_dsp.h"
35 #include "vorbis_enc_data.h"
37 #include "audio_frame_queue.h"
38 #include "libavfilter/bufferqueue.h"
40 #define BITSTREAM_WRITER_LE
46 typedef struct vorbis_enc_codebook {
58 } vorbis_enc_codebook;
60 typedef struct vorbis_enc_floor_class {
65 } vorbis_enc_floor_class;
67 typedef struct vorbis_enc_floor {
69 int *partition_to_class;
71 vorbis_enc_floor_class *classes;
75 vorbis_floor1_entry *list;
78 typedef struct vorbis_enc_residue {
89 typedef struct vorbis_enc_mapping {
99 typedef struct vorbis_enc_mode {
104 typedef struct vorbis_enc_context {
107 int log2_blocksize[2];
113 float *floor; // also used for tmp values for mdct
114 float *coeffs; // also used for residue after floor
118 struct FFBufQueue bufqueue;
121 vorbis_enc_codebook *codebooks;
124 vorbis_enc_floor *floors;
127 vorbis_enc_residue *residues;
130 vorbis_enc_mapping *mappings;
133 vorbis_enc_mode *modes;
137 AVFloatDSPContext *fdsp;
138 } vorbis_enc_context;
140 #define MAX_CHANNELS 2
141 #define MAX_CODEBOOK_DIM 8
143 #define MAX_FLOOR_CLASS_DIM 4
144 #define NUM_FLOOR_PARTITIONS 8
145 #define MAX_FLOOR_VALUES (MAX_FLOOR_CLASS_DIM*NUM_FLOOR_PARTITIONS+2)
147 #define RESIDUE_SIZE 1600
148 #define RESIDUE_PART_SIZE 32
149 #define NUM_RESIDUE_PARTITIONS (RESIDUE_SIZE/RESIDUE_PART_SIZE)
151 static inline int put_codeword(PutBitContext *pb, vorbis_enc_codebook *cb,
154 av_assert2(entry >= 0);
155 av_assert2(entry < cb->nentries);
156 av_assert2(cb->lens[entry]);
157 if (pb->size_in_bits - put_bits_count(pb) < cb->lens[entry])
158 return AVERROR(EINVAL);
159 put_bits(pb, cb->lens[entry], cb->codewords[entry]);
163 static int cb_lookup_vals(int lookup, int dimensions, int entries)
166 return ff_vorbis_nth_root(entries, dimensions);
167 else if (lookup == 2)
168 return dimensions *entries;
172 static int ready_codebook(vorbis_enc_codebook *cb)
176 ff_vorbis_len2vlc(cb->lens, cb->codewords, cb->nentries);
179 cb->pow2 = cb->dimensions = NULL;
181 int vals = cb_lookup_vals(cb->lookup, cb->ndimensions, cb->nentries);
182 cb->dimensions = av_malloc_array(cb->nentries, sizeof(float) * cb->ndimensions);
183 cb->pow2 = av_mallocz_array(cb->nentries, sizeof(float));
184 if (!cb->dimensions || !cb->pow2)
185 return AVERROR(ENOMEM);
186 for (i = 0; i < cb->nentries; i++) {
190 for (j = 0; j < cb->ndimensions; j++) {
193 off = (i / div) % vals; // lookup type 1
195 off = i * cb->ndimensions + j; // lookup type 2
197 cb->dimensions[i * cb->ndimensions + j] = last + cb->min + cb->quantlist[off] * cb->delta;
199 last = cb->dimensions[i * cb->ndimensions + j];
200 cb->pow2[i] += cb->dimensions[i * cb->ndimensions + j] * cb->dimensions[i * cb->ndimensions + j];
209 static int ready_residue(vorbis_enc_residue *rc, vorbis_enc_context *venc)
212 av_assert0(rc->type == 2);
213 rc->maxes = av_mallocz_array(rc->classifications, sizeof(float[2]));
215 return AVERROR(ENOMEM);
216 for (i = 0; i < rc->classifications; i++) {
218 vorbis_enc_codebook * cb;
219 for (j = 0; j < 8; j++)
220 if (rc->books[i][j] != -1)
224 cb = &venc->codebooks[rc->books[i][j]];
225 assert(cb->ndimensions >= 2);
228 for (j = 0; j < cb->nentries; j++) {
232 a = fabs(cb->dimensions[j * cb->ndimensions]);
233 if (a > rc->maxes[i][0])
235 a = fabs(cb->dimensions[j * cb->ndimensions + 1]);
236 if (a > rc->maxes[i][1])
241 for (i = 0; i < rc->classifications; i++) {
242 rc->maxes[i][0] += 0.8;
243 rc->maxes[i][1] += 0.8;
248 static av_cold int dsp_init(AVCodecContext *avctx, vorbis_enc_context *venc)
252 venc->fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
254 return AVERROR(ENOMEM);
257 venc->win[0] = ff_vorbis_vwin[venc->log2_blocksize[0] - 6];
258 venc->win[1] = ff_vorbis_vwin[venc->log2_blocksize[1] - 6];
260 if ((ret = ff_mdct_init(&venc->mdct[0], venc->log2_blocksize[0], 0, 1.0)) < 0)
262 if ((ret = ff_mdct_init(&venc->mdct[1], venc->log2_blocksize[1], 0, 1.0)) < 0)
268 static int create_vorbis_context(vorbis_enc_context *venc,
269 AVCodecContext *avctx)
271 vorbis_enc_floor *fc;
272 vorbis_enc_residue *rc;
273 vorbis_enc_mapping *mc;
276 venc->channels = avctx->channels;
277 venc->sample_rate = avctx->sample_rate;
278 venc->log2_blocksize[0] = venc->log2_blocksize[1] = 11;
280 venc->ncodebooks = FF_ARRAY_ELEMS(cvectors);
281 venc->codebooks = av_malloc(sizeof(vorbis_enc_codebook) * venc->ncodebooks);
282 if (!venc->codebooks)
283 return AVERROR(ENOMEM);
285 // codebook 0..14 - floor1 book, values 0..255
286 // codebook 15 residue masterbook
287 // codebook 16..29 residue
288 for (book = 0; book < venc->ncodebooks; book++) {
289 vorbis_enc_codebook *cb = &venc->codebooks[book];
291 cb->ndimensions = cvectors[book].dim;
292 cb->nentries = cvectors[book].real_len;
293 cb->min = cvectors[book].min;
294 cb->delta = cvectors[book].delta;
295 cb->lookup = cvectors[book].lookup;
298 cb->lens = av_malloc_array(cb->nentries, sizeof(uint8_t));
299 cb->codewords = av_malloc_array(cb->nentries, sizeof(uint32_t));
300 if (!cb->lens || !cb->codewords)
301 return AVERROR(ENOMEM);
302 memcpy(cb->lens, cvectors[book].clens, cvectors[book].len);
303 memset(cb->lens + cvectors[book].len, 0, cb->nentries - cvectors[book].len);
306 vals = cb_lookup_vals(cb->lookup, cb->ndimensions, cb->nentries);
307 cb->quantlist = av_malloc_array(vals, sizeof(int));
309 return AVERROR(ENOMEM);
310 for (i = 0; i < vals; i++)
311 cb->quantlist[i] = cvectors[book].quant[i];
313 cb->quantlist = NULL;
315 if ((ret = ready_codebook(cb)) < 0)
320 venc->floors = av_malloc(sizeof(vorbis_enc_floor) * venc->nfloors);
322 return AVERROR(ENOMEM);
325 fc = &venc->floors[0];
326 fc->partitions = NUM_FLOOR_PARTITIONS;
327 fc->partition_to_class = av_malloc(sizeof(int) * fc->partitions);
328 if (!fc->partition_to_class)
329 return AVERROR(ENOMEM);
331 for (i = 0; i < fc->partitions; i++) {
332 static const int a[] = {0, 1, 2, 2, 3, 3, 4, 4};
333 fc->partition_to_class[i] = a[i];
334 fc->nclasses = FFMAX(fc->nclasses, fc->partition_to_class[i]);
337 fc->classes = av_malloc_array(fc->nclasses, sizeof(vorbis_enc_floor_class));
339 return AVERROR(ENOMEM);
340 for (i = 0; i < fc->nclasses; i++) {
341 vorbis_enc_floor_class * c = &fc->classes[i];
343 c->dim = floor_classes[i].dim;
344 c->subclass = floor_classes[i].subclass;
345 c->masterbook = floor_classes[i].masterbook;
346 books = (1 << c->subclass);
347 c->books = av_malloc_array(books, sizeof(int));
349 return AVERROR(ENOMEM);
350 for (j = 0; j < books; j++)
351 c->books[j] = floor_classes[i].nbooks[j];
354 fc->rangebits = venc->log2_blocksize[0] - 1;
357 for (i = 0; i < fc->partitions; i++)
358 fc->values += fc->classes[fc->partition_to_class[i]].dim;
360 fc->list = av_malloc_array(fc->values, sizeof(vorbis_floor1_entry));
362 return AVERROR(ENOMEM);
364 fc->list[1].x = 1 << fc->rangebits;
365 for (i = 2; i < fc->values; i++) {
366 static const int a[] = {
367 93, 23,372, 6, 46,186,750, 14, 33, 65,
368 130,260,556, 3, 10, 18, 28, 39, 55, 79,
369 111,158,220,312,464,650,850
371 fc->list[i].x = a[i - 2];
373 if (ff_vorbis_ready_floor1_list(avctx, fc->list, fc->values))
377 venc->residues = av_malloc(sizeof(vorbis_enc_residue) * venc->nresidues);
379 return AVERROR(ENOMEM);
382 rc = &venc->residues[0];
386 rc->partition_size = 32;
387 rc->classifications = 10;
389 rc->books = av_malloc(sizeof(*rc->books) * rc->classifications);
391 return AVERROR(ENOMEM);
393 static const int8_t a[10][8] = {
394 { -1, -1, -1, -1, -1, -1, -1, -1, },
395 { -1, -1, 16, -1, -1, -1, -1, -1, },
396 { -1, -1, 17, -1, -1, -1, -1, -1, },
397 { -1, -1, 18, -1, -1, -1, -1, -1, },
398 { -1, -1, 19, -1, -1, -1, -1, -1, },
399 { -1, -1, 20, -1, -1, -1, -1, -1, },
400 { -1, -1, 21, -1, -1, -1, -1, -1, },
401 { 22, 23, -1, -1, -1, -1, -1, -1, },
402 { 24, 25, -1, -1, -1, -1, -1, -1, },
403 { 26, 27, 28, -1, -1, -1, -1, -1, },
405 memcpy(rc->books, a, sizeof a);
407 if ((ret = ready_residue(rc, venc)) < 0)
411 venc->mappings = av_malloc(sizeof(vorbis_enc_mapping) * venc->nmappings);
413 return AVERROR(ENOMEM);
416 mc = &venc->mappings[0];
418 mc->mux = av_malloc(sizeof(int) * venc->channels);
420 return AVERROR(ENOMEM);
421 for (i = 0; i < venc->channels; i++)
423 mc->floor = av_malloc(sizeof(int) * mc->submaps);
424 mc->residue = av_malloc(sizeof(int) * mc->submaps);
425 if (!mc->floor || !mc->residue)
426 return AVERROR(ENOMEM);
427 for (i = 0; i < mc->submaps; i++) {
431 mc->coupling_steps = venc->channels == 2 ? 1 : 0;
432 mc->magnitude = av_malloc(sizeof(int) * mc->coupling_steps);
433 mc->angle = av_malloc(sizeof(int) * mc->coupling_steps);
434 if (!mc->magnitude || !mc->angle)
435 return AVERROR(ENOMEM);
436 if (mc->coupling_steps) {
437 mc->magnitude[0] = 0;
442 venc->modes = av_malloc(sizeof(vorbis_enc_mode) * venc->nmodes);
444 return AVERROR(ENOMEM);
447 venc->modes[0].blockflag = 0;
448 venc->modes[0].mapping = 0;
450 venc->have_saved = 0;
451 venc->saved = av_malloc_array(sizeof(float) * venc->channels, (1 << venc->log2_blocksize[1]) / 2);
452 venc->samples = av_malloc_array(sizeof(float) * venc->channels, (1 << venc->log2_blocksize[1]));
453 venc->floor = av_malloc_array(sizeof(float) * venc->channels, (1 << venc->log2_blocksize[1]) / 2);
454 venc->coeffs = av_malloc_array(sizeof(float) * venc->channels, (1 << venc->log2_blocksize[1]) / 2);
455 if (!venc->saved || !venc->samples || !venc->floor || !venc->coeffs)
456 return AVERROR(ENOMEM);
458 if ((ret = dsp_init(avctx, venc)) < 0)
464 static void put_float(PutBitContext *pb, float f)
468 mant = (int)ldexp(frexp(f, &exp), 20);
474 res |= mant | (exp << 21);
478 static void put_codebook_header(PutBitContext *pb, vorbis_enc_codebook *cb)
483 put_bits(pb, 24, 0x564342); //magic
484 put_bits(pb, 16, cb->ndimensions);
485 put_bits(pb, 24, cb->nentries);
487 for (i = 1; i < cb->nentries; i++)
488 if (cb->lens[i] < cb->lens[i-1])
490 if (i == cb->nentries)
493 put_bits(pb, 1, ordered);
495 int len = cb->lens[0];
496 put_bits(pb, 5, len - 1);
498 while (i < cb->nentries) {
500 for (j = 0; j+i < cb->nentries; j++)
501 if (cb->lens[j+i] != len)
503 put_bits(pb, ilog(cb->nentries - i), j);
509 for (i = 0; i < cb->nentries; i++)
512 if (i != cb->nentries)
514 put_bits(pb, 1, sparse);
516 for (i = 0; i < cb->nentries; i++) {
518 put_bits(pb, 1, !!cb->lens[i]);
520 put_bits(pb, 5, cb->lens[i] - 1);
524 put_bits(pb, 4, cb->lookup);
526 int tmp = cb_lookup_vals(cb->lookup, cb->ndimensions, cb->nentries);
527 int bits = ilog(cb->quantlist[0]);
529 for (i = 1; i < tmp; i++)
530 bits = FFMAX(bits, ilog(cb->quantlist[i]));
532 put_float(pb, cb->min);
533 put_float(pb, cb->delta);
535 put_bits(pb, 4, bits - 1);
536 put_bits(pb, 1, cb->seq_p);
538 for (i = 0; i < tmp; i++)
539 put_bits(pb, bits, cb->quantlist[i]);
543 static void put_floor_header(PutBitContext *pb, vorbis_enc_floor *fc)
547 put_bits(pb, 16, 1); // type, only floor1 is supported
549 put_bits(pb, 5, fc->partitions);
551 for (i = 0; i < fc->partitions; i++)
552 put_bits(pb, 4, fc->partition_to_class[i]);
554 for (i = 0; i < fc->nclasses; i++) {
557 put_bits(pb, 3, fc->classes[i].dim - 1);
558 put_bits(pb, 2, fc->classes[i].subclass);
560 if (fc->classes[i].subclass)
561 put_bits(pb, 8, fc->classes[i].masterbook);
563 books = (1 << fc->classes[i].subclass);
565 for (j = 0; j < books; j++)
566 put_bits(pb, 8, fc->classes[i].books[j] + 1);
569 put_bits(pb, 2, fc->multiplier - 1);
570 put_bits(pb, 4, fc->rangebits);
572 for (i = 2; i < fc->values; i++)
573 put_bits(pb, fc->rangebits, fc->list[i].x);
576 static void put_residue_header(PutBitContext *pb, vorbis_enc_residue *rc)
580 put_bits(pb, 16, rc->type);
582 put_bits(pb, 24, rc->begin);
583 put_bits(pb, 24, rc->end);
584 put_bits(pb, 24, rc->partition_size - 1);
585 put_bits(pb, 6, rc->classifications - 1);
586 put_bits(pb, 8, rc->classbook);
588 for (i = 0; i < rc->classifications; i++) {
590 for (j = 0; j < 8; j++)
591 tmp |= (rc->books[i][j] != -1) << j;
593 put_bits(pb, 3, tmp & 7);
594 put_bits(pb, 1, tmp > 7);
597 put_bits(pb, 5, tmp >> 3);
600 for (i = 0; i < rc->classifications; i++) {
602 for (j = 0; j < 8; j++)
603 if (rc->books[i][j] != -1)
604 put_bits(pb, 8, rc->books[i][j]);
608 static int put_main_header(vorbis_enc_context *venc, uint8_t **out)
613 int buffer_len = 50000;
614 uint8_t *buffer = av_mallocz(buffer_len), *p = buffer;
616 return AVERROR(ENOMEM);
618 // identification header
619 init_put_bits(&pb, p, buffer_len);
620 put_bits(&pb, 8, 1); //magic
621 for (i = 0; "vorbis"[i]; i++)
622 put_bits(&pb, 8, "vorbis"[i]);
623 put_bits32(&pb, 0); // version
624 put_bits(&pb, 8, venc->channels);
625 put_bits32(&pb, venc->sample_rate);
626 put_bits32(&pb, 0); // bitrate
627 put_bits32(&pb, 0); // bitrate
628 put_bits32(&pb, 0); // bitrate
629 put_bits(&pb, 4, venc->log2_blocksize[0]);
630 put_bits(&pb, 4, venc->log2_blocksize[1]);
631 put_bits(&pb, 1, 1); // framing
634 hlens[0] = put_bits_count(&pb) >> 3;
635 buffer_len -= hlens[0];
639 init_put_bits(&pb, p, buffer_len);
640 put_bits(&pb, 8, 3); //magic
641 for (i = 0; "vorbis"[i]; i++)
642 put_bits(&pb, 8, "vorbis"[i]);
643 put_bits32(&pb, 0); // vendor length TODO
644 put_bits32(&pb, 0); // amount of comments
645 put_bits(&pb, 1, 1); // framing
648 hlens[1] = put_bits_count(&pb) >> 3;
649 buffer_len -= hlens[1];
653 init_put_bits(&pb, p, buffer_len);
654 put_bits(&pb, 8, 5); //magic
655 for (i = 0; "vorbis"[i]; i++)
656 put_bits(&pb, 8, "vorbis"[i]);
659 put_bits(&pb, 8, venc->ncodebooks - 1);
660 for (i = 0; i < venc->ncodebooks; i++)
661 put_codebook_header(&pb, &venc->codebooks[i]);
663 // time domain, reserved, zero
665 put_bits(&pb, 16, 0);
668 put_bits(&pb, 6, venc->nfloors - 1);
669 for (i = 0; i < venc->nfloors; i++)
670 put_floor_header(&pb, &venc->floors[i]);
673 put_bits(&pb, 6, venc->nresidues - 1);
674 for (i = 0; i < venc->nresidues; i++)
675 put_residue_header(&pb, &venc->residues[i]);
678 put_bits(&pb, 6, venc->nmappings - 1);
679 for (i = 0; i < venc->nmappings; i++) {
680 vorbis_enc_mapping *mc = &venc->mappings[i];
682 put_bits(&pb, 16, 0); // mapping type
684 put_bits(&pb, 1, mc->submaps > 1);
686 put_bits(&pb, 4, mc->submaps - 1);
688 put_bits(&pb, 1, !!mc->coupling_steps);
689 if (mc->coupling_steps) {
690 put_bits(&pb, 8, mc->coupling_steps - 1);
691 for (j = 0; j < mc->coupling_steps; j++) {
692 put_bits(&pb, ilog(venc->channels - 1), mc->magnitude[j]);
693 put_bits(&pb, ilog(venc->channels - 1), mc->angle[j]);
697 put_bits(&pb, 2, 0); // reserved
700 for (j = 0; j < venc->channels; j++)
701 put_bits(&pb, 4, mc->mux[j]);
703 for (j = 0; j < mc->submaps; j++) {
704 put_bits(&pb, 8, 0); // reserved time configuration
705 put_bits(&pb, 8, mc->floor[j]);
706 put_bits(&pb, 8, mc->residue[j]);
711 put_bits(&pb, 6, venc->nmodes - 1);
712 for (i = 0; i < venc->nmodes; i++) {
713 put_bits(&pb, 1, venc->modes[i].blockflag);
714 put_bits(&pb, 16, 0); // reserved window type
715 put_bits(&pb, 16, 0); // reserved transform type
716 put_bits(&pb, 8, venc->modes[i].mapping);
719 put_bits(&pb, 1, 1); // framing
722 hlens[2] = put_bits_count(&pb) >> 3;
724 len = hlens[0] + hlens[1] + hlens[2];
725 p = *out = av_mallocz(64 + len + len/255);
727 return AVERROR(ENOMEM);
730 p += av_xiphlacing(p, hlens[0]);
731 p += av_xiphlacing(p, hlens[1]);
733 for (i = 0; i < 3; i++) {
734 memcpy(p, buffer + buffer_len, hlens[i]);
736 buffer_len += hlens[i];
743 static float get_floor_average(vorbis_enc_floor * fc, float *coeffs, int i)
745 int begin = fc->list[fc->list[FFMAX(i-1, 0)].sort].x;
746 int end = fc->list[fc->list[FFMIN(i+1, fc->values - 1)].sort].x;
750 for (j = begin; j < end; j++)
751 average += fabs(coeffs[j]);
752 return average / (end - begin);
755 static void floor_fit(vorbis_enc_context *venc, vorbis_enc_floor *fc,
756 float *coeffs, uint16_t *posts, int samples)
758 int range = 255 / fc->multiplier + 1;
760 float tot_average = 0.0;
761 float averages[MAX_FLOOR_VALUES];
762 for (i = 0; i < fc->values; i++) {
763 averages[i] = get_floor_average(fc, coeffs, i);
764 tot_average += averages[i];
766 tot_average /= fc->values;
767 tot_average /= venc->quality;
769 for (i = 0; i < fc->values; i++) {
770 int position = fc->list[fc->list[i].sort].x;
771 float average = averages[i];
774 average = sqrt(tot_average * average) * pow(1.25f, position*0.005f); // MAGIC!
775 for (j = 0; j < range - 1; j++)
776 if (ff_vorbis_floor1_inverse_db_table[j * fc->multiplier] > average)
778 posts[fc->list[i].sort] = j;
782 static int render_point(int x0, int y0, int x1, int y1, int x)
784 return y0 + (x - x0) * (y1 - y0) / (x1 - x0);
787 static int floor_encode(vorbis_enc_context *venc, vorbis_enc_floor *fc,
788 PutBitContext *pb, uint16_t *posts,
789 float *floor, int samples)
791 int range = 255 / fc->multiplier + 1;
792 int coded[MAX_FLOOR_VALUES]; // first 2 values are unused
795 if (pb->size_in_bits - put_bits_count(pb) < 1 + 2 * ilog(range - 1))
796 return AVERROR(EINVAL);
797 put_bits(pb, 1, 1); // non zero
798 put_bits(pb, ilog(range - 1), posts[0]);
799 put_bits(pb, ilog(range - 1), posts[1]);
800 coded[0] = coded[1] = 1;
802 for (i = 2; i < fc->values; i++) {
803 int predicted = render_point(fc->list[fc->list[i].low].x,
804 posts[fc->list[i].low],
805 fc->list[fc->list[i].high].x,
806 posts[fc->list[i].high],
808 int highroom = range - predicted;
809 int lowroom = predicted;
810 int room = FFMIN(highroom, lowroom);
811 if (predicted == posts[i]) {
812 coded[i] = 0; // must be used later as flag!
815 if (!coded[fc->list[i].low ])
816 coded[fc->list[i].low ] = -1;
817 if (!coded[fc->list[i].high])
818 coded[fc->list[i].high] = -1;
820 if (posts[i] > predicted) {
821 if (posts[i] - predicted > room)
822 coded[i] = posts[i] - predicted + lowroom;
824 coded[i] = (posts[i] - predicted) << 1;
826 if (predicted - posts[i] > room)
827 coded[i] = predicted - posts[i] + highroom - 1;
829 coded[i] = ((predicted - posts[i]) << 1) - 1;
834 for (i = 0; i < fc->partitions; i++) {
835 vorbis_enc_floor_class * c = &fc->classes[fc->partition_to_class[i]];
836 int k, cval = 0, csub = 1<<c->subclass;
838 vorbis_enc_codebook * book = &venc->codebooks[c->masterbook];
840 for (k = 0; k < c->dim; k++) {
842 for (l = 0; l < csub; l++) {
844 if (c->books[l] != -1)
845 maxval = venc->codebooks[c->books[l]].nentries;
846 // coded could be -1, but this still works, cause that is 0
847 if (coded[counter + k] < maxval)
852 cshift += c->subclass;
854 if (put_codeword(pb, book, cval))
855 return AVERROR(EINVAL);
857 for (k = 0; k < c->dim; k++) {
858 int book = c->books[cval & (csub-1)];
859 int entry = coded[counter++];
860 cval >>= c->subclass;
865 if (put_codeword(pb, &venc->codebooks[book], entry))
866 return AVERROR(EINVAL);
870 ff_vorbis_floor1_render_list(fc->list, fc->values, posts, coded,
871 fc->multiplier, floor, samples);
876 static float *put_vector(vorbis_enc_codebook *book, PutBitContext *pb,
880 float distance = FLT_MAX;
881 assert(book->dimensions);
882 for (i = 0; i < book->nentries; i++) {
883 float * vec = book->dimensions + i * book->ndimensions, d = book->pow2[i];
887 for (j = 0; j < book->ndimensions; j++)
888 d -= vec[j] * num[j];
894 if (put_codeword(pb, book, entry))
896 return &book->dimensions[entry * book->ndimensions];
899 static int residue_encode(vorbis_enc_context *venc, vorbis_enc_residue *rc,
900 PutBitContext *pb, float *coeffs, int samples,
903 int pass, i, j, p, k;
904 int psize = rc->partition_size;
905 int partitions = (rc->end - rc->begin) / psize;
906 int channels = (rc->type == 2) ? 1 : real_ch;
907 int classes[MAX_CHANNELS][NUM_RESIDUE_PARTITIONS];
908 int classwords = venc->codebooks[rc->classbook].ndimensions;
910 av_assert0(rc->type == 2);
911 av_assert0(real_ch == 2);
912 for (p = 0; p < partitions; p++) {
913 float max1 = 0.0, max2 = 0.0;
914 int s = rc->begin + p * psize;
915 for (k = s; k < s + psize; k += 2) {
916 max1 = FFMAX(max1, fabs(coeffs[ k / real_ch]));
917 max2 = FFMAX(max2, fabs(coeffs[samples + k / real_ch]));
920 for (i = 0; i < rc->classifications - 1; i++)
921 if (max1 < rc->maxes[i][0] && max2 < rc->maxes[i][1])
926 for (pass = 0; pass < 8; pass++) {
928 while (p < partitions) {
930 for (j = 0; j < channels; j++) {
931 vorbis_enc_codebook * book = &venc->codebooks[rc->classbook];
933 for (i = 0; i < classwords; i++) {
934 entry *= rc->classifications;
935 entry += classes[j][p + i];
937 if (put_codeword(pb, book, entry))
938 return AVERROR(EINVAL);
940 for (i = 0; i < classwords && p < partitions; i++, p++) {
941 for (j = 0; j < channels; j++) {
942 int nbook = rc->books[classes[j][p]][pass];
943 vorbis_enc_codebook * book = &venc->codebooks[nbook];
944 float *buf = coeffs + samples*j + rc->begin + p*psize;
948 assert(rc->type == 0 || rc->type == 2);
949 assert(!(psize % book->ndimensions));
952 for (k = 0; k < psize; k += book->ndimensions) {
954 float *a = put_vector(book, pb, &buf[k]);
956 return AVERROR(EINVAL);
957 for (l = 0; l < book->ndimensions; l++)
961 int s = rc->begin + p * psize, a1, b1;
962 a1 = (s % real_ch) * samples;
964 s = real_ch * samples;
965 for (k = 0; k < psize; k += book->ndimensions) {
966 int dim, a2 = a1, b2 = b1;
967 float vec[MAX_CODEBOOK_DIM], *pv = vec;
968 for (dim = book->ndimensions; dim--; ) {
969 *pv++ = coeffs[a2 + b2];
970 if ((a2 += samples) == s) {
975 pv = put_vector(book, pb, vec);
977 return AVERROR(EINVAL);
978 for (dim = book->ndimensions; dim--; ) {
979 coeffs[a1 + b1] -= *pv++;
980 if ((a1 += samples) == s) {
994 static int apply_window_and_mdct(vorbis_enc_context *venc,
995 float **audio, int samples)
998 const float * win = venc->win[0];
999 int window_len = 1 << (venc->log2_blocksize[0] - 1);
1000 float n = (float)(1 << venc->log2_blocksize[0]) / 4.0;
1001 AVFloatDSPContext *fdsp = venc->fdsp;
1003 if (!venc->have_saved && !samples)
1006 if (venc->have_saved) {
1007 for (channel = 0; channel < venc->channels; channel++)
1008 memcpy(venc->samples + channel * window_len * 2,
1009 venc->saved + channel * window_len, sizeof(float) * window_len);
1011 for (channel = 0; channel < venc->channels; channel++)
1012 memset(venc->samples + channel * window_len * 2, 0,
1013 sizeof(float) * window_len);
1017 for (channel = 0; channel < venc->channels; channel++) {
1018 float *offset = venc->samples + channel * window_len * 2 + window_len;
1020 fdsp->vector_fmul_reverse(offset, audio[channel], win, samples);
1021 fdsp->vector_fmul_scalar(offset, offset, 1/n, samples);
1024 for (channel = 0; channel < venc->channels; channel++)
1025 memset(venc->samples + channel * window_len * 2 + window_len,
1026 0, sizeof(float) * window_len);
1029 for (channel = 0; channel < venc->channels; channel++)
1030 venc->mdct[0].mdct_calc(&venc->mdct[0], venc->coeffs + channel * window_len,
1031 venc->samples + channel * window_len * 2);
1034 for (channel = 0; channel < venc->channels; channel++) {
1035 float *offset = venc->saved + channel * window_len;
1037 fdsp->vector_fmul(offset, audio[channel], win, samples);
1038 fdsp->vector_fmul_scalar(offset, offset, 1/n, samples);
1040 venc->have_saved = 1;
1042 venc->have_saved = 0;
1047 /* Used for padding the last encoded packet */
1048 static AVFrame *spawn_empty_frame(AVCodecContext *avctx, int channels)
1050 AVFrame *f = av_frame_alloc();
1055 f->format = avctx->sample_fmt;
1056 f->nb_samples = avctx->frame_size;
1057 f->channel_layout = avctx->channel_layout;
1059 if (av_frame_get_buffer(f, 4)) {
1064 for (int ch = 0; ch < channels; ch++) {
1065 size_t bps = av_get_bytes_per_sample(f->format);
1066 memset(f->extended_data[ch], 0, bps * f->nb_samples);
1071 static float **alloc_audio_arrays(int channels, int frame_size)
1073 float **audio = av_mallocz_array(channels, sizeof(float *));
1077 for (int ch = 0; ch < channels; ch++) {
1078 audio[ch] = av_mallocz_array(frame_size, sizeof(float));
1080 // alloc has failed, free everything allocated thus far
1081 for (ch--; ch >= 0; ch--)
1091 /* Concatenate audio frames into an appropriately sized array of samples */
1092 static void move_audio(vorbis_enc_context *venc, float **audio, int *samples, int sf_size)
1094 AVFrame *cur = NULL;
1095 int frame_size = 1 << (venc->log2_blocksize[1] - 1);
1096 int subframes = frame_size / sf_size;
1098 for (int sf = 0; sf < subframes; sf++) {
1099 cur = ff_bufqueue_get(&venc->bufqueue);
1100 *samples += cur->nb_samples;
1102 for (int ch = 0; ch < venc->channels; ch++) {
1103 const float *input = (float *) cur->extended_data[ch];
1104 const size_t len = cur->nb_samples * sizeof(float);
1105 memcpy(&audio[ch][sf*sf_size], input, len);
1107 av_frame_free(&cur);
1111 static int vorbis_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
1112 const AVFrame *frame, int *got_packet_ptr)
1114 vorbis_enc_context *venc = avctx->priv_data;
1115 float **audio = NULL;
1116 int i, ret, need_more;
1117 int samples = 0, frame_size = 1 << (venc->log2_blocksize[1] - 1);
1118 vorbis_enc_mode *mode;
1119 vorbis_enc_mapping *mapping;
1123 if ((ret = ff_af_queue_add(&venc->afq, frame)) < 0)
1125 ff_bufqueue_add(avctx, &venc->bufqueue, av_frame_clone(frame));
1127 if (!venc->afq.remaining_samples)
1130 need_more = venc->bufqueue.available * avctx->frame_size < frame_size;
1131 need_more = frame && need_more;
1135 audio = alloc_audio_arrays(venc->channels, frame_size);
1137 return AVERROR(ENOMEM);
1139 /* Pad the bufqueue with empty frames for encoding the last packet. */
1141 if (venc->bufqueue.available * avctx->frame_size < frame_size) {
1142 int frames_needed = (frame_size/avctx->frame_size) - venc->bufqueue.available;
1144 for (int i = 0; i < frames_needed; i++) {
1145 AVFrame *empty = spawn_empty_frame(avctx, venc->channels);
1147 return AVERROR(ENOMEM);
1149 ff_bufqueue_add(avctx, &venc->bufqueue, empty);
1154 move_audio(venc, audio, &samples, avctx->frame_size);
1156 if (!apply_window_and_mdct(venc, audio, samples))
1159 if ((ret = ff_alloc_packet2(avctx, avpkt, 8192, 0)) < 0)
1162 init_put_bits(&pb, avpkt->data, avpkt->size);
1164 if (pb.size_in_bits - put_bits_count(&pb) < 1 + ilog(venc->nmodes - 1)) {
1165 av_log(avctx, AV_LOG_ERROR, "output buffer is too small\n");
1166 return AVERROR(EINVAL);
1169 put_bits(&pb, 1, 0); // magic bit
1171 put_bits(&pb, ilog(venc->nmodes - 1), 0); // 0 bits, the mode
1173 mode = &venc->modes[0];
1174 mapping = &venc->mappings[mode->mapping];
1175 if (mode->blockflag) {
1176 put_bits(&pb, 1, 0);
1177 put_bits(&pb, 1, 0);
1180 for (i = 0; i < venc->channels; i++) {
1181 vorbis_enc_floor *fc = &venc->floors[mapping->floor[mapping->mux[i]]];
1182 uint16_t posts[MAX_FLOOR_VALUES];
1183 floor_fit(venc, fc, &venc->coeffs[i * samples], posts, samples);
1184 if (floor_encode(venc, fc, &pb, posts, &venc->floor[i * samples], samples)) {
1185 av_log(avctx, AV_LOG_ERROR, "output buffer is too small\n");
1186 return AVERROR(EINVAL);
1190 for (i = 0; i < venc->channels * samples; i++)
1191 venc->coeffs[i] /= venc->floor[i];
1193 for (i = 0; i < mapping->coupling_steps; i++) {
1194 float *mag = venc->coeffs + mapping->magnitude[i] * samples;
1195 float *ang = venc->coeffs + mapping->angle[i] * samples;
1197 for (j = 0; j < samples; j++) {
1207 if (residue_encode(venc, &venc->residues[mapping->residue[mapping->mux[0]]],
1208 &pb, venc->coeffs, samples, venc->channels)) {
1209 av_log(avctx, AV_LOG_ERROR, "output buffer is too small\n");
1210 return AVERROR(EINVAL);
1213 flush_put_bits(&pb);
1214 avpkt->size = put_bits_count(&pb) >> 3;
1216 for (int ch = 0; ch < venc->channels; ch++)
1220 ff_af_queue_remove(&venc->afq, frame_size, &avpkt->pts, &avpkt->duration);
1221 *got_packet_ptr = 1;
1226 static av_cold int vorbis_encode_close(AVCodecContext *avctx)
1228 vorbis_enc_context *venc = avctx->priv_data;
1231 if (venc->codebooks)
1232 for (i = 0; i < venc->ncodebooks; i++) {
1233 av_freep(&venc->codebooks[i].lens);
1234 av_freep(&venc->codebooks[i].codewords);
1235 av_freep(&venc->codebooks[i].quantlist);
1236 av_freep(&venc->codebooks[i].dimensions);
1237 av_freep(&venc->codebooks[i].pow2);
1239 av_freep(&venc->codebooks);
1242 for (i = 0; i < venc->nfloors; i++) {
1244 if (venc->floors[i].classes)
1245 for (j = 0; j < venc->floors[i].nclasses; j++)
1246 av_freep(&venc->floors[i].classes[j].books);
1247 av_freep(&venc->floors[i].classes);
1248 av_freep(&venc->floors[i].partition_to_class);
1249 av_freep(&venc->floors[i].list);
1251 av_freep(&venc->floors);
1254 for (i = 0; i < venc->nresidues; i++) {
1255 av_freep(&venc->residues[i].books);
1256 av_freep(&venc->residues[i].maxes);
1258 av_freep(&venc->residues);
1261 for (i = 0; i < venc->nmappings; i++) {
1262 av_freep(&venc->mappings[i].mux);
1263 av_freep(&venc->mappings[i].floor);
1264 av_freep(&venc->mappings[i].residue);
1265 av_freep(&venc->mappings[i].magnitude);
1266 av_freep(&venc->mappings[i].angle);
1268 av_freep(&venc->mappings);
1270 av_freep(&venc->modes);
1272 av_freep(&venc->saved);
1273 av_freep(&venc->samples);
1274 av_freep(&venc->floor);
1275 av_freep(&venc->coeffs);
1276 av_freep(&venc->fdsp);
1278 ff_mdct_end(&venc->mdct[0]);
1279 ff_mdct_end(&venc->mdct[1]);
1280 ff_af_queue_close(&venc->afq);
1281 ff_bufqueue_discard_all(&venc->bufqueue);
1283 av_freep(&avctx->extradata);
1288 static av_cold int vorbis_encode_init(AVCodecContext *avctx)
1290 vorbis_enc_context *venc = avctx->priv_data;
1293 if (avctx->channels != 2) {
1294 av_log(avctx, AV_LOG_ERROR, "Current FFmpeg Vorbis encoder only supports 2 channels.\n");
1298 if ((ret = create_vorbis_context(venc, avctx)) < 0)
1301 avctx->bit_rate = 0;
1302 if (avctx->flags & AV_CODEC_FLAG_QSCALE)
1303 venc->quality = avctx->global_quality / (float)FF_QP2LAMBDA;
1306 venc->quality *= venc->quality;
1308 if ((ret = put_main_header(venc, (uint8_t**)&avctx->extradata)) < 0)
1310 avctx->extradata_size = ret;
1312 avctx->frame_size = 64;
1314 ff_af_queue_init(avctx, &venc->afq);
1318 vorbis_encode_close(avctx);
1322 AVCodec ff_vorbis_encoder = {
1324 .long_name = NULL_IF_CONFIG_SMALL("Vorbis"),
1325 .type = AVMEDIA_TYPE_AUDIO,
1326 .id = AV_CODEC_ID_VORBIS,
1327 .priv_data_size = sizeof(vorbis_enc_context),
1328 .init = vorbis_encode_init,
1329 .encode2 = vorbis_encode_frame,
1330 .close = vorbis_encode_close,
1331 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_EXPERIMENTAL,
1332 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLTP,
1333 AV_SAMPLE_FMT_NONE },