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 = 15 + 1 + 8;
277 venc->codebooks = av_malloc(sizeof(codebook_t) * venc->ncodebooks);
279 int codebook0[] = { 2, 10, 8, 14, 7, 12, 11, 14, 1, 5, 3, 7, 4, 9, 7, 13, };
280 int codebook1[] = { 1, 4, 2, 6, 3, 7, 5, 7, };
281 int codebook2[] = { 1, 5, 7, 21, 5, 8, 9, 21, 10, 9, 12, 20, 20, 16, 20, 20, 4, 8, 9, 20, 6, 8, 9, 20, 11, 11, 13, 20, 20, 15, 17, 20, 9, 11, 14, 20, 8, 10, 15, 20, 11, 13, 15, 20, 20, 20, 20, 20, 20, 20, 20, 20, 13, 20, 20, 20, 18, 18, 20, 20, 20, 20, 20, 20, 3, 6, 8, 20, 6, 7, 9, 20, 10, 9, 12, 20, 20, 20, 20, 20, 5, 7, 9, 20, 6, 6, 9, 20, 10, 9, 12, 20, 20, 20, 20, 20, 8, 10, 13, 20, 8, 9, 12, 20, 11, 10, 12, 20, 20, 20, 20, 20, 18, 20, 20, 20, 15, 17, 18, 20, 18, 17, 18, 20, 20, 20, 20, 20, 7, 10, 12, 20, 8, 9, 11, 20, 14, 13, 14, 20, 20, 20, 20, 20, 6, 9, 12, 20, 7, 8, 11, 20, 12, 11, 13, 20, 20, 20, 20, 20, 9, 11, 15, 20, 8, 10, 14, 20, 12, 11, 14, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 11, 16, 18, 20, 15, 15, 17, 20, 20, 17, 20, 20, 20, 20, 20, 20, 9, 14, 16, 20, 12, 12, 15, 20, 17, 15, 18, 20, 20, 20, 20, 20, 16, 19, 18, 20, 15, 16, 20, 20, 17, 17, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, };
282 int codebook3[] = { 2, 3, 7, 13, 4, 4, 7, 15, 8, 6, 9, 17, 21, 16, 15, 21, 2, 5, 7, 11, 5, 5, 7, 14, 9, 7, 10, 16, 17, 15, 16, 21, 4, 7, 10, 17, 7, 7, 9, 15, 11, 9, 11, 16, 21, 18, 15, 21, 18, 21, 21, 21, 15, 17, 17, 19, 21, 19, 18, 20, 21, 21, 21, 20, };
283 int codebook4[] = { 5, 5, 5, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 5, 7, 5, 7, 5, 7, 5, 7, 5, 8, 6, 8, 6, 8, 6, 9, 6, 9, 6, 10, 6, 10, 6, 11, 6, 11, 7, 11, 7, 12, 7, 12, 7, 12, 7, 12, 7, 12, 7, 12, 7, 12, 7, 12, 8, 13, 8, 12, 8, 12, 8, 13, 8, 13, 9, 13, 9, 13, 9, 13, 9, 12, 10, 12, 10, 13, 10, 14, 11, 14, 12, 14, 13, 14, 13, 14, 14, 15, 16, 15, 15, 15, 14, 15, 17, 21, 22, 22, 21, 22, 22, 22, 22, 22, 22, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, };
284 int codebook5[] = { 2, 5, 5, 4, 5, 4, 5, 4, 5, 4, 6, 5, 6, 5, 6, 5, 6, 5, 7, 5, 7, 6, 8, 6, 8, 6, 8, 6, 9, 6, 9, 6, };
285 int codebook6[] = { 8, 5, 8, 4, 9, 4, 9, 4, 9, 4, 9, 4, 9, 4, 9, 4, 9, 4, 9, 4, 9, 4, 8, 4, 8, 4, 9, 5, 9, 5, 9, 5, 9, 5, 9, 6, 10, 6, 10, 7, 10, 8, 11, 9, 11, 11, 12, 13, 12, 14, 13, 15, 13, 15, 14, 16, 14, 17, 15, 17, 15, 15, 16, 16, 15, 16, 16, 16, 15, 18, 16, 15, 17, 17, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, };
286 int codebook7[] = { 1, 5, 5, 5, 5, 5, 5, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 6, 7, 7, 7, 7, 8, 7, 8, 8, 9, 8, 10, 9, 10, 9, };
287 int codebook8[] = { 4, 3, 4, 3, 4, 4, 5, 4, 5, 4, 5, 5, 6, 5, 6, 5, 7, 5, 7, 6, 7, 6, 8, 7, 8, 7, 8, 7, 9, 8, 9, 9, 9, 9, 10, 10, 10, 11, 9, 12, 9, 12, 9, 15, 10, 14, 9, 13, 10, 13, 10, 12, 10, 12, 10, 13, 10, 12, 11, 13, 11, 14, 12, 13, 13, 14, 14, 13, 14, 15, 14, 16, 13, 13, 14, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 15, 15, };
288 int codebook9[] = { 4, 5, 4, 5, 3, 5, 3, 5, 3, 5, 4, 4, 4, 4, 5, 5, 5, };
289 int codebook10[] = { 3, 3, 4, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 5, 7, 5, 8, 6, 8, 6, 9, 7, 10, 7, 10, 8, 10, 8, 11, 9, 11, };
290 int codebook11[] = { 3, 7, 3, 8, 3, 10, 3, 8, 3, 9, 3, 8, 4, 9, 4, 9, 5, 9, 6, 10, 6, 9, 7, 11, 7, 12, 9, 13, 10, 13, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, };
291 int codebook12[] = { 4, 5, 4, 5, 4, 5, 4, 5, 3, 5, 3, 5, 3, 5, 4, 5, 4, };
292 int codebook13[] = { 4, 2, 4, 2, 5, 3, 5, 4, 6, 6, 6, 7, 7, 8, 7, 8, 7, 8, 7, 9, 8, 9, 8, 9, 8, 10, 8, 11, 9, 12, 9, 12, };
293 int codebook14[] = { 2, 5, 2, 6, 3, 6, 4, 7, 4, 7, 5, 9, 5, 11, 6, 11, 6, 11, 7, 11, 6, 11, 6, 11, 9, 11, 8, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 10, 10, 10, 10, 10, 10, };
295 int codebook_sizes[] = { 16, 8, 256, 64, 128, 32, 96, 32, 96, 17, 32, 78, 17, 32, 78 };
296 int * codebook_lens[] = { codebook0, codebook1, codebook2, codebook3, codebook4, codebook5, codebook6, codebook7,
297 codebook8, codebook9, codebook10, codebook11, codebook12, codebook13, codebook14 };
299 // codebook 0..14 - floor1 book, values 0..255
300 for (book = 0; book < 15; book++) {
301 cb = &venc->codebooks[book];
302 cb->nentries = codebook_sizes[book];
303 cb->entries = av_malloc(sizeof(cb_entry_t) * cb->nentries);
304 for (i = 0; i < cb->nentries; i++) cb->entries[i].len = codebook_lens[book][i];
310 cb->quantlist = NULL;
314 // codebook 15 - residue classbook, values 0..1, dimentions 4
315 cb = &venc->codebooks[15];
317 cb->entries = av_malloc(sizeof(cb_entry_t) * cb->nentries);
318 for (i = 0; i < cb->nentries; i++) cb->entries[i].len = 1;
324 cb->quantlist = NULL;
327 // codebook 16..23 - vector, for the residue, values -32767..32767, dimentions 1
328 for (book = 0; book < 8; book++) {
329 cb = &venc->codebooks[16 + book];
331 cb->entries = av_malloc(sizeof(cb_entry_t) * cb->nentries);
332 for (i = 0; i < cb->nentries; i++) cb->entries[i].len = i == 2 ? 1 : 3;
334 cb->delta = 1 << ((7 - book) * 2);
335 cb->min = -cb->delta*2;
338 cb->quantlist = av_malloc(sizeof(int) * cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries));
339 for (i = 0; i < cb->nentries; i++) cb->quantlist[i] = i;
344 venc->floors = av_malloc(sizeof(floor_t) * venc->nfloors);
347 fc = &venc->floors[0];
349 fc->partition_to_class = av_malloc(sizeof(int) * fc->partitions);
351 for (i = 0; i < fc->partitions; i++) {
352 int a[] = {0,1,2,2,3,3,4,4};
353 fc->partition_to_class[i] = a[i];
354 fc->nclasses = FFMAX(fc->nclasses, fc->partition_to_class[i]);
357 fc->classes = av_malloc(sizeof(floor_class_t) * fc->nclasses);
358 for (i = 0; i < fc->nclasses; i++) {
359 floor_class_t * c = &fc->classes[i];
361 int dim[] = {3,4,3,4,3};
362 int subclass[] = {0,1,1,2,2};
363 int masterbook[] = {0/*none*/,0,1,2,3};
368 (int[]){ -1, 9, 10, 11 },
369 (int[]){ -1, 12, 13, 14 },
372 c->subclass = subclass[i];
373 c->masterbook = masterbook[i];
374 books = (1 << c->subclass);
375 c->books = av_malloc(sizeof(int) * books);
376 for (j = 0; j < books; j++) c->books[j] = nbooks[i][j];
379 fc->rangebits = venc->blocksize[0] - 1;
382 for (i = 0; i < fc->partitions; i++)
383 fc->values += fc->classes[fc->partition_to_class[i]].dim;
385 fc->list = av_malloc(sizeof(floor_entry_t) * fc->values);
387 fc->list[1].x = 1 << fc->rangebits;
388 for (i = 2; i < fc->values; i++) {
391 assert(g <= fc->rangebits);
393 g = 1 << (fc->rangebits - g);
394 fc->list[i].x = g + a*2*g;*/
395 //int a[] = {14, 4, 58, 2, 8, 28, 90};
396 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};
397 fc->list[i].x = a[i - 2];
402 venc->residues = av_malloc(sizeof(residue_t) * venc->nresidues);
405 rc = &venc->residues[0];
408 rc->end = 1 << (venc->blocksize[0] - 1);
409 rc->partition_size = 64;
410 rc->classifications = 2;
412 rc->books = av_malloc(sizeof(int[8]) * rc->classifications);
413 for (i = 0; i < rc->classifications; i++) {
415 for (j = 0; j < 8; j++) rc->books[i][j] = 16 + j;
416 rc->books[i][0] = rc->books[i][1] = rc->books[i][2] = rc->books[i][3] = -1;
420 venc->mappings = av_malloc(sizeof(mapping_t) * venc->nmappings);
423 mc = &venc->mappings[0];
425 mc->mux = av_malloc(sizeof(int) * venc->channels);
426 for (i = 0; i < venc->channels; i++) mc->mux[i] = 0;
427 mc->floor = av_malloc(sizeof(int) * mc->submaps);
428 mc->residue = av_malloc(sizeof(int) * mc->submaps);
429 for (i = 0; i < mc->submaps; i++) {
433 mc->coupling_steps = venc->channels == 2 ? 1 : 0;
434 mc->magnitude = av_malloc(sizeof(int) * mc->coupling_steps);
435 mc->angle = av_malloc(sizeof(int) * mc->coupling_steps);
436 if (mc->coupling_steps) {
437 mc->magnitude[0] = 0;
442 venc->modes = av_malloc(sizeof(vorbis_mode_t) * venc->nmodes);
445 venc->modes[0].blockflag = 0;
446 venc->modes[0].mapping = 0;
448 venc->have_saved = 0;
449 venc->saved = av_malloc(sizeof(float) * venc->channels * (1 << venc->blocksize[1]) / 2);
450 venc->samples = av_malloc(sizeof(float) * venc->channels * (1 << venc->blocksize[1]));
451 venc->floor = av_malloc(sizeof(float) * venc->channels * (1 << venc->blocksize[1]) / 2);
452 venc->coeffs = av_malloc(sizeof(float) * venc->channels * (1 << venc->blocksize[1]) / 2);
455 const float *vwin[8]={ vwin64, vwin128, vwin256, vwin512, vwin1024, vwin2048, vwin4096, vwin8192 };
456 venc->win[0] = vwin[venc->blocksize[0] - 6];
457 venc->win[1] = vwin[venc->blocksize[1] - 6];
460 ff_mdct_init(&venc->mdct[0], venc->blocksize[0], 0);
461 ff_mdct_init(&venc->mdct[1], venc->blocksize[1], 0);
464 static void put_float(PutBitContext * pb, float f) {
467 mant = (int)ldexp(frexp(f, &exp), 20);
469 if (mant < 0) { res |= (1 << 31); mant = -mant; }
470 res |= mant | (exp << 21);
471 put_bits(pb, 32, res);
474 static void put_codebook_header(PutBitContext * pb, codebook_t * cb) {
478 put_bits(pb, 24, 0x564342); //magic
479 put_bits(pb, 16, cb->ndimentions);
480 put_bits(pb, 24, cb->nentries);
482 for (i = 1; i < cb->nentries; i++) if (cb->entries[i].len < cb->entries[i-1].len) break;
483 if (i == cb->nentries) ordered = 1;
485 put_bits(pb, 1, ordered);
487 int len = cb->entries[0].len;
488 put_bits(pb, 5, len - 1);
490 while (i < cb->nentries) {
492 for (j = 0; j+i < cb->nentries; j++) if (cb->entries[j+i].len != len) break;
493 put_bits(pb, ilog(cb->nentries - i), j);
499 for (i = 0; i < cb->nentries; i++) if (!cb->entries[i].len) break;
500 if (i != cb->nentries) sparse = 1;
501 put_bits(pb, 1, sparse);
503 for (i = 0; i < cb->nentries; i++) {
504 if (sparse) put_bits(pb, 1, !!cb->entries[i].len);
505 if (cb->entries[i].len) put_bits(pb, 5, cb->entries[i].len - 1);
509 put_bits(pb, 4, cb->lookup);
511 int tmp = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries);
512 int bits = ilog(cb->quantlist[0]);
514 for (i = 1; i < tmp; i++) bits = FFMAX(bits, ilog(cb->quantlist[i]));
516 put_float(pb, cb->min);
517 put_float(pb, cb->delta);
519 put_bits(pb, 4, bits - 1);
520 put_bits(pb, 1, cb->seq_p);
522 for (i = 0; i < tmp; i++) put_bits(pb, bits, cb->quantlist[i]);
526 static void put_floor_header(PutBitContext * pb, floor_t * fc) {
529 put_bits(pb, 16, 1); // type, only floor1 is supported
531 put_bits(pb, 5, fc->partitions);
533 for (i = 0; i < fc->partitions; i++) put_bits(pb, 4, fc->partition_to_class[i]);
535 for (i = 0; i < fc->nclasses; i++) {
538 put_bits(pb, 3, fc->classes[i].dim - 1);
539 put_bits(pb, 2, fc->classes[i].subclass);
541 if (fc->classes[i].subclass) put_bits(pb, 8, fc->classes[i].masterbook);
543 books = (1 << fc->classes[i].subclass);
545 for (j = 0; j < books; j++) put_bits(pb, 8, fc->classes[i].books[j] + 1);
548 put_bits(pb, 2, fc->multiplier - 1);
549 put_bits(pb, 4, fc->rangebits);
551 for (i = 2; i < fc->values; i++) put_bits(pb, fc->rangebits, fc->list[i].x);
554 static void put_residue_header(PutBitContext * pb, residue_t * rc) {
557 put_bits(pb, 16, rc->type);
559 put_bits(pb, 24, rc->begin);
560 put_bits(pb, 24, rc->end);
561 put_bits(pb, 24, rc->partition_size - 1);
562 put_bits(pb, 6, rc->classifications - 1);
563 put_bits(pb, 8, rc->classbook);
565 for (i = 0; i < rc->classifications; i++) {
567 for (j = 0; j < 8; j++) tmp |= (rc->books[i][j] != -1) << j;
569 put_bits(pb, 3, tmp & 7);
570 put_bits(pb, 1, tmp > 7);
572 if (tmp > 7) put_bits(pb, 5, tmp >> 3);
575 for (i = 0; i < rc->classifications; i++) {
577 for (j = 0; j < 8; j++)
578 if (rc->books[i][j] != -1)
579 put_bits(pb, 8, rc->books[i][j]);
583 static int put_main_header(venc_context_t * venc, uint8_t ** out) {
586 uint8_t buffer[50000] = {0}, * p = buffer;
587 int buffer_len = sizeof buffer;
590 // identification header
591 init_put_bits(&pb, p, buffer_len);
592 put_bits(&pb, 8, 1); //magic
593 for (i = 0; "vorbis"[i]; i++) put_bits(&pb, 8, "vorbis"[i]);
594 put_bits(&pb, 32, 0); // version
595 put_bits(&pb, 8, venc->channels);
596 put_bits(&pb, 32, venc->sample_rate);
597 put_bits(&pb, 32, 0); // bitrate
598 put_bits(&pb, 32, 0); // bitrate
599 put_bits(&pb, 32, 0); // bitrate
600 put_bits(&pb, 4, venc->blocksize[0]);
601 put_bits(&pb, 4, venc->blocksize[1]);
602 put_bits(&pb, 1, 1); // framing
605 hlens[0] = (put_bits_count(&pb) + 7) / 8;
606 buffer_len -= hlens[0];
610 init_put_bits(&pb, p, buffer_len);
611 put_bits(&pb, 8, 3); //magic
612 for (i = 0; "vorbis"[i]; i++) put_bits(&pb, 8, "vorbis"[i]);
613 put_bits(&pb, 32, 0); // vendor length TODO
614 put_bits(&pb, 32, 0); // amount of comments
615 put_bits(&pb, 1, 1); // framing
618 hlens[1] = (put_bits_count(&pb) + 7) / 8;
619 buffer_len -= hlens[1];
623 init_put_bits(&pb, p, buffer_len);
624 put_bits(&pb, 8, 5); //magic
625 for (i = 0; "vorbis"[i]; i++) put_bits(&pb, 8, "vorbis"[i]);
628 put_bits(&pb, 8, venc->ncodebooks - 1);
629 for (i = 0; i < venc->ncodebooks; i++) put_codebook_header(&pb, &venc->codebooks[i]);
631 // time domain, reserved, zero
633 put_bits(&pb, 16, 0);
636 put_bits(&pb, 6, venc->nfloors - 1);
637 for (i = 0; i < venc->nfloors; i++) put_floor_header(&pb, &venc->floors[i]);
640 put_bits(&pb, 6, venc->nresidues - 1);
641 for (i = 0; i < venc->nresidues; i++) put_residue_header(&pb, &venc->residues[i]);
644 put_bits(&pb, 6, venc->nmappings - 1);
645 for (i = 0; i < venc->nmappings; i++) {
646 mapping_t * mc = &venc->mappings[i];
648 put_bits(&pb, 16, 0); // mapping type
650 put_bits(&pb, 1, mc->submaps > 1);
651 if (mc->submaps > 1) put_bits(&pb, 4, mc->submaps - 1);
653 put_bits(&pb, 1, !!mc->coupling_steps);
654 if (mc->coupling_steps) {
655 put_bits(&pb, 8, mc->coupling_steps - 1);
656 for (j = 0; j < mc->coupling_steps; j++) {
657 put_bits(&pb, ilog(venc->channels - 1), mc->magnitude[j]);
658 put_bits(&pb, ilog(venc->channels - 1), mc->angle[j]);
662 put_bits(&pb, 2, 0); // reserved
664 if (mc->submaps > 1) for (j = 0; j < venc->channels; j++) put_bits(&pb, 4, mc->mux[j]);
666 for (j = 0; j < mc->submaps; j++) {
667 put_bits(&pb, 8, 0); // reserved time configuration
668 put_bits(&pb, 8, mc->floor[j]);
669 put_bits(&pb, 8, mc->residue[j]);
674 put_bits(&pb, 6, venc->nmodes - 1);
675 for (i = 0; i < venc->nmodes; i++) {
676 put_bits(&pb, 1, venc->modes[i].blockflag);
677 put_bits(&pb, 16, 0); // reserved window type
678 put_bits(&pb, 16, 0); // reserved transform type
679 put_bits(&pb, 8, venc->modes[i].mapping);
682 put_bits(&pb, 1, 1); // framing
685 hlens[2] = (put_bits_count(&pb) + 7) / 8;
687 len = hlens[0] + hlens[1] + hlens[2];
688 p = *out = av_mallocz(64 + len + len/255);
691 p += av_xiphlacing(p, hlens[0]);
692 p += av_xiphlacing(p, hlens[1]);
694 for (i = 0; i < 3; i++) {
695 memcpy(p, buffer + buffer_len, hlens[i]);
697 buffer_len += hlens[i];
703 static void floor_fit(venc_context_t * venc, floor_t * fc, float * coeffs, int * posts, int samples) {
704 int range = 255 / fc->multiplier + 1;
706 for (i = 0; i < fc->values; i++) {
707 int position = fc->list[fc->list[i].sort].x;
708 int begin = fc->list[fc->list[FFMAX(i-1, 0)].sort].x;
709 int end = fc->list[fc->list[FFMIN(i+1, fc->values - 1)].sort].x;
712 begin = (position + begin) / 2;
713 end = (position + end ) / 2;
715 assert(end <= samples);
716 for (j = begin; j < end; j++) average += fabs(coeffs[j]);
717 average /= end - begin;
718 average /= 32; // MAGIC!
719 for (j = 0; j < range - 1; j++) if (floor1_inverse_db_table[j * fc->multiplier] > average) break;
720 posts[fc->list[i].sort] = j;
724 static int render_point(int x0, int y0, int x1, int y1, int x) {
725 return y0 + (x - x0) * (y1 - y0) / (x1 - x0);
728 static void render_line(int x0, int y0, int x1, int y1, float * buf, int n) {
731 int ady = FFMAX(dy, -dy);
737 if (dy < 0) sy = base - 1;
739 ady = ady - FFMAX(base, -base) * adx;
741 buf[x] = floor1_inverse_db_table[y];
742 for (x = x0 + 1; x < x1; x++) {
751 buf[x] = floor1_inverse_db_table[y];
755 static void floor_encode(venc_context_t * venc, floor_t * fc, PutBitContext * pb, int * posts, float * floor, int samples) {
756 int range = 255 / fc->multiplier + 1;
757 int coded[fc->values]; // first 2 values are unused
761 put_bits(pb, 1, 1); // non zero
762 put_bits(pb, ilog(range - 1), posts[0]);
763 put_bits(pb, ilog(range - 1), posts[1]);
765 for (i = 2; i < fc->values; i++) {
766 int predicted = render_point(fc->list[fc->list[i].low].x,
767 posts[fc->list[i].low],
768 fc->list[fc->list[i].high].x,
769 posts[fc->list[i].high],
771 int highroom = range - predicted;
772 int lowroom = predicted;
773 int room = FFMIN(highroom, lowroom);
774 if (predicted == posts[i]) {
775 coded[i] = 0; // must be used later as flag!
778 if (!coded[fc->list[i].low]) coded[fc->list[i].low] = -1;
779 if (!coded[fc->list[i].high]) coded[fc->list[i].high] = -1;
781 if (posts[i] > predicted) {
782 if (posts[i] - predicted > room) coded[i] = posts[i] - predicted + lowroom;
783 else coded[i] = (posts[i] - predicted) << 1;
785 if (predicted - posts[i] > room) coded[i] = predicted - posts[i] + highroom - 1;
786 else coded[i] = ((predicted - posts[i]) << 1) - 1;
791 for (i = 0; i < fc->partitions; i++) {
792 floor_class_t * c = &fc->classes[fc->partition_to_class[i]];
793 int k, cval = 0, csub = 1<<c->subclass;
795 codebook_t * book = &venc->codebooks[c->masterbook];
797 for (k = 0; k < c->dim; k++) {
799 for (l = 0; l < csub; l++) {
801 if (c->books[l] != -1) maxval = venc->codebooks[c->books[l]].nentries;
802 // coded could be -1, but this still works, cause thats 0
803 if (coded[counter + k] < maxval) break;
807 cshift += c->subclass;
809 assert(cval < book->nentries);
810 put_bits(pb, book->entries[cval].len, book->entries[cval].codeword);
812 for (k = 0; k < c->dim; k++) {
813 int book = c->books[cval & (csub-1)];
814 int entry = coded[counter++];
815 cval >>= c->subclass;
816 if (book == -1) continue;
817 if (entry == -1) entry = 0;
818 assert(entry < venc->codebooks[book].nentries);
820 put_bits(pb, venc->codebooks[book].entries[entry].len, venc->codebooks[book].entries[entry].codeword);
825 ly = posts[0] * fc->multiplier; // sorted 0 is still 0
826 coded[0] = coded[1] = 1;
827 for (i = 1; i < fc->values; i++) {
828 int pos = fc->list[i].sort;
830 render_line(lx, ly, fc->list[pos].x, posts[pos] * fc->multiplier, floor, samples);
831 lx = fc->list[pos].x;
832 ly = posts[pos] * fc->multiplier;
834 if (lx >= samples) break;
836 if (lx < samples) render_line(lx, ly, samples, ly, floor, samples);
839 static float * put_vector(codebook_t * book, PutBitContext * pb, float * num) {
843 assert(book->dimentions);
844 for (i = 0; i < book->nentries; i++) {
847 for (j = 0; j < book->ndimentions; j++) {
848 float a = (book->dimentions[i * book->ndimentions + j] - num[j]);
851 if (entry == -1 || distance > d) {
856 put_bits(pb, book->entries[entry].len, book->entries[entry].codeword);
857 return &book->dimentions[entry * book->ndimentions];
860 static void residue_encode(venc_context_t * venc, residue_t * rc, PutBitContext * pb, float * coeffs, int samples, int channels) {
861 int pass, i, j, p, k;
862 int psize = rc->partition_size;
863 int partitions = (rc->end - rc->begin) / psize;
864 int classes[channels][partitions];
865 int classwords = venc->codebooks[rc->classbook].ndimentions;
866 int real_ch = channels;
868 if (rc->type == 2) channels = 1;
870 for (pass = 0; pass < 8; pass++) {
872 while (p < partitions) {
873 if (pass == 0) for (j = 0; j < channels; j++) {
874 codebook_t * book = &venc->codebooks[rc->classbook];
876 put_bits(pb, book->entries[entry].len, book->entries[entry].codeword);
877 for (i = classwords; i--; ) {
878 classes[j][p + i] = entry % rc->classifications;
879 entry /= rc->classifications;
882 for (i = 0; i < classwords && p < partitions; i++, p++) {
883 for (j = 0; j < channels; j++) {
884 int nbook = rc->books[classes[j][p]][pass];
885 codebook_t * book = &venc->codebooks[nbook];
886 float * buf = coeffs + samples*j + rc->begin + p*psize;
887 if (nbook == -1) continue;
889 assert(rc->type == 0 || rc->type == 2);
890 assert(!(psize % book->ndimentions));
893 for (k = 0; k < psize; k += book->ndimentions) {
894 float * a = put_vector(book, pb, &buf[k]);
896 for (l = 0; l < book->ndimentions; l++) buf[k + l] -= a[l];
899 for (k = 0; k < psize; k += book->ndimentions) {
900 int dim = book->ndimentions, s = rc->begin + p * psize, l;
901 float vec[dim], * a = vec;
902 for (l = s + k; l < s + k + dim; l++)
903 *a++ = coeffs[(l % real_ch) * samples + l / real_ch];
904 a = put_vector(book, pb, vec);
905 for (l = s + k; l < s + k + dim; l++)
906 coeffs[(l % real_ch) * samples + l / real_ch] -= *a++;
915 static int window(venc_context_t * venc, signed short * audio, int samples) {
917 const float * win = venc->win[0];
918 int window_len = 1 << (venc->blocksize[0] - 1);
919 float n = (float)(1 << venc->blocksize[0]) / 4.;
922 if (!venc->have_saved && !samples) return 0;
924 if (venc->have_saved) {
925 for (channel = 0; channel < venc->channels; channel++) {
926 memcpy(venc->samples + channel*window_len*2, venc->saved + channel*window_len, sizeof(float)*window_len);
929 for (channel = 0; channel < venc->channels; channel++) {
930 memset(venc->samples + channel*window_len*2, 0, sizeof(float)*window_len);
935 for (channel = 0; channel < venc->channels; channel++) {
936 float * offset = venc->samples + channel*window_len*2 + window_len;
938 for (i = 0; i < samples; i++, j += venc->channels)
939 offset[i] = audio[j] / 32768. / n * win[window_len - i - 1];
942 for (channel = 0; channel < venc->channels; channel++) {
943 memset(venc->samples + channel*window_len*2 + window_len, 0, sizeof(float)*window_len);
947 for (channel = 0; channel < venc->channels; channel++) {
948 ff_mdct_calc(&venc->mdct[0], venc->coeffs + channel*window_len, venc->samples + channel*window_len*2, venc->floor/*tmp*/);
952 for (channel = 0; channel < venc->channels; channel++) {
953 float * offset = venc->saved + channel*window_len;
955 for (i = 0; i < samples; i++, j += venc->channels)
956 offset[i] = audio[j] / 32768. / n * win[i];
958 venc->have_saved = 1;
960 venc->have_saved = 0;
965 static int vorbis_encode_init(AVCodecContext * avccontext)
967 venc_context_t * venc = avccontext->priv_data;
969 create_vorbis_context(venc, avccontext);
971 //if (avccontext->flags & CODEC_FLAG_QSCALE) avccontext->global_quality / (float)FF_QP2LAMBDA); else avccontext->bit_rate;
972 //if(avccontext->cutoff > 0) cfreq = avccontext->cutoff / 1000.0;
974 avccontext->extradata_size = put_main_header(venc, (uint8_t**)&avccontext->extradata);
976 avccontext->frame_size = 1 << (venc->blocksize[0] - 1);
978 avccontext->coded_frame = avcodec_alloc_frame();
979 avccontext->coded_frame->key_frame = 1;
984 static int vorbis_encode_frame(AVCodecContext * avccontext, unsigned char * packets, int buf_size, void *data)
986 venc_context_t * venc = avccontext->priv_data;
987 signed short * audio = data;
988 int samples = data ? avccontext->frame_size : 0;
989 vorbis_mode_t * mode;
994 if (!window(venc, audio, samples)) return 0;
995 samples = 1 << (venc->blocksize[0] - 1);
997 init_put_bits(&pb, packets, buf_size);
999 put_bits(&pb, 1, 0); // magic bit
1001 put_bits(&pb, ilog(venc->nmodes - 1), 0); // 0 bits, the mode
1003 mode = &venc->modes[0];
1004 mapping = &venc->mappings[mode->mapping];
1005 if (mode->blockflag) {
1006 put_bits(&pb, 1, 0);
1007 put_bits(&pb, 1, 0);
1010 for (i = 0; i < venc->channels; i++) {
1011 floor_t * fc = &venc->floors[mapping->floor[mapping->mux[i]]];
1012 int posts[fc->values];
1013 floor_fit(venc, fc, &venc->coeffs[i * samples], posts, samples);
1014 floor_encode(venc, fc, &pb, posts, &venc->floor[i * samples], samples);
1017 for (i = 0; i < venc->channels; i++) {
1019 for (j = 0; j < samples; j++) {
1020 venc->coeffs[i * samples + j] /= venc->floor[i * samples + j];
1024 for (i = 0; i < mapping->coupling_steps; i++) {
1025 float * mag = venc->coeffs + mapping->magnitude[i] * samples;
1026 float * ang = venc->coeffs + mapping->angle[i] * samples;
1028 for (j = 0; j < samples; j++) {
1033 if (a > m) mag[j] = a;
1037 if (a > m) mag[j] = m;
1043 residue_encode(venc, &venc->residues[mapping->residue[mapping->mux[0]]], &pb, venc->coeffs, samples, venc->channels);
1045 return (put_bits_count(&pb) + 7) / 8;
1049 static int vorbis_encode_close(AVCodecContext * avccontext)
1051 venc_context_t * venc = avccontext->priv_data;
1054 if (venc->codebooks) for (i = 0; i < venc->ncodebooks; i++) {
1055 av_freep(&venc->codebooks[i].entries);
1056 av_freep(&venc->codebooks[i].quantlist);
1057 av_freep(&venc->codebooks[i].dimentions);
1059 av_freep(&venc->codebooks);
1061 if (venc->floors) for (i = 0; i < venc->nfloors; i++) {
1063 av_freep(&venc->floors[i].classes);
1064 if (venc->floors[i].classes)
1065 for (j = 0; j < venc->floors[i].nclasses; j++)
1066 av_freep(&venc->floors[i].classes[j].books);
1067 av_freep(&venc->floors[i].partition_to_class);
1068 av_freep(&venc->floors[i].list);
1070 av_freep(&venc->floors);
1072 if (venc->residues) for (i = 0; i < venc->nresidues; i++) {
1073 av_freep(&venc->residues[i].books);
1075 av_freep(&venc->residues);
1077 if (venc->mappings) for (i = 0; i < venc->nmappings; i++) {
1078 av_freep(&venc->mappings[i].mux);
1079 av_freep(&venc->mappings[i].floor);
1080 av_freep(&venc->mappings[i].residue);
1082 av_freep(&venc->mappings);
1084 av_freep(&venc->modes);
1086 av_freep(&venc->saved);
1087 av_freep(&venc->samples);
1088 av_freep(&venc->floor);
1089 av_freep(&venc->coeffs);
1091 ff_mdct_end(&venc->mdct[0]);
1092 ff_mdct_end(&venc->mdct[1]);
1094 av_freep(&avccontext->coded_frame);
1095 av_freep(&avccontext->extradata);
1100 AVCodec vorbis_encoder = {
1104 sizeof(venc_context_t),
1106 vorbis_encode_frame,
1107 vorbis_encode_close,
1108 .capabilities= CODEC_CAP_DELAY,