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>
30 #define ALT_BITSTREAM_READER_LE
31 #include "bitstream.h"
33 #define VORBIS_FRAME_SIZE 64
35 #define BUFFER_SIZE (1024*64)
63 int * partition_to_class;
65 floor_class_t * classes;
69 struct { int x; } * list;
97 int blocksize[2]; // in (1<<n) format
100 codebook_t * codebooks;
106 residue_t * residues;
109 mapping_t * mappings;
112 vorbis_mode_t * modes;
115 static int cb_lookup_vals(int lookup, int dimentions, int entries) {
118 for (tmp = 0; ; tmp++) {
120 for (i = 0; i < dimentions; i++) n *= tmp;
121 if (n > entries) break;
124 } else if (lookup == 2) return dimentions * entries;
128 static void ready_codebook(codebook_t * cb) {
129 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 };
132 for (i = 0; i < cb->nentries; i++) {
133 cb_entry_t * e = &cb->entries[i];
136 else for (j = e->len; !h[j]; j--) assert(j);
139 for (j++; j <= e->len; j++) h[j] = e->codeword | (1 << (j - 1));
141 for (i = 0; i < 33; i++) assert(!h[i]);
143 if (!cb->lookup) cb->dimentions = NULL;
145 int vals = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries);
146 cb->dimentions = av_malloc(sizeof(float) * cb->nentries * cb->ndimentions);
147 for (i = 0; i < cb->nentries; i++) {
151 for (j = 0; j < cb->ndimentions; j++) {
153 if (cb->lookup == 1) off = (i / div) % vals; // lookup type 1
154 else off = i * cb->ndimentions + j; // lookup type 2
156 cb->dimentions[i * cb->ndimentions + j] = last + cb->min + cb->quantlist[off] * cb->delta;
157 if (cb->seq_p) last = cb->dimentions[i * cb->ndimentions + j];
165 static void create_vorbis_context(venc_context_t * venc, AVCodecContext * avccontext) {
172 venc->channels = avccontext->channels;
173 venc->sample_rate = avccontext->sample_rate;
174 venc->blocksize[0] = venc->blocksize[1] = 8;
176 venc->ncodebooks = 10;
177 venc->codebooks = av_malloc(sizeof(codebook_t) * venc->ncodebooks);
179 // codebook 1 - floor1 book, values 0..255
180 cb = &venc->codebooks[0];
182 cb->entries = av_malloc(sizeof(cb_entry_t) * cb->nentries);
183 for (i = 0; i < cb->nentries; i++) cb->entries[i].len = 8;
189 cb->quantlist = NULL;
192 // codebook 2 - residue classbook, values 0..1, dimentions 200
193 cb = &venc->codebooks[1];
195 cb->entries = av_malloc(sizeof(cb_entry_t) * cb->nentries);
196 for (i = 0; i < cb->nentries; i++) cb->entries[i].len = 1;
197 cb->ndimentions = 200;
202 cb->quantlist = NULL;
205 // codebook 3..10 - vector, for the residue, values -32767..32767, dimentions 1
206 for (book = 0; book < 8; book++) {
207 cb = &venc->codebooks[2 + book];
209 cb->entries = av_malloc(sizeof(cb_entry_t) * cb->nentries);
210 for (i = 0; i < cb->nentries; i++) cb->entries[i].len = i == 2 ? 1 : 3;
212 cb->delta = 1 << ((7 - book) * 2);
213 cb->min = -cb->delta*2;
216 cb->quantlist = av_malloc(sizeof(int) * cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries));
217 for (i = 0; i < cb->nentries; i++) cb->quantlist[i] = i;
222 venc->floors = av_malloc(sizeof(floor_t) * venc->nfloors);
225 fc = &venc->floors[0];
227 fc->partition_to_class = av_malloc(sizeof(int) * fc->partitions);
228 for (i = 0; i < fc->partitions; i++) fc->partition_to_class[i] = 0;
230 fc->classes = av_malloc(sizeof(floor_class_t) * fc->nclasses);
231 for (i = 0; i < fc->nclasses; i++) {
232 floor_class_t * c = &fc->classes[i];
237 books = (1 << c->subclass);
238 c->books = av_malloc(sizeof(int) * books);
239 for (j = 0; j < books; j++) c->books[j] = 0;
242 fc->rangebits = venc->blocksize[0];
245 for (i = 0; i < fc->partitions; i++)
246 fc->values += fc->classes[fc->partition_to_class[i]].dim;
248 fc->list = av_malloc(sizeof(*fc->list) * fc->values);
250 fc->list[1].x = 1 << fc->rangebits;
251 for (i = 2; i < fc->values; i++) fc->list[i].x = i * 5;
254 venc->residues = av_malloc(sizeof(residue_t) * venc->nresidues);
257 rc = &venc->residues[0];
260 rc->end = 1 << venc->blocksize[0];
261 rc->partition_size = 64;
262 rc->classifications = 1;
264 rc->books = av_malloc(sizeof(int[8]) * rc->classifications);
265 for (i = 0; i < 8; i++) rc->books[0][i] = 2 + i;
268 venc->mappings = av_malloc(sizeof(mapping_t) * venc->nmappings);
271 mc = &venc->mappings[0];
273 mc->mux = av_malloc(sizeof(int) * venc->channels);
274 for (i = 0; i < venc->channels; i++) mc->mux[i] = 0;
275 mc->floor = av_malloc(sizeof(int) * mc->submaps);
276 mc->residue = av_malloc(sizeof(int) * mc->submaps);
277 for (i = 0; i < mc->submaps; i++) {
283 venc->modes = av_malloc(sizeof(vorbis_mode_t) * venc->nmodes);
286 venc->modes[0].blockflag = 0;
287 venc->modes[0].mapping = 0;
290 static inline int ilog(unsigned int a) {
292 for (i = 0; a >> i; i++);
296 static void put_float(PutBitContext * pb, float f) {
299 mant = (int)ldexp(frexp(f, &exp), 20);
301 if (mant < 0) { res |= (1 << 31); mant = -mant; }
302 res |= mant | (exp << 21);
303 put_bits(pb, 32, res);
306 static void put_codebook_header(PutBitContext * pb, codebook_t * cb) {
310 put_bits(pb, 24, 0x564342); //magic
311 put_bits(pb, 16, cb->ndimentions);
312 put_bits(pb, 24, cb->nentries);
314 for (i = 1; i < cb->nentries; i++) if (cb->entries[i].len < cb->entries[i-1].len) break;
315 if (i == cb->nentries) ordered = 1;
317 put_bits(pb, 1, ordered);
319 int len = cb->entries[0].len;
320 put_bits(pb, 5, len);
322 while (i < cb->nentries) {
324 for (j = 0; j+i < cb->nentries; j++) if (cb->entries[j+i].len != len) break;
325 put_bits(pb, ilog(cb->nentries - i), j);
331 for (i = 0; i < cb->nentries; i++) if (!cb->entries[i].len) break;
332 if (i != cb->nentries) sparse = 1;
333 put_bits(pb, 1, sparse);
335 for (i = 0; i < cb->nentries; i++) {
336 if (sparse) put_bits(pb, 1, !!cb->entries[i].len);
337 if (cb->entries[i].len) put_bits(pb, 5, cb->entries[i].len);
341 put_bits(pb, 4, cb->lookup);
343 int tmp = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries);
344 int bits = ilog(cb->quantlist[0]);
346 for (i = 1; i < tmp; i++) bits = FFMAX(bits, ilog(cb->quantlist[i]));
348 put_float(pb, cb->min);
349 put_float(pb, cb->delta);
351 put_bits(pb, 4, bits - 1);
352 put_bits(pb, 1, cb->seq_p);
354 for (i = 0; i < tmp; i++) put_bits(pb, bits, cb->quantlist[i]);
358 static void put_floor_header(PutBitContext * pb, floor_t * fc) {
361 put_bits(pb, 16, 1); // type, only floor1 is supported
363 put_bits(pb, 5, fc->partitions);
365 for (i = 0; i < fc->partitions; i++) put_bits(pb, 4, fc->partition_to_class[i]);
367 for (i = 0; i < fc->nclasses; i++) {
370 put_bits(pb, 3, fc->classes[i].dim - 1);
371 put_bits(pb, 2, fc->classes[i].subclass);
373 if (fc->classes[i].subclass) put_bits(pb, 8, fc->classes[i].masterbook);
375 books = (1 << fc->classes[i].subclass);
377 for (j = 0; j < books; j++) put_bits(pb, 8, fc->classes[i].books[j] + 1);
380 put_bits(pb, 2, fc->multiplier - 1);
381 put_bits(pb, 4, fc->rangebits);
383 for (i = 2; i < fc->values; i++) put_bits(pb, fc->rangebits, fc->list[i].x);
386 static void put_residue_header(PutBitContext * pb, residue_t * rc) {
389 put_bits(pb, 16, rc->type);
391 put_bits(pb, 24, rc->begin);
392 put_bits(pb, 24, rc->end);
393 put_bits(pb, 24, rc->partition_size - 1);
394 put_bits(pb, 6, rc->classifications);
395 put_bits(pb, 8, rc->classbook);
397 for (i = 0; i < rc->classifications; i++) {
399 for (j = 0; j < 8; j++) tmp |= (!!rc->books[i][j]) << j;
401 put_bits(pb, 3, tmp & 7);
402 put_bits(pb, 1, tmp > 7);
404 if (tmp > 7) put_bits(pb, 5, tmp >> 3);
407 for (i = 0; i < rc->classifications; i++) {
409 for (j = 0; j < 8; j++)
411 put_bits(pb, 8, rc->books[i][j]);
415 static int put_main_header(venc_context_t * venc, uint8_t ** out) {
418 uint8_t buffer[50000] = {0}, * p = buffer;
419 int buffer_len = sizeof buffer;
422 // identification header
423 init_put_bits(&pb, p, buffer_len);
424 put_bits(&pb, 8, 1); //magic
425 for (i = 0; "vorbis"[i]; i++) put_bits(&pb, 8, "vorbis"[i]);
426 put_bits(&pb, 32, 0); // version
427 put_bits(&pb, 8, venc->channels);
428 put_bits(&pb, 32, venc->sample_rate);
429 put_bits(&pb, 32, 0); // bitrate
430 put_bits(&pb, 32, 0); // bitrate
431 put_bits(&pb, 32, 0); // bitrate
432 put_bits(&pb, 4, venc->blocksize[0]);
433 put_bits(&pb, 4, venc->blocksize[1]);
434 put_bits(&pb, 1, 1); // framing
437 hlens[0] = (put_bits_count(&pb) + 7) / 8;
438 buffer_len -= hlens[0];
442 init_put_bits(&pb, p, buffer_len);
443 put_bits(&pb, 8, 3); //magic
444 for (i = 0; "vorbis"[i]; i++) put_bits(&pb, 8, "vorbis"[i]);
445 put_bits(&pb, 32, 0); // vendor length TODO
446 put_bits(&pb, 32, 0); // amount of comments
447 put_bits(&pb, 1, 1); // framing
450 hlens[1] = (put_bits_count(&pb) + 7) / 8;
451 buffer_len -= hlens[1];
455 init_put_bits(&pb, p, buffer_len);
456 put_bits(&pb, 8, 5); //magic
457 for (i = 0; "vorbis"[i]; i++) put_bits(&pb, 8, "vorbis"[i]);
460 put_bits(&pb, 8, venc->ncodebooks - 1);
461 for (i = 0; i < venc->ncodebooks; i++) put_codebook_header(&pb, &venc->codebooks[i]);
463 // time domain, reserved, zero
465 put_bits(&pb, 16, 0);
468 put_bits(&pb, 6, venc->nfloors - 1);
469 for (i = 0; i < venc->nfloors; i++) put_floor_header(&pb, &venc->floors[i]);
472 put_bits(&pb, 6, venc->nresidues - 1);
473 for (i = 0; i < venc->nresidues; i++) put_residue_header(&pb, &venc->residues[i]);
476 put_bits(&pb, 6, venc->nmappings - 1);
477 for (i = 0; i < venc->nmappings; i++) {
478 mapping_t * mc = &venc->mappings[i];
480 put_bits(&pb, 16, 0); // mapping type
482 put_bits(&pb, 1, mc->submaps > 1);
483 if (mc->submaps > 1) put_bits(&pb, 4, mc->submaps - 1);
485 put_bits(&pb, 1, 0); // channel coupling
487 put_bits(&pb, 2, 0); // reserved
489 if (mc->submaps > 1) for (j = 0; j < venc->channels; j++) put_bits(&pb, 4, mc->mux[j]);
491 for (j = 0; j < mc->submaps; j++) {
492 put_bits(&pb, 8, 0); // reserved time configuration
493 put_bits(&pb, 8, mc->floor[j]);
494 put_bits(&pb, 8, mc->residue[j]);
499 put_bits(&pb, 6, venc->nmodes - 1);
500 for (i = 0; i < venc->nmodes; i++) {
501 put_bits(&pb, 1, venc->modes[i].blockflag);
502 put_bits(&pb, 16, 0); // reserved window type
503 put_bits(&pb, 16, 0); // reserved transform type
504 put_bits(&pb, 8, venc->modes[i].mapping);
508 hlens[2] = (put_bits_count(&pb) + 7) / 8;
510 len = hlens[0] + hlens[1] + hlens[2];
511 p = *out = av_mallocz(64 + len + len/255);
514 p += av_xiphlacing(p, hlens[0]);
515 p += av_xiphlacing(p, hlens[1]);
517 for (i = 0; i < 3; i++) {
518 memcpy(p, buffer + buffer_len, hlens[i]);
520 buffer_len += hlens[i];
526 static int vorbis_encode_init(AVCodecContext * avccontext)
528 venc_context_t * venc = avccontext->priv_data;
530 create_vorbis_context(venc, avccontext);
532 //if (avccontext->flags & CODEC_FLAG_QSCALE) avccontext->global_quality / (float)FF_QP2LAMBDA); else avccontext->bit_rate;
533 //if(avccontext->cutoff > 0) cfreq = avccontext->cutoff / 1000.0;
535 avccontext->extradata_size = put_main_header(venc, (uint8_t**)&avccontext->extradata);
537 avccontext->frame_size = 1 << venc->blocksize[0];
539 avccontext->coded_frame = avcodec_alloc_frame();
540 avccontext->coded_frame->key_frame = 1;
546 static int vorbis_encode_frame(AVCodecContext * avccontext, unsigned char * packets, int buf_size, void *data)
549 venc_context_t * venc = avccontext->priv_data;
550 signed short * audio = data;
551 int samples = data ? avccontext->frame_size : 0;
553 avccontext->coded_frame->pts = av_rescale_q(op2->granulepos, (AVRational){1, avccontext->sample_rate}, avccontext->time_base);
554 memcpy(packets, compressed_frame, l);
556 return data ? 50 : 0;
560 static int vorbis_encode_close(AVCodecContext * avccontext)
562 venc_context_t * venc = avccontext->priv_data;
565 if (venc->codebooks) for (i = 0; i < venc->ncodebooks; i++) {
566 av_freep(&venc->codebooks[i].entries);
567 av_freep(&venc->codebooks[i].quantlist);
568 av_freep(&venc->codebooks[i].dimentions);
570 av_freep(&venc->codebooks);
572 if (venc->floors) for (i = 0; i < venc->nfloors; i++) {
574 av_freep(&venc->floors[i].classes);
575 if (venc->floors[i].classes)
576 for (j = 0; j < venc->floors[i].nclasses; j++)
577 av_freep(&venc->floors[i].classes[j].books);
578 av_freep(&venc->floors[i].partition_to_class);
579 av_freep(&venc->floors[i].list);
581 av_freep(&venc->floors);
583 if (venc->residues) for (i = 0; i < venc->nresidues; i++) {
584 av_freep(&venc->residues[i].books);
586 av_freep(&venc->residues);
588 if (venc->mappings) for (i = 0; i < venc->nmappings; i++) {
589 av_freep(&venc->mappings[i].mux);
590 av_freep(&venc->mappings[i].floor);
591 av_freep(&venc->mappings[i].residue);
593 av_freep(&venc->mappings);
595 av_freep(&venc->modes);
597 av_freep(&avccontext->coded_frame);
598 av_freep(&avccontext->extradata);
603 AVCodec oggvorbis_encoder = {
607 sizeof(venc_context_t),
611 .capabilities= CODEC_CAP_DELAY,