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 #define ilog(i) av_log2(2*(i))
132 static inline void init_put_bits(PutBitContext * pb, uint8_t * buf, int buffer_len) {
133 pb->total = buffer_len * 8;
139 static void put_bits(PutBitContext * pb, int bits, uint64_t val) {
140 if ((pb->total_pos += bits) >= pb->total) return;
143 if (pb->pos > bits) {
144 *pb->buf_ptr |= val << (8 - pb->pos);
148 *pb->buf_ptr++ |= (val << (8 - pb->pos)) & 0xFF;
154 for (; bits >= 8; bits -= 8) {
155 *pb->buf_ptr++ = val & 0xFF;
164 static inline void flush_put_bits(PutBitContext * pb) {
167 static inline int put_bits_count(PutBitContext * pb) {
168 return pb->total_pos;
171 static int cb_lookup_vals(int lookup, int dimentions, int entries) {
174 for (tmp = 0; ; tmp++) {
176 for (i = 0; i < dimentions; i++) n *= tmp;
177 if (n > entries) break;
180 } else if (lookup == 2) return dimentions * entries;
184 static void ready_codebook(codebook_t * cb) {
185 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 };
188 for (i = 0; i < cb->nentries; i++) {
189 cb_entry_t * e = &cb->entries[i];
193 for (j = e->len; j; j--)
199 for (j++; j <= e->len; j++) h[j] = e->codeword | (1 << (j - 1));
201 for (i = 0; i < 33; i++) assert(!h[i]);
203 if (!cb->lookup) cb->dimentions = NULL;
205 int vals = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries);
206 cb->dimentions = av_malloc(sizeof(float) * cb->nentries * cb->ndimentions);
207 for (i = 0; i < cb->nentries; i++) {
211 for (j = 0; j < cb->ndimentions; j++) {
213 if (cb->lookup == 1) off = (i / div) % vals; // lookup type 1
214 else off = i * cb->ndimentions + j; // lookup type 2
216 cb->dimentions[i * cb->ndimentions + j] = last + cb->min + cb->quantlist[off] * cb->delta;
217 if (cb->seq_p) last = cb->dimentions[i * cb->ndimentions + j];
225 static void create_vorbis_context(venc_context_t * venc, AVCodecContext * avccontext) {
232 venc->channels = avccontext->channels;
233 venc->sample_rate = avccontext->sample_rate;
234 venc->blocksize[0] = venc->blocksize[1] = 8;
236 venc->ncodebooks = 10;
237 venc->codebooks = av_malloc(sizeof(codebook_t) * venc->ncodebooks);
239 // codebook 0 - floor1 book, values 0..255
240 cb = &venc->codebooks[0];
242 cb->entries = av_malloc(sizeof(cb_entry_t) * cb->nentries);
243 for (i = 0; i < cb->nentries; i++) cb->entries[i].len = 8;
249 cb->quantlist = NULL;
252 // codebook 1 - residue classbook, values 0..1, dimentions 4
253 cb = &venc->codebooks[1];
255 cb->entries = av_malloc(sizeof(cb_entry_t) * cb->nentries);
256 for (i = 0; i < cb->nentries; i++) cb->entries[i].len = 1;
262 cb->quantlist = NULL;
265 // codebook 2..9 - vector, for the residue, values -32767..32767, dimentions 1
266 for (book = 0; book < 8; book++) {
267 cb = &venc->codebooks[2 + book];
269 cb->entries = av_malloc(sizeof(cb_entry_t) * cb->nentries);
270 for (i = 0; i < cb->nentries; i++) cb->entries[i].len = i == 2 ? 1 : 3;
272 cb->delta = 1 << ((7 - book) * 2);
273 cb->min = -cb->delta*2;
276 cb->quantlist = av_malloc(sizeof(int) * cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries));
277 for (i = 0; i < cb->nentries; i++) cb->quantlist[i] = i;
282 venc->floors = av_malloc(sizeof(floor_t) * venc->nfloors);
285 fc = &venc->floors[0];
287 fc->partition_to_class = av_malloc(sizeof(int) * fc->partitions);
288 for (i = 0; i < fc->partitions; i++) fc->partition_to_class[i] = 0;
290 fc->classes = av_malloc(sizeof(floor_class_t) * fc->nclasses);
291 for (i = 0; i < fc->nclasses; i++) {
292 floor_class_t * c = &fc->classes[i];
297 books = (1 << c->subclass);
298 c->books = av_malloc(sizeof(int) * books);
299 for (j = 0; j < books; j++) c->books[j] = 0;
302 fc->rangebits = venc->blocksize[0] - 1;
305 for (i = 0; i < fc->partitions; i++)
306 fc->values += fc->classes[fc->partition_to_class[i]].dim;
308 fc->list = av_malloc(sizeof(*fc->list) * fc->values);
310 fc->list[1].x = 1 << fc->rangebits;
311 for (i = 2; i < fc->values; i++) fc->list[i].x = i * 5;
314 venc->residues = av_malloc(sizeof(residue_t) * venc->nresidues);
317 rc = &venc->residues[0];
320 rc->end = 1 << (venc->blocksize[0] - 1);
321 rc->partition_size = 64;
322 rc->classifications = 2;
324 rc->books = av_malloc(sizeof(int[8]) * rc->classifications);
325 for (i = 0; i < rc->classifications; i++) {
327 for (j = 0; j < 8; j++) rc->books[i][j] = 2 + j;
328 rc->books[i][0] = rc->books[i][1] = rc->books[i][2] = rc->books[i][3] = -1;
332 venc->mappings = av_malloc(sizeof(mapping_t) * venc->nmappings);
335 mc = &venc->mappings[0];
337 mc->mux = av_malloc(sizeof(int) * venc->channels);
338 for (i = 0; i < venc->channels; i++) mc->mux[i] = 0;
339 mc->floor = av_malloc(sizeof(int) * mc->submaps);
340 mc->residue = av_malloc(sizeof(int) * mc->submaps);
341 for (i = 0; i < mc->submaps; i++) {
347 venc->modes = av_malloc(sizeof(vorbis_mode_t) * venc->nmodes);
350 venc->modes[0].blockflag = 0;
351 venc->modes[0].mapping = 0;
353 venc->have_saved = 0;
354 venc->saved = av_malloc(sizeof(float) * venc->channels * (1 << venc->blocksize[1]) / 2);
355 venc->samples = av_malloc(sizeof(float) * venc->channels * (1 << venc->blocksize[1]));
356 venc->floor = av_malloc(sizeof(float) * venc->channels * (1 << venc->blocksize[1]) / 2);
357 venc->coeffs = av_malloc(sizeof(float) * venc->channels * (1 << venc->blocksize[1]) / 2);
360 const float *vwin[8]={ vwin64, vwin128, vwin256, vwin512, vwin1024, vwin2048, vwin4096, vwin8192 };
361 venc->win[0] = vwin[venc->blocksize[0] - 6];
362 venc->win[1] = vwin[venc->blocksize[1] - 6];
365 ff_mdct_init(&venc->mdct[0], venc->blocksize[0], 0);
366 ff_mdct_init(&venc->mdct[1], venc->blocksize[1], 0);
369 static void put_float(PutBitContext * pb, float f) {
372 mant = (int)ldexp(frexp(f, &exp), 20);
374 if (mant < 0) { res |= (1 << 31); mant = -mant; }
375 res |= mant | (exp << 21);
376 put_bits(pb, 32, res);
379 static void put_codebook_header(PutBitContext * pb, codebook_t * cb) {
383 put_bits(pb, 24, 0x564342); //magic
384 put_bits(pb, 16, cb->ndimentions);
385 put_bits(pb, 24, cb->nentries);
387 for (i = 1; i < cb->nentries; i++) if (cb->entries[i].len < cb->entries[i-1].len) break;
388 if (i == cb->nentries) ordered = 1;
390 put_bits(pb, 1, ordered);
392 int len = cb->entries[0].len;
393 put_bits(pb, 5, len - 1);
395 while (i < cb->nentries) {
397 for (j = 0; j+i < cb->nentries; j++) if (cb->entries[j+i].len != len) break;
398 put_bits(pb, ilog(cb->nentries - i), j);
404 for (i = 0; i < cb->nentries; i++) if (!cb->entries[i].len) break;
405 if (i != cb->nentries) sparse = 1;
406 put_bits(pb, 1, sparse);
408 for (i = 0; i < cb->nentries; i++) {
409 if (sparse) put_bits(pb, 1, !!cb->entries[i].len);
410 if (cb->entries[i].len) put_bits(pb, 5, cb->entries[i].len - 1);
414 put_bits(pb, 4, cb->lookup);
416 int tmp = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries);
417 int bits = ilog(cb->quantlist[0]);
419 for (i = 1; i < tmp; i++) bits = FFMAX(bits, ilog(cb->quantlist[i]));
421 put_float(pb, cb->min);
422 put_float(pb, cb->delta);
424 put_bits(pb, 4, bits - 1);
425 put_bits(pb, 1, cb->seq_p);
427 for (i = 0; i < tmp; i++) put_bits(pb, bits, cb->quantlist[i]);
431 static void put_floor_header(PutBitContext * pb, floor_t * fc) {
434 put_bits(pb, 16, 1); // type, only floor1 is supported
436 put_bits(pb, 5, fc->partitions);
438 for (i = 0; i < fc->partitions; i++) put_bits(pb, 4, fc->partition_to_class[i]);
440 for (i = 0; i < fc->nclasses; i++) {
443 put_bits(pb, 3, fc->classes[i].dim - 1);
444 put_bits(pb, 2, fc->classes[i].subclass);
446 if (fc->classes[i].subclass) put_bits(pb, 8, fc->classes[i].masterbook);
448 books = (1 << fc->classes[i].subclass);
450 for (j = 0; j < books; j++) put_bits(pb, 8, fc->classes[i].books[j] + 1);
453 put_bits(pb, 2, fc->multiplier - 1);
454 put_bits(pb, 4, fc->rangebits);
456 for (i = 2; i < fc->values; i++) put_bits(pb, fc->rangebits, fc->list[i].x);
459 static void put_residue_header(PutBitContext * pb, residue_t * rc) {
462 put_bits(pb, 16, rc->type);
464 put_bits(pb, 24, rc->begin);
465 put_bits(pb, 24, rc->end);
466 put_bits(pb, 24, rc->partition_size - 1);
467 put_bits(pb, 6, rc->classifications - 1);
468 put_bits(pb, 8, rc->classbook);
470 for (i = 0; i < rc->classifications; i++) {
472 for (j = 0; j < 8; j++) tmp |= (rc->books[i][j] != -1) << j;
474 put_bits(pb, 3, tmp & 7);
475 put_bits(pb, 1, tmp > 7);
477 if (tmp > 7) put_bits(pb, 5, tmp >> 3);
480 for (i = 0; i < rc->classifications; i++) {
482 for (j = 0; j < 8; j++)
483 if (rc->books[i][j] != -1)
484 put_bits(pb, 8, rc->books[i][j]);
488 static int put_main_header(venc_context_t * venc, uint8_t ** out) {
491 uint8_t buffer[50000] = {0}, * p = buffer;
492 int buffer_len = sizeof buffer;
495 // identification header
496 init_put_bits(&pb, p, buffer_len);
497 put_bits(&pb, 8, 1); //magic
498 for (i = 0; "vorbis"[i]; i++) put_bits(&pb, 8, "vorbis"[i]);
499 put_bits(&pb, 32, 0); // version
500 put_bits(&pb, 8, venc->channels);
501 put_bits(&pb, 32, venc->sample_rate);
502 put_bits(&pb, 32, 0); // bitrate
503 put_bits(&pb, 32, 0); // bitrate
504 put_bits(&pb, 32, 0); // bitrate
505 put_bits(&pb, 4, venc->blocksize[0]);
506 put_bits(&pb, 4, venc->blocksize[1]);
507 put_bits(&pb, 1, 1); // framing
510 hlens[0] = (put_bits_count(&pb) + 7) / 8;
511 buffer_len -= hlens[0];
515 init_put_bits(&pb, p, buffer_len);
516 put_bits(&pb, 8, 3); //magic
517 for (i = 0; "vorbis"[i]; i++) put_bits(&pb, 8, "vorbis"[i]);
518 put_bits(&pb, 32, 0); // vendor length TODO
519 put_bits(&pb, 32, 0); // amount of comments
520 put_bits(&pb, 1, 1); // framing
523 hlens[1] = (put_bits_count(&pb) + 7) / 8;
524 buffer_len -= hlens[1];
528 init_put_bits(&pb, p, buffer_len);
529 put_bits(&pb, 8, 5); //magic
530 for (i = 0; "vorbis"[i]; i++) put_bits(&pb, 8, "vorbis"[i]);
533 put_bits(&pb, 8, venc->ncodebooks - 1);
534 for (i = 0; i < venc->ncodebooks; i++) put_codebook_header(&pb, &venc->codebooks[i]);
536 // time domain, reserved, zero
538 put_bits(&pb, 16, 0);
541 put_bits(&pb, 6, venc->nfloors - 1);
542 for (i = 0; i < venc->nfloors; i++) put_floor_header(&pb, &venc->floors[i]);
545 put_bits(&pb, 6, venc->nresidues - 1);
546 for (i = 0; i < venc->nresidues; i++) put_residue_header(&pb, &venc->residues[i]);
549 put_bits(&pb, 6, venc->nmappings - 1);
550 for (i = 0; i < venc->nmappings; i++) {
551 mapping_t * mc = &venc->mappings[i];
553 put_bits(&pb, 16, 0); // mapping type
555 put_bits(&pb, 1, mc->submaps > 1);
556 if (mc->submaps > 1) put_bits(&pb, 4, mc->submaps - 1);
558 put_bits(&pb, 1, 0); // channel coupling
560 put_bits(&pb, 2, 0); // reserved
562 if (mc->submaps > 1) for (j = 0; j < venc->channels; j++) put_bits(&pb, 4, mc->mux[j]);
564 for (j = 0; j < mc->submaps; j++) {
565 put_bits(&pb, 8, 0); // reserved time configuration
566 put_bits(&pb, 8, mc->floor[j]);
567 put_bits(&pb, 8, mc->residue[j]);
572 put_bits(&pb, 6, venc->nmodes - 1);
573 for (i = 0; i < venc->nmodes; i++) {
574 put_bits(&pb, 1, venc->modes[i].blockflag);
575 put_bits(&pb, 16, 0); // reserved window type
576 put_bits(&pb, 16, 0); // reserved transform type
577 put_bits(&pb, 8, venc->modes[i].mapping);
580 put_bits(&pb, 1, 1); // framing
583 hlens[2] = (put_bits_count(&pb) + 7) / 8;
585 len = hlens[0] + hlens[1] + hlens[2];
586 p = *out = av_mallocz(64 + len + len/255);
589 p += av_xiphlacing(p, hlens[0]);
590 p += av_xiphlacing(p, hlens[1]);
592 for (i = 0; i < 3; i++) {
593 memcpy(p, buffer + buffer_len, hlens[i]);
595 buffer_len += hlens[i];
601 static int vorbis_encode_init(AVCodecContext * avccontext)
603 venc_context_t * venc = avccontext->priv_data;
605 create_vorbis_context(venc, avccontext);
607 //if (avccontext->flags & CODEC_FLAG_QSCALE) avccontext->global_quality / (float)FF_QP2LAMBDA); else avccontext->bit_rate;
608 //if(avccontext->cutoff > 0) cfreq = avccontext->cutoff / 1000.0;
610 avccontext->extradata_size = put_main_header(venc, (uint8_t**)&avccontext->extradata);
612 avccontext->frame_size = 1 << (venc->blocksize[0] - 1);
614 avccontext->coded_frame = avcodec_alloc_frame();
615 avccontext->coded_frame->key_frame = 1;
620 static int window(venc_context_t * venc, signed short * audio, int samples) {
622 const float * win = venc->win[0];
623 int window_len = 1 << (venc->blocksize[0] - 1);
624 float n = (float)(1 << venc->blocksize[0]) / 4.;
627 if (!venc->have_saved && !samples) return 0;
629 if (venc->have_saved) {
630 for (channel = 0; channel < venc->channels; channel++) {
631 memcpy(venc->samples + channel*window_len*2, venc->saved + channel*window_len, sizeof(float)*window_len);
634 for (channel = 0; channel < venc->channels; channel++) {
635 memset(venc->samples + channel*window_len*2, 0, sizeof(float)*window_len);
640 for (channel = 0; channel < venc->channels; channel++) {
641 float * offset = venc->samples + channel*window_len*2 + window_len;
643 for (i = 0; i < samples; i++, j += venc->channels)
644 offset[i] = audio[j] / 32768. * win[window_len - i] / n;
647 for (channel = 0; channel < venc->channels; channel++) {
648 memset(venc->samples + channel*window_len*2 + window_len, 0, sizeof(float)*window_len);
652 for (channel = 0; channel < venc->channels; channel++) {
653 ff_mdct_calc(&venc->mdct[0], venc->coeffs + channel*window_len, venc->samples + channel*window_len*2, venc->floor/*tmp*/);
657 for (channel = 0; channel < venc->channels; channel++) {
658 float * offset = venc->saved + channel*window_len;
660 for (i = 0; i < samples; i++, j += venc->channels)
661 offset[i] = audio[j] / 32768. * win[i] / n;
663 venc->have_saved = 1;
665 venc->have_saved = 0;
670 static float put_vector(codebook_t * book, PutBitContext * pb, float num) {
674 assert(book->dimentions);
675 assert(book->ndimentions == 1);
676 for (i = 0; i < book->nentries; i++) {
677 float d = (book->dimentions[i] - num)*(book->dimentions[i] - num);
678 if (entry == -1 || distance > d) {
683 put_bits(pb, book->entries[entry].len, book->entries[entry].codeword);
684 return book->dimentions[entry];
687 static void residue_encode(venc_context_t * venc, residue_t * rc, PutBitContext * pb, float * coeffs, int samples, int channels) {
688 int pass, i, j, p, k;
689 int psize = rc->partition_size;
690 int partitions = (rc->end - rc->begin) / psize;
691 int classes[channels][partitions];
692 int classwords = venc->codebooks[rc->classbook].ndimentions;
694 for (pass = 0; pass < 8; pass++) {
696 while (p < partitions) {
697 if (pass == 0) for (j = 0; j < channels; j++) {
698 codebook_t * book = &venc->codebooks[rc->classbook];
700 put_bits(pb, book->entries[entry].len, book->entries[entry].codeword);
701 for (i = classwords; i--; ) {
702 classes[j][p + i] = entry % rc->classifications;
703 entry /= rc->classifications;
706 for (i = 0; i < classwords && p < partitions; i++, p++) {
707 for (j = 0; j < channels; j++) {
708 int nbook = rc->books[classes[j][p]][pass];
709 codebook_t * book = &venc->codebooks[nbook];
710 float * buf = coeffs + samples*j + rc->begin + p*psize;
711 if (nbook == -1) continue;
713 assert(rc->type == 0);
714 assert(book->ndimentions == 1);
716 for (k = 0; k < psize; k++) {
717 buf[k] -= put_vector(book, pb, buf[k]);
725 static int vorbis_encode_frame(AVCodecContext * avccontext, unsigned char * packets, int buf_size, void *data)
727 venc_context_t * venc = avccontext->priv_data;
728 signed short * audio = data;
729 int samples = data ? avccontext->frame_size : 0;
730 vorbis_mode_t * mode;
735 if (!window(venc, audio, samples)) return 0;
737 init_put_bits(&pb, packets, buf_size);
739 put_bits(&pb, 1, 0); // magic bit
741 put_bits(&pb, ilog(venc->nmodes - 1), 0); // 0 bits, the mode
743 mode = &venc->modes[0];
744 mapping = &venc->mappings[mode->mapping];
745 if (mode->blockflag) {
750 for (i = 0; i < venc->channels; i++) {
751 floor_t * fc = &venc->floors[mapping->floor[mapping->mux[i]]];
752 int range = 255 / fc->multiplier + 1;
754 put_bits(&pb, 1, 1); // non zero
755 put_bits(&pb, ilog(range - 1), 180); // magic value - 3.7180282E-05
756 put_bits(&pb, ilog(range - 1), 180); // both sides of X
757 for (j = 0; j < fc->partitions; j++) {
758 floor_class_t * c = &fc->classes[fc->partition_to_class[j]];
759 codebook_t * book = &venc->codebooks[c->books[0]];
762 for (k = 0; k < c->dim; k++) {
763 put_bits(&pb, book->entries[entry].len, book->entries[entry].codeword);
767 for (j = 0; j < samples; j++) {
768 venc->floor[i * samples + j] = floor1_inverse_db_table[180];
772 for (i = 0; i < venc->channels; i++) {
774 for (j = 0; j < samples; j++) {
775 venc->coeffs[i * samples + j] /= venc->floor[i * samples + j];
779 residue_encode(venc, &venc->residues[mapping->residue[mapping->mux[0]]], &pb, venc->coeffs, samples, venc->channels);
781 return (put_bits_count(&pb) + 7) / 8;
785 static int vorbis_encode_close(AVCodecContext * avccontext)
787 venc_context_t * venc = avccontext->priv_data;
790 if (venc->codebooks) for (i = 0; i < venc->ncodebooks; i++) {
791 av_freep(&venc->codebooks[i].entries);
792 av_freep(&venc->codebooks[i].quantlist);
793 av_freep(&venc->codebooks[i].dimentions);
795 av_freep(&venc->codebooks);
797 if (venc->floors) for (i = 0; i < venc->nfloors; i++) {
799 av_freep(&venc->floors[i].classes);
800 if (venc->floors[i].classes)
801 for (j = 0; j < venc->floors[i].nclasses; j++)
802 av_freep(&venc->floors[i].classes[j].books);
803 av_freep(&venc->floors[i].partition_to_class);
804 av_freep(&venc->floors[i].list);
806 av_freep(&venc->floors);
808 if (venc->residues) for (i = 0; i < venc->nresidues; i++) {
809 av_freep(&venc->residues[i].books);
811 av_freep(&venc->residues);
813 if (venc->mappings) for (i = 0; i < venc->nmappings; i++) {
814 av_freep(&venc->mappings[i].mux);
815 av_freep(&venc->mappings[i].floor);
816 av_freep(&venc->mappings[i].residue);
818 av_freep(&venc->mappings);
820 av_freep(&venc->modes);
822 av_freep(&venc->saved);
823 av_freep(&venc->samples);
824 av_freep(&venc->floor);
825 av_freep(&venc->coeffs);
827 ff_mdct_end(&venc->mdct[0]);
828 ff_mdct_end(&venc->mdct[1]);
830 av_freep(&avccontext->coded_frame);
831 av_freep(&avccontext->extradata);
836 AVCodec vorbis_encoder = {
840 sizeof(venc_context_t),
844 .capabilities= CODEC_CAP_DELAY,