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
115 float *scratch; // used for tmp values for psy model
119 struct FFBufQueue bufqueue;
122 vorbis_enc_codebook *codebooks;
125 vorbis_enc_floor *floors;
128 vorbis_enc_residue *residues;
131 vorbis_enc_mapping *mappings;
134 vorbis_enc_mode *modes;
138 AVFloatDSPContext *fdsp;
139 } vorbis_enc_context;
141 #define MAX_CHANNELS 2
142 #define MAX_CODEBOOK_DIM 8
144 #define MAX_FLOOR_CLASS_DIM 4
145 #define NUM_FLOOR_PARTITIONS 8
146 #define MAX_FLOOR_VALUES (MAX_FLOOR_CLASS_DIM*NUM_FLOOR_PARTITIONS+2)
148 #define RESIDUE_SIZE 1600
149 #define RESIDUE_PART_SIZE 32
150 #define NUM_RESIDUE_PARTITIONS (RESIDUE_SIZE/RESIDUE_PART_SIZE)
152 static inline int put_codeword(PutBitContext *pb, vorbis_enc_codebook *cb,
155 av_assert2(entry >= 0);
156 av_assert2(entry < cb->nentries);
157 av_assert2(cb->lens[entry]);
158 if (pb->size_in_bits - put_bits_count(pb) < cb->lens[entry])
159 return AVERROR(EINVAL);
160 put_bits(pb, cb->lens[entry], cb->codewords[entry]);
164 static int cb_lookup_vals(int lookup, int dimensions, int entries)
167 return ff_vorbis_nth_root(entries, dimensions);
168 else if (lookup == 2)
169 return dimensions *entries;
173 static int ready_codebook(vorbis_enc_codebook *cb)
177 ff_vorbis_len2vlc(cb->lens, cb->codewords, cb->nentries);
180 cb->pow2 = cb->dimensions = NULL;
182 int vals = cb_lookup_vals(cb->lookup, cb->ndimensions, cb->nentries);
183 cb->dimensions = av_malloc_array(cb->nentries, sizeof(float) * cb->ndimensions);
184 cb->pow2 = av_mallocz_array(cb->nentries, sizeof(float));
185 if (!cb->dimensions || !cb->pow2)
186 return AVERROR(ENOMEM);
187 for (i = 0; i < cb->nentries; i++) {
191 for (j = 0; j < cb->ndimensions; j++) {
194 off = (i / div) % vals; // lookup type 1
196 off = i * cb->ndimensions + j; // lookup type 2
198 cb->dimensions[i * cb->ndimensions + j] = last + cb->min + cb->quantlist[off] * cb->delta;
200 last = cb->dimensions[i * cb->ndimensions + j];
201 cb->pow2[i] += cb->dimensions[i * cb->ndimensions + j] * cb->dimensions[i * cb->ndimensions + j];
210 static int ready_residue(vorbis_enc_residue *rc, vorbis_enc_context *venc)
213 av_assert0(rc->type == 2);
214 rc->maxes = av_mallocz_array(rc->classifications, sizeof(float[2]));
216 return AVERROR(ENOMEM);
217 for (i = 0; i < rc->classifications; i++) {
219 vorbis_enc_codebook * cb;
220 for (j = 0; j < 8; j++)
221 if (rc->books[i][j] != -1)
225 cb = &venc->codebooks[rc->books[i][j]];
226 assert(cb->ndimensions >= 2);
229 for (j = 0; j < cb->nentries; j++) {
233 a = fabs(cb->dimensions[j * cb->ndimensions]);
234 if (a > rc->maxes[i][0])
236 a = fabs(cb->dimensions[j * cb->ndimensions + 1]);
237 if (a > rc->maxes[i][1])
242 for (i = 0; i < rc->classifications; i++) {
243 rc->maxes[i][0] += 0.8;
244 rc->maxes[i][1] += 0.8;
249 static av_cold int dsp_init(AVCodecContext *avctx, vorbis_enc_context *venc)
253 venc->fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
255 return AVERROR(ENOMEM);
258 venc->win[0] = ff_vorbis_vwin[venc->log2_blocksize[0] - 6];
259 venc->win[1] = ff_vorbis_vwin[venc->log2_blocksize[1] - 6];
261 if ((ret = ff_mdct_init(&venc->mdct[0], venc->log2_blocksize[0], 0, 1.0)) < 0)
263 if ((ret = ff_mdct_init(&venc->mdct[1], venc->log2_blocksize[1], 0, 1.0)) < 0)
269 static int create_vorbis_context(vorbis_enc_context *venc,
270 AVCodecContext *avctx)
272 vorbis_enc_floor *fc;
273 vorbis_enc_residue *rc;
274 vorbis_enc_mapping *mc;
277 venc->channels = avctx->channels;
278 venc->sample_rate = avctx->sample_rate;
279 venc->log2_blocksize[0] = venc->log2_blocksize[1] = 11;
281 venc->ncodebooks = FF_ARRAY_ELEMS(cvectors);
282 venc->codebooks = av_malloc(sizeof(vorbis_enc_codebook) * venc->ncodebooks);
283 if (!venc->codebooks)
284 return AVERROR(ENOMEM);
286 // codebook 0..14 - floor1 book, values 0..255
287 // codebook 15 residue masterbook
288 // codebook 16..29 residue
289 for (book = 0; book < venc->ncodebooks; book++) {
290 vorbis_enc_codebook *cb = &venc->codebooks[book];
292 cb->ndimensions = cvectors[book].dim;
293 cb->nentries = cvectors[book].real_len;
294 cb->min = cvectors[book].min;
295 cb->delta = cvectors[book].delta;
296 cb->lookup = cvectors[book].lookup;
299 cb->lens = av_malloc_array(cb->nentries, sizeof(uint8_t));
300 cb->codewords = av_malloc_array(cb->nentries, sizeof(uint32_t));
301 if (!cb->lens || !cb->codewords)
302 return AVERROR(ENOMEM);
303 memcpy(cb->lens, cvectors[book].clens, cvectors[book].len);
304 memset(cb->lens + cvectors[book].len, 0, cb->nentries - cvectors[book].len);
307 vals = cb_lookup_vals(cb->lookup, cb->ndimensions, cb->nentries);
308 cb->quantlist = av_malloc_array(vals, sizeof(int));
310 return AVERROR(ENOMEM);
311 for (i = 0; i < vals; i++)
312 cb->quantlist[i] = cvectors[book].quant[i];
314 cb->quantlist = NULL;
316 if ((ret = ready_codebook(cb)) < 0)
321 venc->floors = av_malloc(sizeof(vorbis_enc_floor) * venc->nfloors);
323 return AVERROR(ENOMEM);
326 fc = &venc->floors[0];
327 fc->partitions = NUM_FLOOR_PARTITIONS;
328 fc->partition_to_class = av_malloc(sizeof(int) * fc->partitions);
329 if (!fc->partition_to_class)
330 return AVERROR(ENOMEM);
332 for (i = 0; i < fc->partitions; i++) {
333 static const int a[] = {0, 1, 2, 2, 3, 3, 4, 4};
334 fc->partition_to_class[i] = a[i];
335 fc->nclasses = FFMAX(fc->nclasses, fc->partition_to_class[i]);
338 fc->classes = av_malloc_array(fc->nclasses, sizeof(vorbis_enc_floor_class));
340 return AVERROR(ENOMEM);
341 for (i = 0; i < fc->nclasses; i++) {
342 vorbis_enc_floor_class * c = &fc->classes[i];
344 c->dim = floor_classes[i].dim;
345 c->subclass = floor_classes[i].subclass;
346 c->masterbook = floor_classes[i].masterbook;
347 books = (1 << c->subclass);
348 c->books = av_malloc_array(books, sizeof(int));
350 return AVERROR(ENOMEM);
351 for (j = 0; j < books; j++)
352 c->books[j] = floor_classes[i].nbooks[j];
355 fc->rangebits = venc->log2_blocksize[1] - 1;
358 for (i = 0; i < fc->partitions; i++)
359 fc->values += fc->classes[fc->partition_to_class[i]].dim;
361 fc->list = av_malloc_array(fc->values, sizeof(vorbis_floor1_entry));
363 return AVERROR(ENOMEM);
365 fc->list[1].x = 1 << fc->rangebits;
366 for (i = 2; i < fc->values; i++) {
367 static const int a[] = {
368 93, 23,372, 6, 46,186,750, 14, 33, 65,
369 130,260,556, 3, 10, 18, 28, 39, 55, 79,
370 111,158,220,312,464,650,850
372 fc->list[i].x = a[i - 2];
374 if (ff_vorbis_ready_floor1_list(avctx, fc->list, fc->values))
378 venc->residues = av_malloc(sizeof(vorbis_enc_residue) * venc->nresidues);
380 return AVERROR(ENOMEM);
383 rc = &venc->residues[0];
387 rc->partition_size = 32;
388 rc->classifications = 10;
390 rc->books = av_malloc(sizeof(*rc->books) * rc->classifications);
392 return AVERROR(ENOMEM);
394 static const int8_t a[10][8] = {
395 { -1, -1, -1, -1, -1, -1, -1, -1, },
396 { -1, -1, 16, -1, -1, -1, -1, -1, },
397 { -1, -1, 17, -1, -1, -1, -1, -1, },
398 { -1, -1, 18, -1, -1, -1, -1, -1, },
399 { -1, -1, 19, -1, -1, -1, -1, -1, },
400 { -1, -1, 20, -1, -1, -1, -1, -1, },
401 { -1, -1, 21, -1, -1, -1, -1, -1, },
402 { 22, 23, -1, -1, -1, -1, -1, -1, },
403 { 24, 25, -1, -1, -1, -1, -1, -1, },
404 { 26, 27, 28, -1, -1, -1, -1, -1, },
406 memcpy(rc->books, a, sizeof a);
408 if ((ret = ready_residue(rc, venc)) < 0)
412 venc->mappings = av_malloc(sizeof(vorbis_enc_mapping) * venc->nmappings);
414 return AVERROR(ENOMEM);
417 mc = &venc->mappings[0];
419 mc->mux = av_malloc(sizeof(int) * venc->channels);
421 return AVERROR(ENOMEM);
422 for (i = 0; i < venc->channels; i++)
424 mc->floor = av_malloc(sizeof(int) * mc->submaps);
425 mc->residue = av_malloc(sizeof(int) * mc->submaps);
426 if (!mc->floor || !mc->residue)
427 return AVERROR(ENOMEM);
428 for (i = 0; i < mc->submaps; i++) {
432 mc->coupling_steps = venc->channels == 2 ? 1 : 0;
433 mc->magnitude = av_malloc(sizeof(int) * mc->coupling_steps);
434 mc->angle = av_malloc(sizeof(int) * mc->coupling_steps);
435 if (!mc->magnitude || !mc->angle)
436 return AVERROR(ENOMEM);
437 if (mc->coupling_steps) {
438 mc->magnitude[0] = 0;
443 venc->modes = av_malloc(sizeof(vorbis_enc_mode) * venc->nmodes);
445 return AVERROR(ENOMEM);
448 venc->modes[0].blockflag = 0;
449 venc->modes[0].mapping = 0;
451 venc->modes[1].blockflag = 1;
452 venc->modes[1].mapping = 0;
454 venc->have_saved = 0;
455 venc->saved = av_malloc_array(sizeof(float) * venc->channels, (1 << venc->log2_blocksize[1]) / 2);
456 venc->samples = av_malloc_array(sizeof(float) * venc->channels, (1 << venc->log2_blocksize[1]));
457 venc->floor = av_malloc_array(sizeof(float) * venc->channels, (1 << venc->log2_blocksize[1]) / 2);
458 venc->coeffs = av_malloc_array(sizeof(float) * venc->channels, (1 << venc->log2_blocksize[1]) / 2);
459 venc->scratch = av_malloc_array(sizeof(float) * venc->channels, (1 << venc->log2_blocksize[1]));
461 if (!venc->saved || !venc->samples || !venc->floor || !venc->coeffs || !venc->scratch)
462 return AVERROR(ENOMEM);
464 if ((ret = dsp_init(avctx, venc)) < 0)
470 static void put_float(PutBitContext *pb, float f)
474 mant = (int)ldexp(frexp(f, &exp), 20);
480 res |= mant | (exp << 21);
484 static void put_codebook_header(PutBitContext *pb, vorbis_enc_codebook *cb)
489 put_bits(pb, 24, 0x564342); //magic
490 put_bits(pb, 16, cb->ndimensions);
491 put_bits(pb, 24, cb->nentries);
493 for (i = 1; i < cb->nentries; i++)
494 if (cb->lens[i] < cb->lens[i-1])
496 if (i == cb->nentries)
499 put_bits(pb, 1, ordered);
501 int len = cb->lens[0];
502 put_bits(pb, 5, len - 1);
504 while (i < cb->nentries) {
506 for (j = 0; j+i < cb->nentries; j++)
507 if (cb->lens[j+i] != len)
509 put_bits(pb, ilog(cb->nentries - i), j);
515 for (i = 0; i < cb->nentries; i++)
518 if (i != cb->nentries)
520 put_bits(pb, 1, sparse);
522 for (i = 0; i < cb->nentries; i++) {
524 put_bits(pb, 1, !!cb->lens[i]);
526 put_bits(pb, 5, cb->lens[i] - 1);
530 put_bits(pb, 4, cb->lookup);
532 int tmp = cb_lookup_vals(cb->lookup, cb->ndimensions, cb->nentries);
533 int bits = ilog(cb->quantlist[0]);
535 for (i = 1; i < tmp; i++)
536 bits = FFMAX(bits, ilog(cb->quantlist[i]));
538 put_float(pb, cb->min);
539 put_float(pb, cb->delta);
541 put_bits(pb, 4, bits - 1);
542 put_bits(pb, 1, cb->seq_p);
544 for (i = 0; i < tmp; i++)
545 put_bits(pb, bits, cb->quantlist[i]);
549 static void put_floor_header(PutBitContext *pb, vorbis_enc_floor *fc)
553 put_bits(pb, 16, 1); // type, only floor1 is supported
555 put_bits(pb, 5, fc->partitions);
557 for (i = 0; i < fc->partitions; i++)
558 put_bits(pb, 4, fc->partition_to_class[i]);
560 for (i = 0; i < fc->nclasses; i++) {
563 put_bits(pb, 3, fc->classes[i].dim - 1);
564 put_bits(pb, 2, fc->classes[i].subclass);
566 if (fc->classes[i].subclass)
567 put_bits(pb, 8, fc->classes[i].masterbook);
569 books = (1 << fc->classes[i].subclass);
571 for (j = 0; j < books; j++)
572 put_bits(pb, 8, fc->classes[i].books[j] + 1);
575 put_bits(pb, 2, fc->multiplier - 1);
576 put_bits(pb, 4, fc->rangebits);
578 for (i = 2; i < fc->values; i++)
579 put_bits(pb, fc->rangebits, fc->list[i].x);
582 static void put_residue_header(PutBitContext *pb, vorbis_enc_residue *rc)
586 put_bits(pb, 16, rc->type);
588 put_bits(pb, 24, rc->begin);
589 put_bits(pb, 24, rc->end);
590 put_bits(pb, 24, rc->partition_size - 1);
591 put_bits(pb, 6, rc->classifications - 1);
592 put_bits(pb, 8, rc->classbook);
594 for (i = 0; i < rc->classifications; i++) {
596 for (j = 0; j < 8; j++)
597 tmp |= (rc->books[i][j] != -1) << j;
599 put_bits(pb, 3, tmp & 7);
600 put_bits(pb, 1, tmp > 7);
603 put_bits(pb, 5, tmp >> 3);
606 for (i = 0; i < rc->classifications; i++) {
608 for (j = 0; j < 8; j++)
609 if (rc->books[i][j] != -1)
610 put_bits(pb, 8, rc->books[i][j]);
614 static int put_main_header(vorbis_enc_context *venc, uint8_t **out)
619 int buffer_len = 50000;
620 uint8_t *buffer = av_mallocz(buffer_len), *p = buffer;
622 return AVERROR(ENOMEM);
624 // identification header
625 init_put_bits(&pb, p, buffer_len);
626 put_bits(&pb, 8, 1); //magic
627 for (i = 0; "vorbis"[i]; i++)
628 put_bits(&pb, 8, "vorbis"[i]);
629 put_bits32(&pb, 0); // version
630 put_bits(&pb, 8, venc->channels);
631 put_bits32(&pb, venc->sample_rate);
632 put_bits32(&pb, 0); // bitrate
633 put_bits32(&pb, 0); // bitrate
634 put_bits32(&pb, 0); // bitrate
635 put_bits(&pb, 4, venc->log2_blocksize[0]);
636 put_bits(&pb, 4, venc->log2_blocksize[1]);
637 put_bits(&pb, 1, 1); // framing
640 hlens[0] = put_bits_count(&pb) >> 3;
641 buffer_len -= hlens[0];
645 init_put_bits(&pb, p, buffer_len);
646 put_bits(&pb, 8, 3); //magic
647 for (i = 0; "vorbis"[i]; i++)
648 put_bits(&pb, 8, "vorbis"[i]);
649 put_bits32(&pb, 0); // vendor length TODO
650 put_bits32(&pb, 0); // amount of comments
651 put_bits(&pb, 1, 1); // framing
654 hlens[1] = put_bits_count(&pb) >> 3;
655 buffer_len -= hlens[1];
659 init_put_bits(&pb, p, buffer_len);
660 put_bits(&pb, 8, 5); //magic
661 for (i = 0; "vorbis"[i]; i++)
662 put_bits(&pb, 8, "vorbis"[i]);
665 put_bits(&pb, 8, venc->ncodebooks - 1);
666 for (i = 0; i < venc->ncodebooks; i++)
667 put_codebook_header(&pb, &venc->codebooks[i]);
669 // time domain, reserved, zero
671 put_bits(&pb, 16, 0);
674 put_bits(&pb, 6, venc->nfloors - 1);
675 for (i = 0; i < venc->nfloors; i++)
676 put_floor_header(&pb, &venc->floors[i]);
679 put_bits(&pb, 6, venc->nresidues - 1);
680 for (i = 0; i < venc->nresidues; i++)
681 put_residue_header(&pb, &venc->residues[i]);
684 put_bits(&pb, 6, venc->nmappings - 1);
685 for (i = 0; i < venc->nmappings; i++) {
686 vorbis_enc_mapping *mc = &venc->mappings[i];
688 put_bits(&pb, 16, 0); // mapping type
690 put_bits(&pb, 1, mc->submaps > 1);
692 put_bits(&pb, 4, mc->submaps - 1);
694 put_bits(&pb, 1, !!mc->coupling_steps);
695 if (mc->coupling_steps) {
696 put_bits(&pb, 8, mc->coupling_steps - 1);
697 for (j = 0; j < mc->coupling_steps; j++) {
698 put_bits(&pb, ilog(venc->channels - 1), mc->magnitude[j]);
699 put_bits(&pb, ilog(venc->channels - 1), mc->angle[j]);
703 put_bits(&pb, 2, 0); // reserved
706 for (j = 0; j < venc->channels; j++)
707 put_bits(&pb, 4, mc->mux[j]);
709 for (j = 0; j < mc->submaps; j++) {
710 put_bits(&pb, 8, 0); // reserved time configuration
711 put_bits(&pb, 8, mc->floor[j]);
712 put_bits(&pb, 8, mc->residue[j]);
717 put_bits(&pb, 6, venc->nmodes - 1);
718 for (i = 0; i < venc->nmodes; i++) {
719 put_bits(&pb, 1, venc->modes[i].blockflag);
720 put_bits(&pb, 16, 0); // reserved window type
721 put_bits(&pb, 16, 0); // reserved transform type
722 put_bits(&pb, 8, venc->modes[i].mapping);
725 put_bits(&pb, 1, 1); // framing
728 hlens[2] = put_bits_count(&pb) >> 3;
730 len = hlens[0] + hlens[1] + hlens[2];
731 p = *out = av_mallocz(64 + len + len/255);
733 return AVERROR(ENOMEM);
736 p += av_xiphlacing(p, hlens[0]);
737 p += av_xiphlacing(p, hlens[1]);
739 for (i = 0; i < 3; i++) {
740 memcpy(p, buffer + buffer_len, hlens[i]);
742 buffer_len += hlens[i];
749 static float get_floor_average(vorbis_enc_floor * fc, float *coeffs, int i)
751 int begin = fc->list[fc->list[FFMAX(i-1, 0)].sort].x;
752 int end = fc->list[fc->list[FFMIN(i+1, fc->values - 1)].sort].x;
756 for (j = begin; j < end; j++)
757 average += fabs(coeffs[j]);
758 return average / (end - begin);
761 static void floor_fit(vorbis_enc_context *venc, vorbis_enc_floor *fc,
762 float *coeffs, uint16_t *posts, int samples)
764 int range = 255 / fc->multiplier + 1;
766 float tot_average = 0.0;
767 float averages[MAX_FLOOR_VALUES];
768 for (i = 0; i < fc->values; i++) {
769 averages[i] = get_floor_average(fc, coeffs, i);
770 tot_average += averages[i];
772 tot_average /= fc->values;
773 tot_average /= venc->quality;
775 for (i = 0; i < fc->values; i++) {
776 int position = fc->list[fc->list[i].sort].x;
777 float average = averages[i];
780 average = sqrt(tot_average * average) * pow(1.25f, position*0.005f); // MAGIC!
781 for (j = 0; j < range - 1; j++)
782 if (ff_vorbis_floor1_inverse_db_table[j * fc->multiplier] > average)
784 posts[fc->list[i].sort] = j;
788 static int render_point(int x0, int y0, int x1, int y1, int x)
790 return y0 + (x - x0) * (y1 - y0) / (x1 - x0);
793 static int floor_encode(vorbis_enc_context *venc, vorbis_enc_floor *fc,
794 PutBitContext *pb, uint16_t *posts,
795 float *floor, int samples)
797 int range = 255 / fc->multiplier + 1;
798 int coded[MAX_FLOOR_VALUES]; // first 2 values are unused
801 if (pb->size_in_bits - put_bits_count(pb) < 1 + 2 * ilog(range - 1))
802 return AVERROR(EINVAL);
803 put_bits(pb, 1, 1); // non zero
804 put_bits(pb, ilog(range - 1), posts[0]);
805 put_bits(pb, ilog(range - 1), posts[1]);
806 coded[0] = coded[1] = 1;
808 for (i = 2; i < fc->values; i++) {
809 int predicted = render_point(fc->list[fc->list[i].low].x,
810 posts[fc->list[i].low],
811 fc->list[fc->list[i].high].x,
812 posts[fc->list[i].high],
814 int highroom = range - predicted;
815 int lowroom = predicted;
816 int room = FFMIN(highroom, lowroom);
817 if (predicted == posts[i]) {
818 coded[i] = 0; // must be used later as flag!
821 if (!coded[fc->list[i].low ])
822 coded[fc->list[i].low ] = -1;
823 if (!coded[fc->list[i].high])
824 coded[fc->list[i].high] = -1;
826 if (posts[i] > predicted) {
827 if (posts[i] - predicted > room)
828 coded[i] = posts[i] - predicted + lowroom;
830 coded[i] = (posts[i] - predicted) << 1;
832 if (predicted - posts[i] > room)
833 coded[i] = predicted - posts[i] + highroom - 1;
835 coded[i] = ((predicted - posts[i]) << 1) - 1;
840 for (i = 0; i < fc->partitions; i++) {
841 vorbis_enc_floor_class * c = &fc->classes[fc->partition_to_class[i]];
842 int k, cval = 0, csub = 1<<c->subclass;
844 vorbis_enc_codebook * book = &venc->codebooks[c->masterbook];
846 for (k = 0; k < c->dim; k++) {
848 for (l = 0; l < csub; l++) {
850 if (c->books[l] != -1)
851 maxval = venc->codebooks[c->books[l]].nentries;
852 // coded could be -1, but this still works, cause that is 0
853 if (coded[counter + k] < maxval)
858 cshift += c->subclass;
860 if (put_codeword(pb, book, cval))
861 return AVERROR(EINVAL);
863 for (k = 0; k < c->dim; k++) {
864 int book = c->books[cval & (csub-1)];
865 int entry = coded[counter++];
866 cval >>= c->subclass;
871 if (put_codeword(pb, &venc->codebooks[book], entry))
872 return AVERROR(EINVAL);
876 ff_vorbis_floor1_render_list(fc->list, fc->values, posts, coded,
877 fc->multiplier, floor, samples);
882 static float *put_vector(vorbis_enc_codebook *book, PutBitContext *pb,
886 float distance = FLT_MAX;
887 assert(book->dimensions);
888 for (i = 0; i < book->nentries; i++) {
889 float * vec = book->dimensions + i * book->ndimensions, d = book->pow2[i];
893 for (j = 0; j < book->ndimensions; j++)
894 d -= vec[j] * num[j];
900 if (put_codeword(pb, book, entry))
902 return &book->dimensions[entry * book->ndimensions];
905 static int residue_encode(vorbis_enc_context *venc, vorbis_enc_residue *rc,
906 PutBitContext *pb, float *coeffs, int samples,
909 int pass, i, j, p, k;
910 int psize = rc->partition_size;
911 int partitions = (rc->end - rc->begin) / psize;
912 int channels = (rc->type == 2) ? 1 : real_ch;
913 int classes[MAX_CHANNELS][NUM_RESIDUE_PARTITIONS];
914 int classwords = venc->codebooks[rc->classbook].ndimensions;
916 av_assert0(rc->type == 2);
917 av_assert0(real_ch == 2);
918 for (p = 0; p < partitions; p++) {
919 float max1 = 0.0, max2 = 0.0;
920 int s = rc->begin + p * psize;
921 for (k = s; k < s + psize; k += 2) {
922 max1 = FFMAX(max1, fabs(coeffs[ k / real_ch]));
923 max2 = FFMAX(max2, fabs(coeffs[samples + k / real_ch]));
926 for (i = 0; i < rc->classifications - 1; i++)
927 if (max1 < rc->maxes[i][0] && max2 < rc->maxes[i][1])
932 for (pass = 0; pass < 8; pass++) {
934 while (p < partitions) {
936 for (j = 0; j < channels; j++) {
937 vorbis_enc_codebook * book = &venc->codebooks[rc->classbook];
939 for (i = 0; i < classwords; i++) {
940 entry *= rc->classifications;
941 entry += classes[j][p + i];
943 if (put_codeword(pb, book, entry))
944 return AVERROR(EINVAL);
946 for (i = 0; i < classwords && p < partitions; i++, p++) {
947 for (j = 0; j < channels; j++) {
948 int nbook = rc->books[classes[j][p]][pass];
949 vorbis_enc_codebook * book = &venc->codebooks[nbook];
950 float *buf = coeffs + samples*j + rc->begin + p*psize;
954 assert(rc->type == 0 || rc->type == 2);
955 assert(!(psize % book->ndimensions));
958 for (k = 0; k < psize; k += book->ndimensions) {
960 float *a = put_vector(book, pb, &buf[k]);
962 return AVERROR(EINVAL);
963 for (l = 0; l < book->ndimensions; l++)
967 int s = rc->begin + p * psize, a1, b1;
968 a1 = (s % real_ch) * samples;
970 s = real_ch * samples;
971 for (k = 0; k < psize; k += book->ndimensions) {
972 int dim, a2 = a1, b2 = b1;
973 float vec[MAX_CODEBOOK_DIM], *pv = vec;
974 for (dim = book->ndimensions; dim--; ) {
975 *pv++ = coeffs[a2 + b2];
976 if ((a2 += samples) == s) {
981 pv = put_vector(book, pb, vec);
983 return AVERROR(EINVAL);
984 for (dim = book->ndimensions; dim--; ) {
985 coeffs[a1 + b1] -= *pv++;
986 if ((a1 += samples) == s) {
1000 static int apply_window_and_mdct(vorbis_enc_context *venc)
1003 const float * win = venc->win[1];
1004 int window_len = 1 << (venc->log2_blocksize[1] - 1);
1005 float n = (float)(1 << venc->log2_blocksize[1]) / 4.0;
1006 AVFloatDSPContext *fdsp = venc->fdsp;
1008 for (channel = 0; channel < venc->channels; channel++) {
1009 float *offset = venc->samples + channel * window_len * 2;
1011 fdsp->vector_fmul(offset, offset, win, window_len);
1012 fdsp->vector_fmul_scalar(offset, offset, 1/n, window_len);
1014 offset += window_len;
1016 fdsp->vector_fmul_reverse(offset, offset, win, window_len);
1017 fdsp->vector_fmul_scalar(offset, offset, 1/n, window_len);
1019 venc->mdct[1].mdct_calc(&venc->mdct[1], venc->coeffs + channel * window_len,
1020 venc->samples + channel * window_len * 2);
1025 /* Used for padding the last encoded packet */
1026 static AVFrame *spawn_empty_frame(AVCodecContext *avctx, int channels)
1028 AVFrame *f = av_frame_alloc();
1034 f->format = avctx->sample_fmt;
1035 f->nb_samples = avctx->frame_size;
1036 f->channel_layout = avctx->channel_layout;
1038 if (av_frame_get_buffer(f, 4)) {
1043 for (ch = 0; ch < channels; ch++) {
1044 size_t bps = av_get_bytes_per_sample(f->format);
1045 memset(f->extended_data[ch], 0, bps * f->nb_samples);
1050 /* Set up audio samples for psy analysis and window/mdct */
1051 static void move_audio(vorbis_enc_context *venc, int sf_size)
1053 AVFrame *cur = NULL;
1054 int frame_size = 1 << (venc->log2_blocksize[1] - 1);
1055 int subframes = frame_size / sf_size;
1058 /* Copy samples from last frame into current frame */
1059 if (venc->have_saved)
1060 for (ch = 0; ch < venc->channels; ch++)
1061 memcpy(venc->samples + 2 * ch * frame_size,
1062 venc->saved + ch * frame_size, sizeof(float) * frame_size);
1064 for (ch = 0; ch < venc->channels; ch++)
1065 memset(venc->samples + 2 * ch * frame_size, 0, sizeof(float) * frame_size);
1067 for (sf = 0; sf < subframes; sf++) {
1068 cur = ff_bufqueue_get(&venc->bufqueue);
1070 for (ch = 0; ch < venc->channels; ch++) {
1071 float *offset = venc->samples + 2 * ch * frame_size + frame_size;
1072 float *save = venc->saved + ch * frame_size;
1073 const float *input = (float *) cur->extended_data[ch];
1074 const size_t len = cur->nb_samples * sizeof(float);
1076 memcpy(offset + sf*sf_size, input, len);
1077 memcpy(save + sf*sf_size, input, len); // Move samples for next frame
1079 av_frame_free(&cur);
1081 venc->have_saved = 1;
1082 memcpy(venc->scratch, venc->samples, 2 * venc->channels * frame_size);
1085 static int vorbis_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
1086 const AVFrame *frame, int *got_packet_ptr)
1088 vorbis_enc_context *venc = avctx->priv_data;
1089 int i, ret, need_more;
1090 int frame_size = 1 << (venc->log2_blocksize[1] - 1);
1091 vorbis_enc_mode *mode;
1092 vorbis_enc_mapping *mapping;
1096 if ((ret = ff_af_queue_add(&venc->afq, frame)) < 0)
1098 ff_bufqueue_add(avctx, &venc->bufqueue, av_frame_clone(frame));
1100 if (!venc->afq.remaining_samples)
1103 need_more = venc->bufqueue.available * avctx->frame_size < frame_size;
1104 need_more = frame && need_more;
1108 /* Pad the bufqueue with empty frames for encoding the last packet. */
1110 if (venc->bufqueue.available * avctx->frame_size < frame_size) {
1111 int frames_needed = (frame_size/avctx->frame_size) - venc->bufqueue.available;
1114 for (i = 0; i < frames_needed; i++) {
1115 AVFrame *empty = spawn_empty_frame(avctx, venc->channels);
1117 return AVERROR(ENOMEM);
1119 ff_bufqueue_add(avctx, &venc->bufqueue, empty);
1124 move_audio(venc, avctx->frame_size);
1126 if (!apply_window_and_mdct(venc))
1129 if ((ret = ff_alloc_packet2(avctx, avpkt, 8192, 0)) < 0)
1132 init_put_bits(&pb, avpkt->data, avpkt->size);
1134 if (pb.size_in_bits - put_bits_count(&pb) < 1 + ilog(venc->nmodes - 1)) {
1135 av_log(avctx, AV_LOG_ERROR, "output buffer is too small\n");
1136 return AVERROR(EINVAL);
1139 put_bits(&pb, 1, 0); // magic bit
1141 put_bits(&pb, ilog(venc->nmodes - 1), 1); // Mode for current frame
1143 mode = &venc->modes[1];
1144 mapping = &venc->mappings[mode->mapping];
1145 if (mode->blockflag) {
1146 put_bits(&pb, 1, 1); // Previous windowflag
1147 put_bits(&pb, 1, 1); // Next windowflag
1150 for (i = 0; i < venc->channels; i++) {
1151 vorbis_enc_floor *fc = &venc->floors[mapping->floor[mapping->mux[i]]];
1152 uint16_t posts[MAX_FLOOR_VALUES];
1153 floor_fit(venc, fc, &venc->coeffs[i * frame_size], posts, frame_size);
1154 if (floor_encode(venc, fc, &pb, posts, &venc->floor[i * frame_size], frame_size)) {
1155 av_log(avctx, AV_LOG_ERROR, "output buffer is too small\n");
1156 return AVERROR(EINVAL);
1160 for (i = 0; i < venc->channels * frame_size; i++)
1161 venc->coeffs[i] /= venc->floor[i];
1163 for (i = 0; i < mapping->coupling_steps; i++) {
1164 float *mag = venc->coeffs + mapping->magnitude[i] * frame_size;
1165 float *ang = venc->coeffs + mapping->angle[i] * frame_size;
1167 for (j = 0; j < frame_size; j++) {
1177 if (residue_encode(venc, &venc->residues[mapping->residue[mapping->mux[0]]],
1178 &pb, venc->coeffs, frame_size, venc->channels)) {
1179 av_log(avctx, AV_LOG_ERROR, "output buffer is too small\n");
1180 return AVERROR(EINVAL);
1183 flush_put_bits(&pb);
1184 avpkt->size = put_bits_count(&pb) >> 3;
1186 ff_af_queue_remove(&venc->afq, frame_size, &avpkt->pts, &avpkt->duration);
1188 if (frame_size > avpkt->duration) {
1189 uint8_t *side = av_packet_new_side_data(avpkt, AV_PKT_DATA_SKIP_SAMPLES, 10);
1191 return AVERROR(ENOMEM);
1192 AV_WL32(&side[4], frame_size - avpkt->duration);
1195 *got_packet_ptr = 1;
1200 static av_cold int vorbis_encode_close(AVCodecContext *avctx)
1202 vorbis_enc_context *venc = avctx->priv_data;
1205 if (venc->codebooks)
1206 for (i = 0; i < venc->ncodebooks; i++) {
1207 av_freep(&venc->codebooks[i].lens);
1208 av_freep(&venc->codebooks[i].codewords);
1209 av_freep(&venc->codebooks[i].quantlist);
1210 av_freep(&venc->codebooks[i].dimensions);
1211 av_freep(&venc->codebooks[i].pow2);
1213 av_freep(&venc->codebooks);
1216 for (i = 0; i < venc->nfloors; i++) {
1218 if (venc->floors[i].classes)
1219 for (j = 0; j < venc->floors[i].nclasses; j++)
1220 av_freep(&venc->floors[i].classes[j].books);
1221 av_freep(&venc->floors[i].classes);
1222 av_freep(&venc->floors[i].partition_to_class);
1223 av_freep(&venc->floors[i].list);
1225 av_freep(&venc->floors);
1228 for (i = 0; i < venc->nresidues; i++) {
1229 av_freep(&venc->residues[i].books);
1230 av_freep(&venc->residues[i].maxes);
1232 av_freep(&venc->residues);
1235 for (i = 0; i < venc->nmappings; i++) {
1236 av_freep(&venc->mappings[i].mux);
1237 av_freep(&venc->mappings[i].floor);
1238 av_freep(&venc->mappings[i].residue);
1239 av_freep(&venc->mappings[i].magnitude);
1240 av_freep(&venc->mappings[i].angle);
1242 av_freep(&venc->mappings);
1244 av_freep(&venc->modes);
1246 av_freep(&venc->saved);
1247 av_freep(&venc->samples);
1248 av_freep(&venc->floor);
1249 av_freep(&venc->coeffs);
1250 av_freep(&venc->scratch);
1251 av_freep(&venc->fdsp);
1253 ff_mdct_end(&venc->mdct[0]);
1254 ff_mdct_end(&venc->mdct[1]);
1255 ff_af_queue_close(&venc->afq);
1256 ff_bufqueue_discard_all(&venc->bufqueue);
1258 av_freep(&avctx->extradata);
1263 static av_cold int vorbis_encode_init(AVCodecContext *avctx)
1265 vorbis_enc_context *venc = avctx->priv_data;
1268 if (avctx->channels != 2) {
1269 av_log(avctx, AV_LOG_ERROR, "Current FFmpeg Vorbis encoder only supports 2 channels.\n");
1273 if ((ret = create_vorbis_context(venc, avctx)) < 0)
1276 avctx->bit_rate = 0;
1277 if (avctx->flags & AV_CODEC_FLAG_QSCALE)
1278 venc->quality = avctx->global_quality / (float)FF_QP2LAMBDA;
1281 venc->quality *= venc->quality;
1283 if ((ret = put_main_header(venc, (uint8_t**)&avctx->extradata)) < 0)
1285 avctx->extradata_size = ret;
1287 avctx->frame_size = 64;
1289 ff_af_queue_init(avctx, &venc->afq);
1293 vorbis_encode_close(avctx);
1297 AVCodec ff_vorbis_encoder = {
1299 .long_name = NULL_IF_CONFIG_SMALL("Vorbis"),
1300 .type = AVMEDIA_TYPE_AUDIO,
1301 .id = AV_CODEC_ID_VORBIS,
1302 .priv_data_size = sizeof(vorbis_enc_context),
1303 .init = vorbis_encode_init,
1304 .encode2 = vorbis_encode_frame,
1305 .close = vorbis_encode_close,
1306 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_EXPERIMENTAL,
1307 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLTP,
1308 AV_SAMPLE_FMT_NONE },