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"
71 int * partition_to_class;
73 floor_class_t * classes;
105 int blocksize[2]; // in (1<<n) format
107 const float * win[2];
111 float * floor; // also used for tmp values for mdct
112 float * coeffs; // also used for residue after floor
115 codebook_t * codebooks;
121 residue_t * residues;
124 mapping_t * mappings;
127 vorbis_mode_t * modes;
137 #define ilog(i) av_log2(2*(i))
139 static inline void init_put_bits(PutBitContext * pb, uint8_t * buf, int buffer_len) {
140 pb->total = buffer_len * 8;
146 static void put_bits(PutBitContext * pb, int bits, uint64_t val) {
147 if ((pb->total_pos += bits) >= pb->total) return;
150 if (pb->pos > bits) {
151 *pb->buf_ptr |= val << (8 - pb->pos);
155 *pb->buf_ptr++ |= (val << (8 - pb->pos)) & 0xFF;
161 for (; bits >= 8; bits -= 8) {
162 *pb->buf_ptr++ = val & 0xFF;
171 static inline void flush_put_bits(PutBitContext * pb) {
174 static inline int put_bits_count(PutBitContext * pb) {
175 return pb->total_pos;
178 static int cb_lookup_vals(int lookup, int dimentions, int entries) {
181 for (tmp = 0; ; tmp++) {
183 for (i = 0; i < dimentions; i++) n *= tmp;
184 if (n > entries) break;
187 } else if (lookup == 2) return dimentions * entries;
191 static void ready_codebook(codebook_t * cb) {
192 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 };
195 for (i = 0; i < cb->nentries; i++) {
196 cb_entry_t * e = &cb->entries[i];
200 for (j = e->len; j; j--)
206 for (j++; j <= e->len; j++) h[j] = e->codeword | (1 << (j - 1));
208 for (i = 0; i < 33; i++) assert(!h[i]);
210 if (!cb->lookup) cb->dimentions = NULL;
212 int vals = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries);
213 cb->dimentions = av_malloc(sizeof(float) * cb->nentries * cb->ndimentions);
214 for (i = 0; i < cb->nentries; i++) {
218 for (j = 0; j < cb->ndimentions; j++) {
220 if (cb->lookup == 1) off = (i / div) % vals; // lookup type 1
221 else off = i * cb->ndimentions + j; // lookup type 2
223 cb->dimentions[i * cb->ndimentions + j] = last + cb->min + cb->quantlist[off] * cb->delta;
224 if (cb->seq_p) last = cb->dimentions[i * cb->ndimentions + j];
232 static void ready_floor(floor_t * fc) {
234 fc->list[0].sort = 0;
235 fc->list[1].sort = 1;
236 for (i = 2; i < fc->values; i++) {
239 fc->list[i].high = 1;
240 fc->list[i].sort = i;
241 for (j = 2; j < i; j++) {
242 int tmp = fc->list[j].x;
243 if (tmp < fc->list[i].x) {
244 if (tmp > fc->list[fc->list[i].low].x) fc->list[i].low = j;
246 if (tmp < fc->list[fc->list[i].high].x) fc->list[i].high = j;
250 for (i = 0; i < fc->values - 1; i++) {
252 for (j = i + 1; j < fc->values; j++) {
253 if (fc->list[fc->list[i].sort].x > fc->list[fc->list[j].sort].x) {
254 int tmp = fc->list[i].sort;
255 fc->list[i].sort = fc->list[j].sort;
256 fc->list[j].sort = tmp;
262 static void create_vorbis_context(venc_context_t * venc, AVCodecContext * avccontext) {
269 venc->channels = avccontext->channels;
270 venc->sample_rate = avccontext->sample_rate;
271 venc->blocksize[0] = venc->blocksize[1] = 8;
273 venc->ncodebooks = 10;
274 venc->codebooks = av_malloc(sizeof(codebook_t) * venc->ncodebooks);
276 // codebook 0 - floor1 book, values 0..255
277 cb = &venc->codebooks[0];
279 cb->entries = av_malloc(sizeof(cb_entry_t) * cb->nentries);
280 for (i = 0; i < cb->nentries; i++) cb->entries[i].len = 9;
286 cb->quantlist = NULL;
289 // codebook 1 - residue classbook, values 0..1, dimentions 4
290 cb = &venc->codebooks[1];
292 cb->entries = av_malloc(sizeof(cb_entry_t) * cb->nentries);
293 for (i = 0; i < cb->nentries; i++) cb->entries[i].len = 1;
299 cb->quantlist = NULL;
302 // codebook 2..9 - vector, for the residue, values -32767..32767, dimentions 1
303 for (book = 0; book < 8; book++) {
304 cb = &venc->codebooks[2 + book];
306 cb->entries = av_malloc(sizeof(cb_entry_t) * cb->nentries);
307 for (i = 0; i < cb->nentries; i++) cb->entries[i].len = i == 2 ? 1 : 3;
309 cb->delta = 1 << ((7 - book) * 2);
310 cb->min = -cb->delta*2;
313 cb->quantlist = av_malloc(sizeof(int) * cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries));
314 for (i = 0; i < cb->nentries; i++) cb->quantlist[i] = i;
319 venc->floors = av_malloc(sizeof(floor_t) * venc->nfloors);
322 fc = &venc->floors[0];
324 fc->partition_to_class = av_malloc(sizeof(int) * fc->partitions);
325 for (i = 0; i < fc->partitions; i++) fc->partition_to_class[i] = 0;
327 fc->classes = av_malloc(sizeof(floor_class_t) * fc->nclasses);
328 for (i = 0; i < fc->nclasses; i++) {
329 floor_class_t * c = &fc->classes[i];
334 books = (1 << c->subclass);
335 c->books = av_malloc(sizeof(int) * books);
336 for (j = 0; j < books; j++) c->books[j] = 0;
339 fc->rangebits = venc->blocksize[0] - 1;
342 for (i = 0; i < fc->partitions; i++)
343 fc->values += fc->classes[fc->partition_to_class[i]].dim;
345 fc->list = av_malloc(sizeof(floor_entry_t) * fc->values);
347 fc->list[1].x = 1 << fc->rangebits;
348 for (i = 2; i < fc->values; i++) {
351 assert(g <= fc->rangebits);
353 g = 1 << (fc->rangebits - g);
354 fc->list[i].x = g + a*2*g;
359 venc->residues = av_malloc(sizeof(residue_t) * venc->nresidues);
362 rc = &venc->residues[0];
365 rc->end = 1 << (venc->blocksize[0] - 1);
366 rc->partition_size = 64;
367 rc->classifications = 2;
369 rc->books = av_malloc(sizeof(int[8]) * rc->classifications);
370 for (i = 0; i < rc->classifications; i++) {
372 for (j = 0; j < 8; j++) rc->books[i][j] = 2 + j;
373 rc->books[i][0] = rc->books[i][1] = rc->books[i][2] = rc->books[i][3] = -1;
377 venc->mappings = av_malloc(sizeof(mapping_t) * venc->nmappings);
380 mc = &venc->mappings[0];
382 mc->mux = av_malloc(sizeof(int) * venc->channels);
383 for (i = 0; i < venc->channels; i++) mc->mux[i] = 0;
384 mc->floor = av_malloc(sizeof(int) * mc->submaps);
385 mc->residue = av_malloc(sizeof(int) * mc->submaps);
386 for (i = 0; i < mc->submaps; i++) {
392 venc->modes = av_malloc(sizeof(vorbis_mode_t) * venc->nmodes);
395 venc->modes[0].blockflag = 0;
396 venc->modes[0].mapping = 0;
398 venc->have_saved = 0;
399 venc->saved = av_malloc(sizeof(float) * venc->channels * (1 << venc->blocksize[1]) / 2);
400 venc->samples = av_malloc(sizeof(float) * venc->channels * (1 << venc->blocksize[1]));
401 venc->floor = av_malloc(sizeof(float) * venc->channels * (1 << venc->blocksize[1]) / 2);
402 venc->coeffs = av_malloc(sizeof(float) * venc->channels * (1 << venc->blocksize[1]) / 2);
405 const float *vwin[8]={ vwin64, vwin128, vwin256, vwin512, vwin1024, vwin2048, vwin4096, vwin8192 };
406 venc->win[0] = vwin[venc->blocksize[0] - 6];
407 venc->win[1] = vwin[venc->blocksize[1] - 6];
410 ff_mdct_init(&venc->mdct[0], venc->blocksize[0], 0);
411 ff_mdct_init(&venc->mdct[1], venc->blocksize[1], 0);
414 static void put_float(PutBitContext * pb, float f) {
417 mant = (int)ldexp(frexp(f, &exp), 20);
419 if (mant < 0) { res |= (1 << 31); mant = -mant; }
420 res |= mant | (exp << 21);
421 put_bits(pb, 32, res);
424 static void put_codebook_header(PutBitContext * pb, codebook_t * cb) {
428 put_bits(pb, 24, 0x564342); //magic
429 put_bits(pb, 16, cb->ndimentions);
430 put_bits(pb, 24, cb->nentries);
432 for (i = 1; i < cb->nentries; i++) if (cb->entries[i].len < cb->entries[i-1].len) break;
433 if (i == cb->nentries) ordered = 1;
435 put_bits(pb, 1, ordered);
437 int len = cb->entries[0].len;
438 put_bits(pb, 5, len - 1);
440 while (i < cb->nentries) {
442 for (j = 0; j+i < cb->nentries; j++) if (cb->entries[j+i].len != len) break;
443 put_bits(pb, ilog(cb->nentries - i), j);
449 for (i = 0; i < cb->nentries; i++) if (!cb->entries[i].len) break;
450 if (i != cb->nentries) sparse = 1;
451 put_bits(pb, 1, sparse);
453 for (i = 0; i < cb->nentries; i++) {
454 if (sparse) put_bits(pb, 1, !!cb->entries[i].len);
455 if (cb->entries[i].len) put_bits(pb, 5, cb->entries[i].len - 1);
459 put_bits(pb, 4, cb->lookup);
461 int tmp = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries);
462 int bits = ilog(cb->quantlist[0]);
464 for (i = 1; i < tmp; i++) bits = FFMAX(bits, ilog(cb->quantlist[i]));
466 put_float(pb, cb->min);
467 put_float(pb, cb->delta);
469 put_bits(pb, 4, bits - 1);
470 put_bits(pb, 1, cb->seq_p);
472 for (i = 0; i < tmp; i++) put_bits(pb, bits, cb->quantlist[i]);
476 static void put_floor_header(PutBitContext * pb, floor_t * fc) {
479 put_bits(pb, 16, 1); // type, only floor1 is supported
481 put_bits(pb, 5, fc->partitions);
483 for (i = 0; i < fc->partitions; i++) put_bits(pb, 4, fc->partition_to_class[i]);
485 for (i = 0; i < fc->nclasses; i++) {
488 put_bits(pb, 3, fc->classes[i].dim - 1);
489 put_bits(pb, 2, fc->classes[i].subclass);
491 if (fc->classes[i].subclass) put_bits(pb, 8, fc->classes[i].masterbook);
493 books = (1 << fc->classes[i].subclass);
495 for (j = 0; j < books; j++) put_bits(pb, 8, fc->classes[i].books[j] + 1);
498 put_bits(pb, 2, fc->multiplier - 1);
499 put_bits(pb, 4, fc->rangebits);
501 for (i = 2; i < fc->values; i++) put_bits(pb, fc->rangebits, fc->list[i].x);
504 static void put_residue_header(PutBitContext * pb, residue_t * rc) {
507 put_bits(pb, 16, rc->type);
509 put_bits(pb, 24, rc->begin);
510 put_bits(pb, 24, rc->end);
511 put_bits(pb, 24, rc->partition_size - 1);
512 put_bits(pb, 6, rc->classifications - 1);
513 put_bits(pb, 8, rc->classbook);
515 for (i = 0; i < rc->classifications; i++) {
517 for (j = 0; j < 8; j++) tmp |= (rc->books[i][j] != -1) << j;
519 put_bits(pb, 3, tmp & 7);
520 put_bits(pb, 1, tmp > 7);
522 if (tmp > 7) put_bits(pb, 5, tmp >> 3);
525 for (i = 0; i < rc->classifications; i++) {
527 for (j = 0; j < 8; j++)
528 if (rc->books[i][j] != -1)
529 put_bits(pb, 8, rc->books[i][j]);
533 static int put_main_header(venc_context_t * venc, uint8_t ** out) {
536 uint8_t buffer[50000] = {0}, * p = buffer;
537 int buffer_len = sizeof buffer;
540 // identification header
541 init_put_bits(&pb, p, buffer_len);
542 put_bits(&pb, 8, 1); //magic
543 for (i = 0; "vorbis"[i]; i++) put_bits(&pb, 8, "vorbis"[i]);
544 put_bits(&pb, 32, 0); // version
545 put_bits(&pb, 8, venc->channels);
546 put_bits(&pb, 32, venc->sample_rate);
547 put_bits(&pb, 32, 0); // bitrate
548 put_bits(&pb, 32, 0); // bitrate
549 put_bits(&pb, 32, 0); // bitrate
550 put_bits(&pb, 4, venc->blocksize[0]);
551 put_bits(&pb, 4, venc->blocksize[1]);
552 put_bits(&pb, 1, 1); // framing
555 hlens[0] = (put_bits_count(&pb) + 7) / 8;
556 buffer_len -= hlens[0];
560 init_put_bits(&pb, p, buffer_len);
561 put_bits(&pb, 8, 3); //magic
562 for (i = 0; "vorbis"[i]; i++) put_bits(&pb, 8, "vorbis"[i]);
563 put_bits(&pb, 32, 0); // vendor length TODO
564 put_bits(&pb, 32, 0); // amount of comments
565 put_bits(&pb, 1, 1); // framing
568 hlens[1] = (put_bits_count(&pb) + 7) / 8;
569 buffer_len -= hlens[1];
573 init_put_bits(&pb, p, buffer_len);
574 put_bits(&pb, 8, 5); //magic
575 for (i = 0; "vorbis"[i]; i++) put_bits(&pb, 8, "vorbis"[i]);
578 put_bits(&pb, 8, venc->ncodebooks - 1);
579 for (i = 0; i < venc->ncodebooks; i++) put_codebook_header(&pb, &venc->codebooks[i]);
581 // time domain, reserved, zero
583 put_bits(&pb, 16, 0);
586 put_bits(&pb, 6, venc->nfloors - 1);
587 for (i = 0; i < venc->nfloors; i++) put_floor_header(&pb, &venc->floors[i]);
590 put_bits(&pb, 6, venc->nresidues - 1);
591 for (i = 0; i < venc->nresidues; i++) put_residue_header(&pb, &venc->residues[i]);
594 put_bits(&pb, 6, venc->nmappings - 1);
595 for (i = 0; i < venc->nmappings; i++) {
596 mapping_t * mc = &venc->mappings[i];
598 put_bits(&pb, 16, 0); // mapping type
600 put_bits(&pb, 1, mc->submaps > 1);
601 if (mc->submaps > 1) put_bits(&pb, 4, mc->submaps - 1);
603 put_bits(&pb, 1, 0); // channel coupling
605 put_bits(&pb, 2, 0); // reserved
607 if (mc->submaps > 1) for (j = 0; j < venc->channels; j++) put_bits(&pb, 4, mc->mux[j]);
609 for (j = 0; j < mc->submaps; j++) {
610 put_bits(&pb, 8, 0); // reserved time configuration
611 put_bits(&pb, 8, mc->floor[j]);
612 put_bits(&pb, 8, mc->residue[j]);
617 put_bits(&pb, 6, venc->nmodes - 1);
618 for (i = 0; i < venc->nmodes; i++) {
619 put_bits(&pb, 1, venc->modes[i].blockflag);
620 put_bits(&pb, 16, 0); // reserved window type
621 put_bits(&pb, 16, 0); // reserved transform type
622 put_bits(&pb, 8, venc->modes[i].mapping);
625 put_bits(&pb, 1, 1); // framing
628 hlens[2] = (put_bits_count(&pb) + 7) / 8;
630 len = hlens[0] + hlens[1] + hlens[2];
631 p = *out = av_mallocz(64 + len + len/255);
634 p += av_xiphlacing(p, hlens[0]);
635 p += av_xiphlacing(p, hlens[1]);
637 for (i = 0; i < 3; i++) {
638 memcpy(p, buffer + buffer_len, hlens[i]);
640 buffer_len += hlens[i];
646 static void floor_fit(venc_context_t * venc, floor_t * fc, float * coeffs, int * posts, int samples) {
647 int range = 255 / fc->multiplier + 1;
649 for (i = 0; i < fc->values; i++) {
650 int position = fc->list[fc->list[i].sort].x;
651 int begin = fc->list[fc->list[FFMAX(i-1, 0)].sort].x;
652 int end = fc->list[fc->list[FFMIN(i+1, fc->values - 1)].sort].x;
655 begin = (position + begin) / 2;
656 end = (position + end ) / 2;
658 assert(end <= samples);
659 for (j = begin; j < end; j++) average += fabs(coeffs[j]);
660 average /= end - begin;
661 average /= 64; // MAGIC!
662 for (j = 0; j < range; j++) if (floor1_inverse_db_table[j * fc->multiplier] > average) break;
663 posts[fc->list[i].sort] = j;
667 static int render_point(int x0, int y0, int x1, int y1, int x) {
668 return y0 + (x - x0) * (y1 - y0) / (x1 - x0);
671 static void render_line(int x0, int y0, int x1, int y1, float * buf, int n) {
674 int ady = FFMAX(dy, -dy);
680 if (dy < 0) sy = base - 1;
682 ady = ady - FFMAX(base, -base) * adx;
684 buf[x] = floor1_inverse_db_table[y];
685 for (x = x0 + 1; x < x1; x++) {
694 buf[x] = floor1_inverse_db_table[y];
698 static void floor_encode(venc_context_t * venc, floor_t * fc, PutBitContext * pb, int * posts, float * floor, int samples) {
699 int range = 255 / fc->multiplier + 1;
700 int coded[fc->values]; // first 2 values are unused
704 put_bits(pb, 1, 1); // non zero
705 put_bits(pb, ilog(range - 1), posts[0]);
706 put_bits(pb, ilog(range - 1), posts[1]);
708 for (i = 2; i < fc->values; i++) {
709 int predicted = render_point(fc->list[fc->list[i].low].x,
710 posts[fc->list[i].low],
711 fc->list[fc->list[i].high].x,
712 posts[fc->list[i].high],
714 int highroom = range - predicted;
715 int lowroom = predicted;
716 int room = FFMIN(highroom, lowroom);
717 if (predicted == posts[i]) {
718 coded[i] = 0; // must be used later as flag!
721 if (!coded[fc->list[i].low]) coded[fc->list[i].low] = -1;
722 if (!coded[fc->list[i].high]) coded[fc->list[i].high] = -1;
724 if (posts[i] > predicted) {
725 if (posts[i] - predicted > room) coded[i] = posts[i] - predicted + lowroom;
726 else coded[i] = (posts[i] - predicted) << 1;
728 if (predicted - posts[i] > room) coded[i] = predicted - posts[i] + highroom - 1;
729 else coded[i] = ((predicted - posts[i]) << 1) - 1;
734 for (i = 0; i < fc->partitions; i++) {
735 floor_class_t * c = &fc->classes[fc->partition_to_class[i]];
736 codebook_t * book = &venc->codebooks[c->books[0]];
738 assert(!c->subclass);
739 for (k = 0; k < c->dim; k++) {
740 int entry = coded[counter++];
741 if (entry == -1) entry = 0;
742 assert(entry < book->nentries);
744 put_bits(pb, book->entries[entry].len, book->entries[entry].codeword);
749 ly = posts[0] * fc->multiplier; // sorted 0 is still 0
750 coded[0] = coded[1] = 1;
751 for (i = 1; i < fc->values; i++) {
752 int pos = fc->list[i].sort;
754 render_line(lx, ly, fc->list[pos].x, posts[pos] * fc->multiplier, floor, samples);
755 lx = fc->list[pos].x;
756 ly = posts[pos] * fc->multiplier;
758 if (lx >= samples) break;
760 if (lx < samples) render_line(lx, ly, samples, ly, floor, samples);
763 static float * put_vector(codebook_t * book, PutBitContext * pb, float * num) {
767 assert(book->dimentions);
768 for (i = 0; i < book->nentries; i++) {
771 for (j = 0; j < book->ndimentions; j++) {
772 float a = (book->dimentions[i * book->ndimentions + j] - num[j]);
775 if (entry == -1 || distance > d) {
780 put_bits(pb, book->entries[entry].len, book->entries[entry].codeword);
781 return &book->dimentions[entry * book->ndimentions];
784 static void residue_encode(venc_context_t * venc, residue_t * rc, PutBitContext * pb, float * coeffs, int samples, int channels) {
785 int pass, i, j, p, k;
786 int psize = rc->partition_size;
787 int partitions = (rc->end - rc->begin) / psize;
788 int classes[channels][partitions];
789 int classwords = venc->codebooks[rc->classbook].ndimentions;
791 for (pass = 0; pass < 8; pass++) {
793 while (p < partitions) {
794 if (pass == 0) for (j = 0; j < channels; j++) {
795 codebook_t * book = &venc->codebooks[rc->classbook];
797 put_bits(pb, book->entries[entry].len, book->entries[entry].codeword);
798 for (i = classwords; i--; ) {
799 classes[j][p + i] = entry % rc->classifications;
800 entry /= rc->classifications;
803 for (i = 0; i < classwords && p < partitions; i++, p++) {
804 for (j = 0; j < channels; j++) {
805 int nbook = rc->books[classes[j][p]][pass];
806 codebook_t * book = &venc->codebooks[nbook];
807 float * buf = coeffs + samples*j + rc->begin + p*psize;
808 if (nbook == -1) continue;
810 assert(rc->type == 0);
811 assert(!(psize % book->ndimentions));
813 for (k = 0; k < psize; k += book->ndimentions) {
814 float * a = put_vector(book, pb, &buf[k]);
816 for (l = 0; l < book->ndimentions; l++) buf[k + l] -= a[l];
824 static int window(venc_context_t * venc, signed short * audio, int samples) {
826 const float * win = venc->win[0];
827 int window_len = 1 << (venc->blocksize[0] - 1);
828 float n = (float)(1 << venc->blocksize[0]) / 4.;
831 if (!venc->have_saved && !samples) return 0;
833 if (venc->have_saved) {
834 for (channel = 0; channel < venc->channels; channel++) {
835 memcpy(venc->samples + channel*window_len*2, venc->saved + channel*window_len, sizeof(float)*window_len);
838 for (channel = 0; channel < venc->channels; channel++) {
839 memset(venc->samples + channel*window_len*2, 0, sizeof(float)*window_len);
844 for (channel = 0; channel < venc->channels; channel++) {
845 float * offset = venc->samples + channel*window_len*2 + window_len;
847 for (i = 0; i < samples; i++, j += venc->channels)
848 offset[i] = audio[j] / 32768. * win[window_len - i] / n;
851 for (channel = 0; channel < venc->channels; channel++) {
852 memset(venc->samples + channel*window_len*2 + window_len, 0, sizeof(float)*window_len);
856 for (channel = 0; channel < venc->channels; channel++) {
857 ff_mdct_calc(&venc->mdct[0], venc->coeffs + channel*window_len, venc->samples + channel*window_len*2, venc->floor/*tmp*/);
861 for (channel = 0; channel < venc->channels; channel++) {
862 float * offset = venc->saved + channel*window_len;
864 for (i = 0; i < samples; i++, j += venc->channels)
865 offset[i] = audio[j] / 32768. * win[i] / n;
867 venc->have_saved = 1;
869 venc->have_saved = 0;
874 static int vorbis_encode_init(AVCodecContext * avccontext)
876 venc_context_t * venc = avccontext->priv_data;
878 create_vorbis_context(venc, avccontext);
880 //if (avccontext->flags & CODEC_FLAG_QSCALE) avccontext->global_quality / (float)FF_QP2LAMBDA); else avccontext->bit_rate;
881 //if(avccontext->cutoff > 0) cfreq = avccontext->cutoff / 1000.0;
883 avccontext->extradata_size = put_main_header(venc, (uint8_t**)&avccontext->extradata);
885 avccontext->frame_size = 1 << (venc->blocksize[0] - 1);
887 avccontext->coded_frame = avcodec_alloc_frame();
888 avccontext->coded_frame->key_frame = 1;
893 static int vorbis_encode_frame(AVCodecContext * avccontext, unsigned char * packets, int buf_size, void *data)
895 venc_context_t * venc = avccontext->priv_data;
896 signed short * audio = data;
897 int samples = data ? avccontext->frame_size : 0;
898 vorbis_mode_t * mode;
903 if (!window(venc, audio, samples)) return 0;
904 samples = 1 << (venc->blocksize[0] - 1);
906 init_put_bits(&pb, packets, buf_size);
908 put_bits(&pb, 1, 0); // magic bit
910 put_bits(&pb, ilog(venc->nmodes - 1), 0); // 0 bits, the mode
912 mode = &venc->modes[0];
913 mapping = &venc->mappings[mode->mapping];
914 if (mode->blockflag) {
919 for (i = 0; i < venc->channels; i++) {
920 floor_t * fc = &venc->floors[mapping->floor[mapping->mux[i]]];
921 int posts[fc->values];
922 floor_fit(venc, fc, &venc->coeffs[i * samples], posts, samples);
923 floor_encode(venc, fc, &pb, posts, &venc->floor[i * samples], samples);
926 for (i = 0; i < venc->channels; i++) {
928 for (j = 0; j < samples; j++) {
929 venc->coeffs[i * samples + j] /= venc->floor[i * samples + j];
933 residue_encode(venc, &venc->residues[mapping->residue[mapping->mux[0]]], &pb, venc->coeffs, samples, venc->channels);
935 return (put_bits_count(&pb) + 7) / 8;
939 static int vorbis_encode_close(AVCodecContext * avccontext)
941 venc_context_t * venc = avccontext->priv_data;
944 if (venc->codebooks) for (i = 0; i < venc->ncodebooks; i++) {
945 av_freep(&venc->codebooks[i].entries);
946 av_freep(&venc->codebooks[i].quantlist);
947 av_freep(&venc->codebooks[i].dimentions);
949 av_freep(&venc->codebooks);
951 if (venc->floors) for (i = 0; i < venc->nfloors; i++) {
953 av_freep(&venc->floors[i].classes);
954 if (venc->floors[i].classes)
955 for (j = 0; j < venc->floors[i].nclasses; j++)
956 av_freep(&venc->floors[i].classes[j].books);
957 av_freep(&venc->floors[i].partition_to_class);
958 av_freep(&venc->floors[i].list);
960 av_freep(&venc->floors);
962 if (venc->residues) for (i = 0; i < venc->nresidues; i++) {
963 av_freep(&venc->residues[i].books);
965 av_freep(&venc->residues);
967 if (venc->mappings) for (i = 0; i < venc->nmappings; i++) {
968 av_freep(&venc->mappings[i].mux);
969 av_freep(&venc->mappings[i].floor);
970 av_freep(&venc->mappings[i].residue);
972 av_freep(&venc->mappings);
974 av_freep(&venc->modes);
976 av_freep(&venc->saved);
977 av_freep(&venc->samples);
978 av_freep(&venc->floor);
979 av_freep(&venc->coeffs);
981 ff_mdct_end(&venc->mdct[0]);
982 ff_mdct_end(&venc->mdct[1]);
984 av_freep(&avccontext->coded_frame);
985 av_freep(&avccontext->extradata);
990 AVCodec vorbis_encoder = {
994 sizeof(venc_context_t),
998 .capabilities= CODEC_CAP_DELAY,