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;
108 int blocksize[2]; // in (1<<n) format
110 const float * win[2];
114 float * floor; // also used for tmp values for mdct
115 float * coeffs; // also used for residue after floor
118 codebook_t * codebooks;
124 residue_t * residues;
127 mapping_t * mappings;
130 vorbis_mode_t * modes;
140 #define ilog(i) av_log2(2*(i))
142 static inline void init_put_bits(PutBitContext * pb, uint8_t * buf, int buffer_len) {
143 pb->total = buffer_len * 8;
149 static void put_bits(PutBitContext * pb, int bits, uint64_t val) {
150 if ((pb->total_pos += bits) >= pb->total) return;
153 if (pb->pos > bits) {
154 *pb->buf_ptr |= val << (8 - pb->pos);
158 *pb->buf_ptr++ |= (val << (8 - pb->pos)) & 0xFF;
164 for (; bits >= 8; bits -= 8) {
165 *pb->buf_ptr++ = val & 0xFF;
174 static inline void flush_put_bits(PutBitContext * pb) {
177 static inline int put_bits_count(PutBitContext * pb) {
178 return pb->total_pos;
181 static int cb_lookup_vals(int lookup, int dimentions, int entries) {
184 for (tmp = 0; ; tmp++) {
186 for (i = 0; i < dimentions; i++) n *= tmp;
187 if (n > entries) break;
190 } else if (lookup == 2) return dimentions * entries;
194 static void ready_codebook(codebook_t * cb) {
195 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 };
198 for (i = 0; i < cb->nentries; i++) {
199 cb_entry_t * e = &cb->entries[i];
203 for (j = e->len; j; j--)
209 for (j++; j <= e->len; j++) h[j] = e->codeword | (1 << (j - 1));
211 for (i = 0; i < 33; i++) assert(!h[i]);
213 if (!cb->lookup) cb->dimentions = NULL;
215 int vals = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries);
216 cb->dimentions = av_malloc(sizeof(float) * cb->nentries * cb->ndimentions);
217 for (i = 0; i < cb->nentries; i++) {
221 for (j = 0; j < cb->ndimentions; j++) {
223 if (cb->lookup == 1) off = (i / div) % vals; // lookup type 1
224 else off = i * cb->ndimentions + j; // lookup type 2
226 cb->dimentions[i * cb->ndimentions + j] = last + cb->min + cb->quantlist[off] * cb->delta;
227 if (cb->seq_p) last = cb->dimentions[i * cb->ndimentions + j];
235 static void ready_floor(floor_t * fc) {
237 fc->list[0].sort = 0;
238 fc->list[1].sort = 1;
239 for (i = 2; i < fc->values; i++) {
242 fc->list[i].high = 1;
243 fc->list[i].sort = i;
244 for (j = 2; j < i; j++) {
245 int tmp = fc->list[j].x;
246 if (tmp < fc->list[i].x) {
247 if (tmp > fc->list[fc->list[i].low].x) fc->list[i].low = j;
249 if (tmp < fc->list[fc->list[i].high].x) fc->list[i].high = j;
253 for (i = 0; i < fc->values - 1; i++) {
255 for (j = i + 1; j < fc->values; j++) {
256 if (fc->list[fc->list[i].sort].x > fc->list[fc->list[j].sort].x) {
257 int tmp = fc->list[i].sort;
258 fc->list[i].sort = fc->list[j].sort;
259 fc->list[j].sort = tmp;
265 static void create_vorbis_context(venc_context_t * venc, AVCodecContext * avccontext) {
272 venc->channels = avccontext->channels;
273 venc->sample_rate = avccontext->sample_rate;
274 venc->blocksize[0] = venc->blocksize[1] = 11;
276 venc->ncodebooks = 10;
277 venc->codebooks = av_malloc(sizeof(codebook_t) * venc->ncodebooks);
279 // codebook 0 - floor1 book, values 0..255
280 cb = &venc->codebooks[0];
282 cb->entries = av_malloc(sizeof(cb_entry_t) * cb->nentries);
283 for (i = 0; i < cb->nentries; i++) cb->entries[i].len = 9;
289 cb->quantlist = NULL;
292 // codebook 1 - residue classbook, values 0..1, dimentions 4
293 cb = &venc->codebooks[1];
295 cb->entries = av_malloc(sizeof(cb_entry_t) * cb->nentries);
296 for (i = 0; i < cb->nentries; i++) cb->entries[i].len = 1;
302 cb->quantlist = NULL;
305 // codebook 2..9 - vector, for the residue, values -32767..32767, dimentions 1
306 for (book = 0; book < 8; book++) {
307 cb = &venc->codebooks[2 + book];
309 cb->entries = av_malloc(sizeof(cb_entry_t) * cb->nentries);
310 for (i = 0; i < cb->nentries; i++) cb->entries[i].len = i == 2 ? 1 : 3;
312 cb->delta = 1 << ((7 - book) * 2);
313 cb->min = -cb->delta*2;
316 cb->quantlist = av_malloc(sizeof(int) * cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries));
317 for (i = 0; i < cb->nentries; i++) cb->quantlist[i] = i;
322 venc->floors = av_malloc(sizeof(floor_t) * venc->nfloors);
325 fc = &venc->floors[0];
327 fc->partition_to_class = av_malloc(sizeof(int) * fc->partitions);
328 for (i = 0; i < fc->partitions; i++) fc->partition_to_class[i] = 0;
330 fc->classes = av_malloc(sizeof(floor_class_t) * fc->nclasses);
331 for (i = 0; i < fc->nclasses; i++) {
332 floor_class_t * c = &fc->classes[i];
337 books = (1 << c->subclass);
338 c->books = av_malloc(sizeof(int) * books);
339 for (j = 0; j < books; j++) c->books[j] = 0;
342 fc->rangebits = venc->blocksize[0] - 1;
345 for (i = 0; i < fc->partitions; i++)
346 fc->values += fc->classes[fc->partition_to_class[i]].dim;
348 fc->list = av_malloc(sizeof(floor_entry_t) * fc->values);
350 fc->list[1].x = 1 << fc->rangebits;
351 for (i = 2; i < fc->values; i++) {
354 assert(g <= fc->rangebits);
356 g = 1 << (fc->rangebits - g);
357 fc->list[i].x = g + a*2*g;*/
358 //int a[] = {14, 4, 58, 2, 8, 28, 90};
359 int a[] = {93,23,372,6,46,186,750,14,33,65,130,260,556,3,10,18,28,39,55,79,111,158,220,312,464,650,850};
360 fc->list[i].x = a[i - 2];
365 venc->residues = av_malloc(sizeof(residue_t) * venc->nresidues);
368 rc = &venc->residues[0];
371 rc->end = 1 << (venc->blocksize[0] - 1);
372 rc->partition_size = 64;
373 rc->classifications = 2;
375 rc->books = av_malloc(sizeof(int[8]) * rc->classifications);
376 for (i = 0; i < rc->classifications; i++) {
378 for (j = 0; j < 8; j++) rc->books[i][j] = 2 + j;
379 rc->books[i][0] = rc->books[i][1] = rc->books[i][2] = rc->books[i][3] = -1;
383 venc->mappings = av_malloc(sizeof(mapping_t) * venc->nmappings);
386 mc = &venc->mappings[0];
388 mc->mux = av_malloc(sizeof(int) * venc->channels);
389 for (i = 0; i < venc->channels; i++) mc->mux[i] = 0;
390 mc->floor = av_malloc(sizeof(int) * mc->submaps);
391 mc->residue = av_malloc(sizeof(int) * mc->submaps);
392 for (i = 0; i < mc->submaps; i++) {
396 mc->coupling_steps = venc->channels == 2 ? 1 : 0;
397 mc->magnitude = av_malloc(sizeof(int) * mc->coupling_steps);
398 mc->angle = av_malloc(sizeof(int) * mc->coupling_steps);
399 if (mc->coupling_steps) {
400 mc->magnitude[0] = 0;
405 venc->modes = av_malloc(sizeof(vorbis_mode_t) * venc->nmodes);
408 venc->modes[0].blockflag = 0;
409 venc->modes[0].mapping = 0;
411 venc->have_saved = 0;
412 venc->saved = av_malloc(sizeof(float) * venc->channels * (1 << venc->blocksize[1]) / 2);
413 venc->samples = av_malloc(sizeof(float) * venc->channels * (1 << venc->blocksize[1]));
414 venc->floor = av_malloc(sizeof(float) * venc->channels * (1 << venc->blocksize[1]) / 2);
415 venc->coeffs = av_malloc(sizeof(float) * venc->channels * (1 << venc->blocksize[1]) / 2);
418 const float *vwin[8]={ vwin64, vwin128, vwin256, vwin512, vwin1024, vwin2048, vwin4096, vwin8192 };
419 venc->win[0] = vwin[venc->blocksize[0] - 6];
420 venc->win[1] = vwin[venc->blocksize[1] - 6];
423 ff_mdct_init(&venc->mdct[0], venc->blocksize[0], 0);
424 ff_mdct_init(&venc->mdct[1], venc->blocksize[1], 0);
427 static void put_float(PutBitContext * pb, float f) {
430 mant = (int)ldexp(frexp(f, &exp), 20);
432 if (mant < 0) { res |= (1 << 31); mant = -mant; }
433 res |= mant | (exp << 21);
434 put_bits(pb, 32, res);
437 static void put_codebook_header(PutBitContext * pb, codebook_t * cb) {
441 put_bits(pb, 24, 0x564342); //magic
442 put_bits(pb, 16, cb->ndimentions);
443 put_bits(pb, 24, cb->nentries);
445 for (i = 1; i < cb->nentries; i++) if (cb->entries[i].len < cb->entries[i-1].len) break;
446 if (i == cb->nentries) ordered = 1;
448 put_bits(pb, 1, ordered);
450 int len = cb->entries[0].len;
451 put_bits(pb, 5, len - 1);
453 while (i < cb->nentries) {
455 for (j = 0; j+i < cb->nentries; j++) if (cb->entries[j+i].len != len) break;
456 put_bits(pb, ilog(cb->nentries - i), j);
462 for (i = 0; i < cb->nentries; i++) if (!cb->entries[i].len) break;
463 if (i != cb->nentries) sparse = 1;
464 put_bits(pb, 1, sparse);
466 for (i = 0; i < cb->nentries; i++) {
467 if (sparse) put_bits(pb, 1, !!cb->entries[i].len);
468 if (cb->entries[i].len) put_bits(pb, 5, cb->entries[i].len - 1);
472 put_bits(pb, 4, cb->lookup);
474 int tmp = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries);
475 int bits = ilog(cb->quantlist[0]);
477 for (i = 1; i < tmp; i++) bits = FFMAX(bits, ilog(cb->quantlist[i]));
479 put_float(pb, cb->min);
480 put_float(pb, cb->delta);
482 put_bits(pb, 4, bits - 1);
483 put_bits(pb, 1, cb->seq_p);
485 for (i = 0; i < tmp; i++) put_bits(pb, bits, cb->quantlist[i]);
489 static void put_floor_header(PutBitContext * pb, floor_t * fc) {
492 put_bits(pb, 16, 1); // type, only floor1 is supported
494 put_bits(pb, 5, fc->partitions);
496 for (i = 0; i < fc->partitions; i++) put_bits(pb, 4, fc->partition_to_class[i]);
498 for (i = 0; i < fc->nclasses; i++) {
501 put_bits(pb, 3, fc->classes[i].dim - 1);
502 put_bits(pb, 2, fc->classes[i].subclass);
504 if (fc->classes[i].subclass) put_bits(pb, 8, fc->classes[i].masterbook);
506 books = (1 << fc->classes[i].subclass);
508 for (j = 0; j < books; j++) put_bits(pb, 8, fc->classes[i].books[j] + 1);
511 put_bits(pb, 2, fc->multiplier - 1);
512 put_bits(pb, 4, fc->rangebits);
514 for (i = 2; i < fc->values; i++) put_bits(pb, fc->rangebits, fc->list[i].x);
517 static void put_residue_header(PutBitContext * pb, residue_t * rc) {
520 put_bits(pb, 16, rc->type);
522 put_bits(pb, 24, rc->begin);
523 put_bits(pb, 24, rc->end);
524 put_bits(pb, 24, rc->partition_size - 1);
525 put_bits(pb, 6, rc->classifications - 1);
526 put_bits(pb, 8, rc->classbook);
528 for (i = 0; i < rc->classifications; i++) {
530 for (j = 0; j < 8; j++) tmp |= (rc->books[i][j] != -1) << j;
532 put_bits(pb, 3, tmp & 7);
533 put_bits(pb, 1, tmp > 7);
535 if (tmp > 7) put_bits(pb, 5, tmp >> 3);
538 for (i = 0; i < rc->classifications; i++) {
540 for (j = 0; j < 8; j++)
541 if (rc->books[i][j] != -1)
542 put_bits(pb, 8, rc->books[i][j]);
546 static int put_main_header(venc_context_t * venc, uint8_t ** out) {
549 uint8_t buffer[50000] = {0}, * p = buffer;
550 int buffer_len = sizeof buffer;
553 // identification header
554 init_put_bits(&pb, p, buffer_len);
555 put_bits(&pb, 8, 1); //magic
556 for (i = 0; "vorbis"[i]; i++) put_bits(&pb, 8, "vorbis"[i]);
557 put_bits(&pb, 32, 0); // version
558 put_bits(&pb, 8, venc->channels);
559 put_bits(&pb, 32, venc->sample_rate);
560 put_bits(&pb, 32, 0); // bitrate
561 put_bits(&pb, 32, 0); // bitrate
562 put_bits(&pb, 32, 0); // bitrate
563 put_bits(&pb, 4, venc->blocksize[0]);
564 put_bits(&pb, 4, venc->blocksize[1]);
565 put_bits(&pb, 1, 1); // framing
568 hlens[0] = (put_bits_count(&pb) + 7) / 8;
569 buffer_len -= hlens[0];
573 init_put_bits(&pb, p, buffer_len);
574 put_bits(&pb, 8, 3); //magic
575 for (i = 0; "vorbis"[i]; i++) put_bits(&pb, 8, "vorbis"[i]);
576 put_bits(&pb, 32, 0); // vendor length TODO
577 put_bits(&pb, 32, 0); // amount of comments
578 put_bits(&pb, 1, 1); // framing
581 hlens[1] = (put_bits_count(&pb) + 7) / 8;
582 buffer_len -= hlens[1];
586 init_put_bits(&pb, p, buffer_len);
587 put_bits(&pb, 8, 5); //magic
588 for (i = 0; "vorbis"[i]; i++) put_bits(&pb, 8, "vorbis"[i]);
591 put_bits(&pb, 8, venc->ncodebooks - 1);
592 for (i = 0; i < venc->ncodebooks; i++) put_codebook_header(&pb, &venc->codebooks[i]);
594 // time domain, reserved, zero
596 put_bits(&pb, 16, 0);
599 put_bits(&pb, 6, venc->nfloors - 1);
600 for (i = 0; i < venc->nfloors; i++) put_floor_header(&pb, &venc->floors[i]);
603 put_bits(&pb, 6, venc->nresidues - 1);
604 for (i = 0; i < venc->nresidues; i++) put_residue_header(&pb, &venc->residues[i]);
607 put_bits(&pb, 6, venc->nmappings - 1);
608 for (i = 0; i < venc->nmappings; i++) {
609 mapping_t * mc = &venc->mappings[i];
611 put_bits(&pb, 16, 0); // mapping type
613 put_bits(&pb, 1, mc->submaps > 1);
614 if (mc->submaps > 1) put_bits(&pb, 4, mc->submaps - 1);
616 put_bits(&pb, 1, !!mc->coupling_steps);
617 if (mc->coupling_steps) {
618 put_bits(&pb, 8, mc->coupling_steps - 1);
619 for (j = 0; j < mc->coupling_steps; j++) {
620 put_bits(&pb, ilog(venc->channels - 1), mc->magnitude[j]);
621 put_bits(&pb, ilog(venc->channels - 1), mc->angle[j]);
625 put_bits(&pb, 2, 0); // reserved
627 if (mc->submaps > 1) for (j = 0; j < venc->channels; j++) put_bits(&pb, 4, mc->mux[j]);
629 for (j = 0; j < mc->submaps; j++) {
630 put_bits(&pb, 8, 0); // reserved time configuration
631 put_bits(&pb, 8, mc->floor[j]);
632 put_bits(&pb, 8, mc->residue[j]);
637 put_bits(&pb, 6, venc->nmodes - 1);
638 for (i = 0; i < venc->nmodes; i++) {
639 put_bits(&pb, 1, venc->modes[i].blockflag);
640 put_bits(&pb, 16, 0); // reserved window type
641 put_bits(&pb, 16, 0); // reserved transform type
642 put_bits(&pb, 8, venc->modes[i].mapping);
645 put_bits(&pb, 1, 1); // framing
648 hlens[2] = (put_bits_count(&pb) + 7) / 8;
650 len = hlens[0] + hlens[1] + hlens[2];
651 p = *out = av_mallocz(64 + len + len/255);
654 p += av_xiphlacing(p, hlens[0]);
655 p += av_xiphlacing(p, hlens[1]);
657 for (i = 0; i < 3; i++) {
658 memcpy(p, buffer + buffer_len, hlens[i]);
660 buffer_len += hlens[i];
666 static void floor_fit(venc_context_t * venc, floor_t * fc, float * coeffs, int * posts, int samples) {
667 int range = 255 / fc->multiplier + 1;
669 for (i = 0; i < fc->values; i++) {
670 int position = fc->list[fc->list[i].sort].x;
671 int begin = fc->list[fc->list[FFMAX(i-1, 0)].sort].x;
672 int end = fc->list[fc->list[FFMIN(i+1, fc->values - 1)].sort].x;
675 begin = (position + begin) / 2;
676 end = (position + end ) / 2;
678 assert(end <= samples);
679 for (j = begin; j < end; j++) average += fabs(coeffs[j]);
680 average /= end - begin;
681 average /= 32; // MAGIC!
682 for (j = 0; j < range - 1; j++) if (floor1_inverse_db_table[j * fc->multiplier] > average) break;
683 posts[fc->list[i].sort] = j;
687 static int render_point(int x0, int y0, int x1, int y1, int x) {
688 return y0 + (x - x0) * (y1 - y0) / (x1 - x0);
691 static void render_line(int x0, int y0, int x1, int y1, float * buf, int n) {
694 int ady = FFMAX(dy, -dy);
700 if (dy < 0) sy = base - 1;
702 ady = ady - FFMAX(base, -base) * adx;
704 buf[x] = floor1_inverse_db_table[y];
705 for (x = x0 + 1; x < x1; x++) {
714 buf[x] = floor1_inverse_db_table[y];
718 static void floor_encode(venc_context_t * venc, floor_t * fc, PutBitContext * pb, int * posts, float * floor, int samples) {
719 int range = 255 / fc->multiplier + 1;
720 int coded[fc->values]; // first 2 values are unused
724 put_bits(pb, 1, 1); // non zero
725 put_bits(pb, ilog(range - 1), posts[0]);
726 put_bits(pb, ilog(range - 1), posts[1]);
728 for (i = 2; i < fc->values; i++) {
729 int predicted = render_point(fc->list[fc->list[i].low].x,
730 posts[fc->list[i].low],
731 fc->list[fc->list[i].high].x,
732 posts[fc->list[i].high],
734 int highroom = range - predicted;
735 int lowroom = predicted;
736 int room = FFMIN(highroom, lowroom);
737 if (predicted == posts[i]) {
738 coded[i] = 0; // must be used later as flag!
741 if (!coded[fc->list[i].low]) coded[fc->list[i].low] = -1;
742 if (!coded[fc->list[i].high]) coded[fc->list[i].high] = -1;
744 if (posts[i] > predicted) {
745 if (posts[i] - predicted > room) coded[i] = posts[i] - predicted + lowroom;
746 else coded[i] = (posts[i] - predicted) << 1;
748 if (predicted - posts[i] > room) coded[i] = predicted - posts[i] + highroom - 1;
749 else coded[i] = ((predicted - posts[i]) << 1) - 1;
754 for (i = 0; i < fc->partitions; i++) {
755 floor_class_t * c = &fc->classes[fc->partition_to_class[i]];
756 int k, cval = 0, csub = 1<<c->subclass;
758 codebook_t * book = &venc->codebooks[c->masterbook];
760 for (k = 0; k < c->dim; k++) {
762 for (l = 0; l < csub; l++) {
764 if (c->books[l] != -1) maxval = venc->codebooks[c->books[l]].nentries;
765 // coded could be -1, but this still works, cause thats 0
766 if (coded[counter + k] < maxval) break;
770 cshift += c->subclass;
772 assert(cval < book->nentries);
773 put_bits(pb, book->entries[cval].len, book->entries[cval].codeword);
775 for (k = 0; k < c->dim; k++) {
776 codebook_t * book = &venc->codebooks[c->books[cval & (csub-1)]];
777 int entry = coded[counter++];
778 cval >>= c->subclass;
779 if (entry == -1) entry = 0;
780 assert(entry < book->nentries);
782 put_bits(pb, book->entries[entry].len, book->entries[entry].codeword);
787 ly = posts[0] * fc->multiplier; // sorted 0 is still 0
788 coded[0] = coded[1] = 1;
789 for (i = 1; i < fc->values; i++) {
790 int pos = fc->list[i].sort;
792 render_line(lx, ly, fc->list[pos].x, posts[pos] * fc->multiplier, floor, samples);
793 lx = fc->list[pos].x;
794 ly = posts[pos] * fc->multiplier;
796 if (lx >= samples) break;
798 if (lx < samples) render_line(lx, ly, samples, ly, floor, samples);
801 static float * put_vector(codebook_t * book, PutBitContext * pb, float * num) {
805 assert(book->dimentions);
806 for (i = 0; i < book->nentries; i++) {
809 for (j = 0; j < book->ndimentions; j++) {
810 float a = (book->dimentions[i * book->ndimentions + j] - num[j]);
813 if (entry == -1 || distance > d) {
818 put_bits(pb, book->entries[entry].len, book->entries[entry].codeword);
819 return &book->dimentions[entry * book->ndimentions];
822 static void residue_encode(venc_context_t * venc, residue_t * rc, PutBitContext * pb, float * coeffs, int samples, int channels) {
823 int pass, i, j, p, k;
824 int psize = rc->partition_size;
825 int partitions = (rc->end - rc->begin) / psize;
826 int classes[channels][partitions];
827 int classwords = venc->codebooks[rc->classbook].ndimentions;
828 int real_ch = channels;
830 if (rc->type == 2) channels = 1;
832 for (pass = 0; pass < 8; pass++) {
834 while (p < partitions) {
835 if (pass == 0) for (j = 0; j < channels; j++) {
836 codebook_t * book = &venc->codebooks[rc->classbook];
838 put_bits(pb, book->entries[entry].len, book->entries[entry].codeword);
839 for (i = classwords; i--; ) {
840 classes[j][p + i] = entry % rc->classifications;
841 entry /= rc->classifications;
844 for (i = 0; i < classwords && p < partitions; i++, p++) {
845 for (j = 0; j < channels; j++) {
846 int nbook = rc->books[classes[j][p]][pass];
847 codebook_t * book = &venc->codebooks[nbook];
848 float * buf = coeffs + samples*j + rc->begin + p*psize;
849 if (nbook == -1) continue;
851 assert(rc->type == 0 || rc->type == 2);
852 assert(!(psize % book->ndimentions));
855 for (k = 0; k < psize; k += book->ndimentions) {
856 float * a = put_vector(book, pb, &buf[k]);
858 for (l = 0; l < book->ndimentions; l++) buf[k + l] -= a[l];
861 for (k = 0; k < psize; k += book->ndimentions) {
862 int dim = book->ndimentions, s = rc->begin + p * psize, l;
863 float vec[dim], * a = vec;
864 for (l = s + k; l < s + k + dim; l++)
865 *a++ = coeffs[(l % real_ch) * samples + l / real_ch];
866 a = put_vector(book, pb, vec);
867 for (l = s + k; l < s + k + dim; l++)
868 coeffs[(l % real_ch) * samples + l / real_ch] -= *a++;
877 static int window(venc_context_t * venc, signed short * audio, int samples) {
879 const float * win = venc->win[0];
880 int window_len = 1 << (venc->blocksize[0] - 1);
881 float n = (float)(1 << venc->blocksize[0]) / 4.;
884 if (!venc->have_saved && !samples) return 0;
886 if (venc->have_saved) {
887 for (channel = 0; channel < venc->channels; channel++) {
888 memcpy(venc->samples + channel*window_len*2, venc->saved + channel*window_len, sizeof(float)*window_len);
891 for (channel = 0; channel < venc->channels; channel++) {
892 memset(venc->samples + channel*window_len*2, 0, sizeof(float)*window_len);
897 for (channel = 0; channel < venc->channels; channel++) {
898 float * offset = venc->samples + channel*window_len*2 + window_len;
900 for (i = 0; i < samples; i++, j += venc->channels)
901 offset[i] = audio[j] / 32768. / n * win[window_len - i - 1];
904 for (channel = 0; channel < venc->channels; channel++) {
905 memset(venc->samples + channel*window_len*2 + window_len, 0, sizeof(float)*window_len);
909 for (channel = 0; channel < venc->channels; channel++) {
910 ff_mdct_calc(&venc->mdct[0], venc->coeffs + channel*window_len, venc->samples + channel*window_len*2, venc->floor/*tmp*/);
914 for (channel = 0; channel < venc->channels; channel++) {
915 float * offset = venc->saved + channel*window_len;
917 for (i = 0; i < samples; i++, j += venc->channels)
918 offset[i] = audio[j] / 32768. / n * win[i];
920 venc->have_saved = 1;
922 venc->have_saved = 0;
927 static int vorbis_encode_init(AVCodecContext * avccontext)
929 venc_context_t * venc = avccontext->priv_data;
931 create_vorbis_context(venc, avccontext);
933 //if (avccontext->flags & CODEC_FLAG_QSCALE) avccontext->global_quality / (float)FF_QP2LAMBDA); else avccontext->bit_rate;
934 //if(avccontext->cutoff > 0) cfreq = avccontext->cutoff / 1000.0;
936 avccontext->extradata_size = put_main_header(venc, (uint8_t**)&avccontext->extradata);
938 avccontext->frame_size = 1 << (venc->blocksize[0] - 1);
940 avccontext->coded_frame = avcodec_alloc_frame();
941 avccontext->coded_frame->key_frame = 1;
946 static int vorbis_encode_frame(AVCodecContext * avccontext, unsigned char * packets, int buf_size, void *data)
948 venc_context_t * venc = avccontext->priv_data;
949 signed short * audio = data;
950 int samples = data ? avccontext->frame_size : 0;
951 vorbis_mode_t * mode;
956 if (!window(venc, audio, samples)) return 0;
957 samples = 1 << (venc->blocksize[0] - 1);
959 init_put_bits(&pb, packets, buf_size);
961 put_bits(&pb, 1, 0); // magic bit
963 put_bits(&pb, ilog(venc->nmodes - 1), 0); // 0 bits, the mode
965 mode = &venc->modes[0];
966 mapping = &venc->mappings[mode->mapping];
967 if (mode->blockflag) {
972 for (i = 0; i < venc->channels; i++) {
973 floor_t * fc = &venc->floors[mapping->floor[mapping->mux[i]]];
974 int posts[fc->values];
975 floor_fit(venc, fc, &venc->coeffs[i * samples], posts, samples);
976 floor_encode(venc, fc, &pb, posts, &venc->floor[i * samples], samples);
979 for (i = 0; i < venc->channels; i++) {
981 for (j = 0; j < samples; j++) {
982 venc->coeffs[i * samples + j] /= venc->floor[i * samples + j];
986 for (i = 0; i < mapping->coupling_steps; i++) {
987 float * mag = venc->coeffs + mapping->magnitude[i] * samples;
988 float * ang = venc->coeffs + mapping->angle[i] * samples;
990 for (j = 0; j < samples; j++) {
995 if (a > m) mag[j] = a;
999 if (a > m) mag[j] = m;
1005 residue_encode(venc, &venc->residues[mapping->residue[mapping->mux[0]]], &pb, venc->coeffs, samples, venc->channels);
1007 return (put_bits_count(&pb) + 7) / 8;
1011 static int vorbis_encode_close(AVCodecContext * avccontext)
1013 venc_context_t * venc = avccontext->priv_data;
1016 if (venc->codebooks) for (i = 0; i < venc->ncodebooks; i++) {
1017 av_freep(&venc->codebooks[i].entries);
1018 av_freep(&venc->codebooks[i].quantlist);
1019 av_freep(&venc->codebooks[i].dimentions);
1021 av_freep(&venc->codebooks);
1023 if (venc->floors) for (i = 0; i < venc->nfloors; i++) {
1025 av_freep(&venc->floors[i].classes);
1026 if (venc->floors[i].classes)
1027 for (j = 0; j < venc->floors[i].nclasses; j++)
1028 av_freep(&venc->floors[i].classes[j].books);
1029 av_freep(&venc->floors[i].partition_to_class);
1030 av_freep(&venc->floors[i].list);
1032 av_freep(&venc->floors);
1034 if (venc->residues) for (i = 0; i < venc->nresidues; i++) {
1035 av_freep(&venc->residues[i].books);
1037 av_freep(&venc->residues);
1039 if (venc->mappings) for (i = 0; i < venc->nmappings; i++) {
1040 av_freep(&venc->mappings[i].mux);
1041 av_freep(&venc->mappings[i].floor);
1042 av_freep(&venc->mappings[i].residue);
1044 av_freep(&venc->mappings);
1046 av_freep(&venc->modes);
1048 av_freep(&venc->saved);
1049 av_freep(&venc->samples);
1050 av_freep(&venc->floor);
1051 av_freep(&venc->coeffs);
1053 ff_mdct_end(&venc->mdct[0]);
1054 ff_mdct_end(&venc->mdct[1]);
1056 av_freep(&avccontext->coded_frame);
1057 av_freep(&avccontext->extradata);
1062 AVCodec vorbis_encoder = {
1066 sizeof(venc_context_t),
1068 vorbis_encode_frame,
1069 vorbis_encode_close,
1070 .capabilities= CODEC_CAP_DELAY,