2 * copyright (c) 2006 Oded Shimon <ods15@ods15.dyndns.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 * Native Vorbis encoder.
22 * @author Oded Shimon <ods15@ods15.dyndns.org>
27 #define BITSTREAM_H // don't include this
29 typedef int GetBitContext;
35 //#define ALT_BITSTREAM_WRITER
36 //#include "bitstream.h"
64 int * partition_to_class;
66 floor_class_t * classes;
70 struct { int x; } * list;
98 int blocksize[2]; // in (1<<n) format
100 const float * win[2];
104 float * floor; // also used for tmp values for mdct
105 float * coeffs; // also used for residue after floor
108 codebook_t * codebooks;
114 residue_t * residues;
117 mapping_t * mappings;
120 vorbis_mode_t * modes;
130 static inline void init_put_bits(PutBitContext * pb, uint8_t * buf, int buffer_len) {
131 pb->total = buffer_len * 8;
137 static void put_bits(PutBitContext * pb, int bits, uint64_t val) {
138 if ((pb->total_pos += bits) >= pb->total) return;
141 if (pb->pos > bits) {
142 *pb->buf_ptr |= val << (8 - pb->pos);
146 *pb->buf_ptr++ |= (val << (8 - pb->pos)) & 0xFF;
152 for (; bits >= 8; bits -= 8) {
153 *pb->buf_ptr++ = val & 0xFF;
162 static inline void flush_put_bits(PutBitContext * pb) {
165 static inline int put_bits_count(PutBitContext * pb) {
166 return pb->total_pos;
169 static int cb_lookup_vals(int lookup, int dimentions, int entries) {
172 for (tmp = 0; ; tmp++) {
174 for (i = 0; i < dimentions; i++) n *= tmp;
175 if (n > entries) break;
178 } else if (lookup == 2) return dimentions * entries;
182 static void ready_codebook(codebook_t * cb) {
183 int h[33] = { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
186 for (i = 0; i < cb->nentries; i++) {
187 cb_entry_t * e = &cb->entries[i];
190 else for (j = e->len; !h[j]; j--) assert(j);
193 for (j++; j <= e->len; j++) h[j] = e->codeword | (1 << (j - 1));
195 for (i = 0; i < 33; i++) assert(!h[i]);
197 if (!cb->lookup) cb->dimentions = NULL;
199 int vals = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries);
200 cb->dimentions = av_malloc(sizeof(float) * cb->nentries * cb->ndimentions);
201 for (i = 0; i < cb->nentries; i++) {
205 for (j = 0; j < cb->ndimentions; j++) {
207 if (cb->lookup == 1) off = (i / div) % vals; // lookup type 1
208 else off = i * cb->ndimentions + j; // lookup type 2
210 cb->dimentions[i * cb->ndimentions + j] = last + cb->min + cb->quantlist[off] * cb->delta;
211 if (cb->seq_p) last = cb->dimentions[i * cb->ndimentions + j];
219 static void create_vorbis_context(venc_context_t * venc, AVCodecContext * avccontext) {
226 venc->channels = avccontext->channels;
227 venc->sample_rate = avccontext->sample_rate;
228 venc->blocksize[0] = venc->blocksize[1] = 8;
230 venc->ncodebooks = 10;
231 venc->codebooks = av_malloc(sizeof(codebook_t) * venc->ncodebooks);
233 // codebook 0 - floor1 book, values 0..255
234 cb = &venc->codebooks[0];
236 cb->entries = av_malloc(sizeof(cb_entry_t) * cb->nentries);
237 for (i = 0; i < cb->nentries; i++) cb->entries[i].len = 8;
243 cb->quantlist = NULL;
246 // codebook 1 - residue classbook, values 0..1, dimentions 4
247 cb = &venc->codebooks[1];
249 cb->entries = av_malloc(sizeof(cb_entry_t) * cb->nentries);
250 for (i = 0; i < cb->nentries; i++) cb->entries[i].len = 1;
256 cb->quantlist = NULL;
259 // codebook 2..9 - vector, for the residue, values -32767..32767, dimentions 1
260 for (book = 0; book < 8; book++) {
261 cb = &venc->codebooks[2 + book];
263 cb->entries = av_malloc(sizeof(cb_entry_t) * cb->nentries);
264 for (i = 0; i < cb->nentries; i++) cb->entries[i].len = i == 2 ? 1 : 3;
266 cb->delta = 1 << ((7 - book) * 2);
267 cb->min = -cb->delta*2;
270 cb->quantlist = av_malloc(sizeof(int) * cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries));
271 for (i = 0; i < cb->nentries; i++) cb->quantlist[i] = i;
276 venc->floors = av_malloc(sizeof(floor_t) * venc->nfloors);
279 fc = &venc->floors[0];
281 fc->partition_to_class = av_malloc(sizeof(int) * fc->partitions);
282 for (i = 0; i < fc->partitions; i++) fc->partition_to_class[i] = 0;
284 fc->classes = av_malloc(sizeof(floor_class_t) * fc->nclasses);
285 for (i = 0; i < fc->nclasses; i++) {
286 floor_class_t * c = &fc->classes[i];
291 books = (1 << c->subclass);
292 c->books = av_malloc(sizeof(int) * books);
293 for (j = 0; j < books; j++) c->books[j] = 0;
296 fc->rangebits = venc->blocksize[0];
299 for (i = 0; i < fc->partitions; i++)
300 fc->values += fc->classes[fc->partition_to_class[i]].dim;
302 fc->list = av_malloc(sizeof(*fc->list) * fc->values);
304 fc->list[1].x = 1 << fc->rangebits;
305 for (i = 2; i < fc->values; i++) fc->list[i].x = i * 5;
308 venc->residues = av_malloc(sizeof(residue_t) * venc->nresidues);
311 rc = &venc->residues[0];
314 rc->end = 1 << (venc->blocksize[0] - 1);
315 rc->partition_size = 64;
316 rc->classifications = 1;
318 rc->books = av_malloc(sizeof(int[8]) * rc->classifications);
319 for (i = 0; i < 8; i++) rc->books[0][i] = 2 + i;
322 venc->mappings = av_malloc(sizeof(mapping_t) * venc->nmappings);
325 mc = &venc->mappings[0];
327 mc->mux = av_malloc(sizeof(int) * venc->channels);
328 for (i = 0; i < venc->channels; i++) mc->mux[i] = 0;
329 mc->floor = av_malloc(sizeof(int) * mc->submaps);
330 mc->residue = av_malloc(sizeof(int) * mc->submaps);
331 for (i = 0; i < mc->submaps; i++) {
337 venc->modes = av_malloc(sizeof(vorbis_mode_t) * venc->nmodes);
340 venc->modes[0].blockflag = 0;
341 venc->modes[0].mapping = 0;
343 venc->have_saved = 0;
344 venc->saved = av_malloc(sizeof(float) * venc->channels * (1 << venc->blocksize[1]) / 2);
345 venc->samples = av_malloc(sizeof(float) * venc->channels * (1 << venc->blocksize[1]));
346 venc->floor = av_malloc(sizeof(float) * venc->channels * (1 << venc->blocksize[1]) / 2);
347 venc->coeffs = av_malloc(sizeof(float) * venc->channels * (1 << venc->blocksize[1]) / 2);
350 const float *vwin[8]={ vwin64, vwin128, vwin256, vwin512, vwin1024, vwin2048, vwin4096, vwin8192 };
351 venc->win[0] = vwin[venc->blocksize[0] - 6];
352 venc->win[1] = vwin[venc->blocksize[1] - 6];
355 ff_mdct_init(&venc->mdct[0], venc->blocksize[0], 0);
356 ff_mdct_init(&venc->mdct[1], venc->blocksize[1], 0);
359 static inline int ilog(unsigned int a) {
361 for (i = 0; a >> i; i++);
365 static void put_float(PutBitContext * pb, float f) {
368 mant = (int)ldexp(frexp(f, &exp), 20);
370 if (mant < 0) { res |= (1 << 31); mant = -mant; }
371 res |= mant | (exp << 21);
372 put_bits(pb, 32, res);
375 static void put_codebook_header(PutBitContext * pb, codebook_t * cb) {
379 put_bits(pb, 24, 0x564342); //magic
380 put_bits(pb, 16, cb->ndimentions);
381 put_bits(pb, 24, cb->nentries);
383 for (i = 1; i < cb->nentries; i++) if (cb->entries[i].len < cb->entries[i-1].len) break;
384 if (i == cb->nentries) ordered = 1;
386 put_bits(pb, 1, ordered);
388 int len = cb->entries[0].len;
389 put_bits(pb, 5, len);
391 while (i < cb->nentries) {
393 for (j = 0; j+i < cb->nentries; j++) if (cb->entries[j+i].len != len) break;
394 put_bits(pb, ilog(cb->nentries - i), j);
400 for (i = 0; i < cb->nentries; i++) if (!cb->entries[i].len) break;
401 if (i != cb->nentries) sparse = 1;
402 put_bits(pb, 1, sparse);
404 for (i = 0; i < cb->nentries; i++) {
405 if (sparse) put_bits(pb, 1, !!cb->entries[i].len);
406 if (cb->entries[i].len) put_bits(pb, 5, cb->entries[i].len - 1);
410 put_bits(pb, 4, cb->lookup);
412 int tmp = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries);
413 int bits = ilog(cb->quantlist[0]);
415 for (i = 1; i < tmp; i++) bits = FFMAX(bits, ilog(cb->quantlist[i]));
417 put_float(pb, cb->min);
418 put_float(pb, cb->delta);
420 put_bits(pb, 4, bits - 1);
421 put_bits(pb, 1, cb->seq_p);
423 for (i = 0; i < tmp; i++) put_bits(pb, bits, cb->quantlist[i]);
427 static void put_floor_header(PutBitContext * pb, floor_t * fc) {
430 put_bits(pb, 16, 1); // type, only floor1 is supported
432 put_bits(pb, 5, fc->partitions);
434 for (i = 0; i < fc->partitions; i++) put_bits(pb, 4, fc->partition_to_class[i]);
436 for (i = 0; i < fc->nclasses; i++) {
439 put_bits(pb, 3, fc->classes[i].dim - 1);
440 put_bits(pb, 2, fc->classes[i].subclass);
442 if (fc->classes[i].subclass) put_bits(pb, 8, fc->classes[i].masterbook);
444 books = (1 << fc->classes[i].subclass);
446 for (j = 0; j < books; j++) put_bits(pb, 8, fc->classes[i].books[j] + 1);
449 put_bits(pb, 2, fc->multiplier - 1);
450 put_bits(pb, 4, fc->rangebits);
452 for (i = 2; i < fc->values; i++) put_bits(pb, fc->rangebits, fc->list[i].x);
455 static void put_residue_header(PutBitContext * pb, residue_t * rc) {
458 put_bits(pb, 16, rc->type);
460 put_bits(pb, 24, rc->begin);
461 put_bits(pb, 24, rc->end);
462 put_bits(pb, 24, rc->partition_size - 1);
463 put_bits(pb, 6, rc->classifications - 1);
464 put_bits(pb, 8, rc->classbook);
466 for (i = 0; i < rc->classifications; i++) {
468 for (j = 0; j < 8; j++) tmp |= (!!rc->books[i][j]) << j;
470 put_bits(pb, 3, tmp & 7);
471 put_bits(pb, 1, tmp > 7);
473 if (tmp > 7) put_bits(pb, 5, tmp >> 3);
476 for (i = 0; i < rc->classifications; i++) {
478 for (j = 0; j < 8; j++)
480 put_bits(pb, 8, rc->books[i][j]);
484 static int put_main_header(venc_context_t * venc, uint8_t ** out) {
487 uint8_t buffer[50000] = {0}, * p = buffer;
488 int buffer_len = sizeof buffer;
491 // identification header
492 init_put_bits(&pb, p, buffer_len);
493 put_bits(&pb, 8, 1); //magic
494 for (i = 0; "vorbis"[i]; i++) put_bits(&pb, 8, "vorbis"[i]);
495 put_bits(&pb, 32, 0); // version
496 put_bits(&pb, 8, venc->channels);
497 put_bits(&pb, 32, venc->sample_rate);
498 put_bits(&pb, 32, 0); // bitrate
499 put_bits(&pb, 32, 0); // bitrate
500 put_bits(&pb, 32, 0); // bitrate
501 put_bits(&pb, 4, venc->blocksize[0]);
502 put_bits(&pb, 4, venc->blocksize[1]);
503 put_bits(&pb, 1, 1); // framing
506 hlens[0] = (put_bits_count(&pb) + 7) / 8;
507 buffer_len -= hlens[0];
511 init_put_bits(&pb, p, buffer_len);
512 put_bits(&pb, 8, 3); //magic
513 for (i = 0; "vorbis"[i]; i++) put_bits(&pb, 8, "vorbis"[i]);
514 put_bits(&pb, 32, 0); // vendor length TODO
515 put_bits(&pb, 32, 0); // amount of comments
516 put_bits(&pb, 1, 1); // framing
519 hlens[1] = (put_bits_count(&pb) + 7) / 8;
520 buffer_len -= hlens[1];
524 init_put_bits(&pb, p, buffer_len);
525 put_bits(&pb, 8, 5); //magic
526 for (i = 0; "vorbis"[i]; i++) put_bits(&pb, 8, "vorbis"[i]);
529 put_bits(&pb, 8, venc->ncodebooks - 1);
530 for (i = 0; i < venc->ncodebooks; i++) put_codebook_header(&pb, &venc->codebooks[i]);
532 // time domain, reserved, zero
534 put_bits(&pb, 16, 0);
537 put_bits(&pb, 6, venc->nfloors - 1);
538 for (i = 0; i < venc->nfloors; i++) put_floor_header(&pb, &venc->floors[i]);
541 put_bits(&pb, 6, venc->nresidues - 1);
542 for (i = 0; i < venc->nresidues; i++) put_residue_header(&pb, &venc->residues[i]);
545 put_bits(&pb, 6, venc->nmappings - 1);
546 for (i = 0; i < venc->nmappings; i++) {
547 mapping_t * mc = &venc->mappings[i];
549 put_bits(&pb, 16, 0); // mapping type
551 put_bits(&pb, 1, mc->submaps > 1);
552 if (mc->submaps > 1) put_bits(&pb, 4, mc->submaps - 1);
554 put_bits(&pb, 1, 0); // channel coupling
556 put_bits(&pb, 2, 0); // reserved
558 if (mc->submaps > 1) for (j = 0; j < venc->channels; j++) put_bits(&pb, 4, mc->mux[j]);
560 for (j = 0; j < mc->submaps; j++) {
561 put_bits(&pb, 8, 0); // reserved time configuration
562 put_bits(&pb, 8, mc->floor[j]);
563 put_bits(&pb, 8, mc->residue[j]);
568 put_bits(&pb, 6, venc->nmodes - 1);
569 for (i = 0; i < venc->nmodes; i++) {
570 put_bits(&pb, 1, venc->modes[i].blockflag);
571 put_bits(&pb, 16, 0); // reserved window type
572 put_bits(&pb, 16, 0); // reserved transform type
573 put_bits(&pb, 8, venc->modes[i].mapping);
576 put_bits(&pb, 1, 1); // framing
579 hlens[2] = (put_bits_count(&pb) + 7) / 8;
581 len = hlens[0] + hlens[1] + hlens[2];
582 p = *out = av_mallocz(64 + len + len/255);
585 p += av_xiphlacing(p, hlens[0]);
586 p += av_xiphlacing(p, hlens[1]);
588 for (i = 0; i < 3; i++) {
589 memcpy(p, buffer + buffer_len, hlens[i]);
591 buffer_len += hlens[i];
597 static int vorbis_encode_init(AVCodecContext * avccontext)
599 venc_context_t * venc = avccontext->priv_data;
601 create_vorbis_context(venc, avccontext);
603 //if (avccontext->flags & CODEC_FLAG_QSCALE) avccontext->global_quality / (float)FF_QP2LAMBDA); else avccontext->bit_rate;
604 //if(avccontext->cutoff > 0) cfreq = avccontext->cutoff / 1000.0;
606 avccontext->extradata_size = put_main_header(venc, (uint8_t**)&avccontext->extradata);
608 avccontext->frame_size = 1 << (venc->blocksize[0] - 1);
610 avccontext->coded_frame = avcodec_alloc_frame();
611 avccontext->coded_frame->key_frame = 1;
616 static int window(venc_context_t * venc, signed short * audio, int samples) {
618 const float * win = venc->win[0];
619 int window_len = 1 << (venc->blocksize[0] - 1);
622 if (!venc->have_saved && !samples) return 0;
624 if (venc->have_saved) {
625 for (channel = 0; channel < venc->channels; channel++) {
626 memcpy(venc->samples + channel*window_len*2, venc->saved + channel*window_len, sizeof(float)*window_len);
629 for (channel = 0; channel < venc->channels; channel++) {
630 memset(venc->samples + channel*window_len*2, 0, sizeof(float)*window_len);
635 for (channel = 0; channel < venc->channels; channel++) {
636 float * offset = venc->samples + channel*window_len*2 + window_len;
638 for (i = 0; i < samples; i++, j += venc->channels)
639 offset[i] = audio[j] / 32768. * win[window_len - i];
642 for (channel = 0; channel < venc->channels; channel++) {
643 memset(venc->samples + channel*window_len*2 + window_len, 0, sizeof(float)*window_len);
647 for (channel = 0; channel < venc->channels; channel++) {
648 ff_mdct_calc(&venc->mdct[0], venc->coeffs + channel*window_len, venc->samples + channel*window_len*2, venc->floor/*tmp*/);
652 for (channel = 0; channel < venc->channels; channel++) {
653 float * offset = venc->saved + channel*window_len;
655 for (i = 0; i < samples; i++, j += venc->channels)
656 offset[i] = audio[j] / 32768. * win[i];
658 venc->have_saved = 1;
660 venc->have_saved = 0;
665 static float put_vector(codebook_t * book, PutBitContext * pb, float num) {
669 assert(book->dimentions);
670 assert(book->ndimentions == 1);
671 for (i = 0; i < book->nentries; i++) {
672 float d = (book->dimentions[i] - num)*(book->dimentions[i] - num);
673 if (entry == -1 || distance > d) {
678 put_bits(pb, book->entries[entry].len, book->entries[entry].codeword);
679 return book->dimentions[entry];
682 static void residue_encode(venc_context_t * venc, residue_t * rc, PutBitContext * pb, float * coeffs, int samples, int channels) {
683 int pass, i, j, p, k;
684 int psize = rc->partition_size;
685 int partitions = (rc->end - rc->begin) / psize;
686 int classes[channels][partitions];
687 int classwords = venc->codebooks[rc->classbook].ndimentions;
689 for (pass = 0; pass < 8; pass++) {
691 while (p < partitions) {
692 if (pass == 0) for (j = 0; j < channels; j++) {
693 codebook_t * book = &venc->codebooks[rc->classbook];
695 put_bits(pb, book->entries[entry].len, book->entries[entry].codeword);
696 for (i = classwords; i--; ) {
697 classes[j][p + i] = entry % rc->classifications;
698 entry /= rc->classifications;
701 for (i = 0; i < classwords && p < partitions; i++, p++) {
702 for (j = 0; j < channels; j++) {
703 int nbook = rc->books[classes[j][p]][pass];
704 codebook_t * book = &venc->codebooks[nbook];
705 float * buf = coeffs + samples*j + rc->begin + p*psize;
707 assert(rc->type == 0);
708 assert(book->ndimentions == 1);
710 for (k = 0; k < psize; k++) {
711 buf[k] -= put_vector(book, pb, buf[k]);
719 static int vorbis_encode_frame(AVCodecContext * avccontext, unsigned char * packets, int buf_size, void *data)
721 venc_context_t * venc = avccontext->priv_data;
722 signed short * audio = data;
723 int samples = data ? avccontext->frame_size : 0;
724 vorbis_mode_t * mode;
729 if (!window(venc, audio, samples)) return 0;
731 init_put_bits(&pb, packets, buf_size);
733 put_bits(&pb, 1, 0); // magic bit
735 put_bits(&pb, ilog(venc->nmodes - 1), 0); // 0 bits, the mode
737 mode = &venc->modes[0];
738 mapping = &venc->mappings[mode->mapping];
739 if (mode->blockflag) {
744 for (i = 0; i < venc->channels; i++) {
745 floor_t * fc = &venc->floors[mapping->floor[mapping->mux[i]]];
746 int range = 255 / fc->multiplier + 1;
748 put_bits(&pb, 1, 1); // non zero
749 put_bits(&pb, ilog(range - 1), 113); // magic value - 3.7180282E-05
750 put_bits(&pb, ilog(range - 1), 113); // both sides of X
751 for (j = 0; j < fc->partitions; j++) {
752 floor_class_t * c = &fc->classes[fc->partition_to_class[j]];
753 codebook_t * book = &venc->codebooks[c->books[0]];
756 for (k = 0; k < c->dim; k++) {
757 put_bits(&pb, book->entries[entry].len, book->entries[entry].codeword);
761 for (j = 0; j < samples; j++) {
762 venc->floor[i * samples + j] = floor1_inverse_db_table[220];
766 for (i = 0; i < venc->channels; i++) {
768 for (j = 0; j < samples; j++) {
769 venc->coeffs[i * samples + j] /= venc->floor[i * samples + j];
773 residue_encode(venc, &venc->residues[mapping->residue[mapping->mux[0]]], &pb, venc->coeffs, samples, venc->channels);
775 return (put_bits_count(&pb) + 7) / 8;
779 static int vorbis_encode_close(AVCodecContext * avccontext)
781 venc_context_t * venc = avccontext->priv_data;
784 if (venc->codebooks) for (i = 0; i < venc->ncodebooks; i++) {
785 av_freep(&venc->codebooks[i].entries);
786 av_freep(&venc->codebooks[i].quantlist);
787 av_freep(&venc->codebooks[i].dimentions);
789 av_freep(&venc->codebooks);
791 if (venc->floors) for (i = 0; i < venc->nfloors; i++) {
793 av_freep(&venc->floors[i].classes);
794 if (venc->floors[i].classes)
795 for (j = 0; j < venc->floors[i].nclasses; j++)
796 av_freep(&venc->floors[i].classes[j].books);
797 av_freep(&venc->floors[i].partition_to_class);
798 av_freep(&venc->floors[i].list);
800 av_freep(&venc->floors);
802 if (venc->residues) for (i = 0; i < venc->nresidues; i++) {
803 av_freep(&venc->residues[i].books);
805 av_freep(&venc->residues);
807 if (venc->mappings) for (i = 0; i < venc->nmappings; i++) {
808 av_freep(&venc->mappings[i].mux);
809 av_freep(&venc->mappings[i].floor);
810 av_freep(&venc->mappings[i].residue);
812 av_freep(&venc->mappings);
814 av_freep(&venc->modes);
816 av_freep(&venc->saved);
817 av_freep(&venc->samples);
818 av_freep(&venc->floor);
819 av_freep(&venc->coeffs);
821 ff_mdct_end(&venc->mdct[0]);
822 ff_mdct_end(&venc->mdct[1]);
824 av_freep(&avccontext->coded_frame);
825 av_freep(&avccontext->extradata);
830 AVCodec oggvorbis_encoder = {
834 sizeof(venc_context_t),
838 .capabilities= CODEC_CAP_DELAY,