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) {
171 venc->channels = avccontext->channels;
172 venc->sample_rate = avccontext->sample_rate;
173 venc->blocksize[0] = venc->blocksize[1] = 8;
175 venc->ncodebooks = 10;
176 venc->codebooks = av_malloc(sizeof(codebook_t) * venc->ncodebooks);
178 // codebook 1 - floor1 book, values 0..255
179 cb = &venc->codebooks[0];
181 cb->entries = av_malloc(sizeof(cb_entry_t) * cb->nentries);
182 for (i = 0; i < cb->nentries; i++) cb->entries[i].len = 8;
188 cb->quantlist = NULL;
191 // codebook 2 - residue classbook, values 0..1, dimentions 200
192 cb = &venc->codebooks[1];
194 cb->entries = av_malloc(sizeof(cb_entry_t) * cb->nentries);
195 for (i = 0; i < cb->nentries; i++) cb->entries[i].len = 1;
196 cb->ndimentions = 200;
201 cb->quantlist = NULL;
204 // codebook 3..10 - vector, for the residue, values -32767..32767, dimentions 1
205 for (book = 0; book < 8; book++) {
206 cb = &venc->codebooks[2 + book];
208 cb->entries = av_malloc(sizeof(cb_entry_t) * cb->nentries);
209 for (i = 0; i < cb->nentries; i++) cb->entries[i].len = i == 2 ? 1 : 3;
211 cb->delta = 1 << ((7 - book) * 2);
212 cb->min = -cb->delta*2;
215 cb->quantlist = av_malloc(sizeof(int) * cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries));
216 for (i = 0; i < cb->nentries; i++) cb->quantlist[i] = i;
221 venc->floors = av_malloc(sizeof(floor_t) * venc->nfloors);
224 fc = &venc->floors[0];
226 fc->partition_to_class = av_malloc(sizeof(int) * fc->partitions);
227 for (i = 0; i < fc->partitions; i++) fc->partition_to_class = 0;
229 fc->classes = av_malloc(sizeof(floor_class_t) * fc->nclasses);
230 for (i = 0; i < fc->nclasses; i++) {
231 floor_class_t * c = &fc->classes[i];
236 books = (1 << c->subclass);
237 c->books = av_malloc(sizeof(int) * books);
238 for (j = 0; j < books; j++) c->books[j] = 0;
241 fc->rangebits = venc->blocksize[0];
244 for (i = 0; i < fc->partitions; i++)
245 fc->values += fc->classes[fc->partition_to_class[i]].dim;
247 fc->list = av_malloc(sizeof(*fc->list) * fc->values);
249 fc->list[1].x = 1 << fc->rangebits;
250 for (i = 2; i < fc->values; i++) fc->list[i].x = i * 5;
253 venc->residues = av_malloc(sizeof(residue_t) * venc->nresidues);
256 rc = &venc->residues[0];
259 rc->end = 1 << venc->blocksize[0];
260 rc->partition_size = 64;
261 rc->classifications = 1;
263 rc->books = av_malloc(sizeof(int[8]) * rc->classifications);
264 for (i = 0; i < 8; i++) rc->books[0][i] = 2 + i;
267 venc->mappings = av_malloc(sizeof(mapping_t) * venc->nmappings);
270 venc->modes = av_malloc(sizeof(vorbis_mode_t) * venc->nmodes);
273 static inline int ilog(unsigned int a) {
275 for (i = 0; a >> i; i++);
279 static void put_float(PutBitContext * pb, float f) {
282 mant = (int)ldexp(frexp(f, &exp), 20);
284 if (mant < 0) { res |= (1 << 31); mant = -mant; }
285 res |= mant | (exp << 21);
286 put_bits(pb, 32, res);
289 static void put_codebook_header(PutBitContext * pb, codebook_t * cb) {
293 put_bits(pb, 24, 0x564342); //magic
294 put_bits(pb, 16, cb->ndimentions);
295 put_bits(pb, 24, cb->nentries);
297 for (i = 1; i < cb->nentries; i++) if (cb->entries[i].len < cb->entries[i-1].len) break;
298 if (i == cb->nentries) ordered = 1;
300 put_bits(pb, 1, ordered);
302 int len = cb->entries[0].len;
303 put_bits(pb, 5, len);
305 while (i < cb->nentries) {
307 for (j = 0; j+i < cb->nentries; j++) if (cb->entries[j+i].len != len) break;
308 put_bits(pb, ilog(cb->nentries - i), j);
314 for (i = 0; i < cb->nentries; i++) if (!cb->entries[i].len) break;
315 if (i != cb->nentries) sparse = 1;
316 put_bits(pb, 1, sparse);
318 for (i = 0; i < cb->nentries; i++) {
319 if (sparse) put_bits(pb, 1, !!cb->entries[i].len);
320 if (cb->entries[i].len) put_bits(pb, 5, cb->entries[i].len);
324 put_bits(pb, 4, cb->lookup);
326 int tmp = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries);
327 int bits = ilog(cb->quantlist[0]);
329 for (i = 1; i < tmp; i++) bits = FFMAX(bits, ilog(cb->quantlist[i]));
331 put_float(pb, cb->min);
332 put_float(pb, cb->delta);
334 put_bits(pb, 4, bits - 1);
335 put_bits(pb, 1, cb->seq_p);
337 for (i = 0; i < tmp; i++) put_bits(pb, bits, cb->quantlist[i]);
341 static void put_floor_header(PutBitContext * pb, floor_t * fc) {
344 put_bits(pb, 16, 1); // type, only floor1 is supported
346 put_bits(pb, 5, fc->partitions);
348 for (i = 0; i < fc->partitions; i++) put_bits(pb, 4, fc->partition_to_class[i]);
350 for (i = 0; i < fc->nclasses; i++) {
353 put_bits(pb, 3, fc->classes[i].dim - 1);
354 put_bits(pb, 2, fc->classes[i].subclass);
356 if (fc->classes[i].subclass) put_bits(pb, 8, fc->classes[i].masterbook);
358 books = (1 << fc->classes[i].subclass);
360 for (j = 0; j < books; j++) put_bits(pb, 8, fc->classes[i].books[j] + 1);
363 put_bits(pb, 2, fc->multiplier - 1);
364 put_bits(pb, 4, fc->rangebits);
366 for (i = 2; i < fc->values; i++) put_bits(pb, fc->rangebits, fc->list[i].x);
369 static void put_residue_header(PutBitContext * pb, residue_t * rc) {
372 put_bits(pb, 16, rc->type);
374 put_bits(pb, 24, rc->begin);
375 put_bits(pb, 24, rc->end);
376 put_bits(pb, 24, rc->partition_size - 1);
377 put_bits(pb, 6, rc->classifications);
378 put_bits(pb, 8, rc->classbook);
380 for (i = 0; i < rc->classifications; i++) {
382 for (j = 0; j < 8; j++) tmp |= (!!rc->books[i][j]) << j;
384 put_bits(pb, 3, tmp & 7);
385 put_bits(pb, 1, tmp > 7);
387 if (tmp > 7) put_bits(pb, 5, tmp >> 3);
390 for (i = 0; i < rc->classifications; i++) {
392 for (j = 0; j < 8; j++)
394 put_bits(pb, 8, rc->books[i][j]);
398 static int put_main_header(venc_context_t * venc, uint8_t ** out) {
401 uint8_t buffer[50000] = {0}, * p = buffer;
402 int buffer_len = sizeof buffer;
405 // identification header
406 init_put_bits(&pb, p, buffer_len);
407 put_bits(&pb, 8, 1); //magic
408 for (i = 0; "vorbis"[i]; i++) put_bits(&pb, 8, "vorbis"[i]);
409 put_bits(&pb, 32, 0); // version
410 put_bits(&pb, 8, venc->channels);
411 put_bits(&pb, 32, venc->sample_rate);
412 put_bits(&pb, 32, 0); // bitrate
413 put_bits(&pb, 32, 0); // bitrate
414 put_bits(&pb, 32, 0); // bitrate
415 put_bits(&pb, 4, venc->blocksize[0]);
416 put_bits(&pb, 4, venc->blocksize[1]);
417 put_bits(&pb, 1, 1); // framing
420 hlens[0] = (put_bits_count(&pb) + 7) / 8;
421 buffer_len -= hlens[0];
425 init_put_bits(&pb, p, buffer_len);
426 put_bits(&pb, 8, 3); //magic
427 for (i = 0; "vorbis"[i]; i++) put_bits(&pb, 8, "vorbis"[i]);
428 put_bits(&pb, 32, 0); // vendor length TODO
429 put_bits(&pb, 32, 0); // amount of comments
430 put_bits(&pb, 1, 1); // framing
433 hlens[1] = (put_bits_count(&pb) + 7) / 8;
434 buffer_len -= hlens[1];
438 init_put_bits(&pb, p, buffer_len);
439 put_bits(&pb, 8, 5); //magic
440 for (i = 0; "vorbis"[i]; i++) put_bits(&pb, 8, "vorbis"[i]);
443 put_bits(&pb, 8, venc->ncodebooks - 1);
444 for (i = 0; i < venc->ncodebooks; i++) put_codebook_header(&pb, &venc->codebooks[i]);
446 // time domain, reserved, zero
448 put_bits(&pb, 16, 0);
451 put_bits(&pb, 6, venc->nfloors - 1);
452 for (i = 0; i < venc->nfloors; i++) put_floor_header(&pb, &venc->floors[i]);
455 put_bits(&pb, 6, venc->nresidues - 1);
456 for (i = 0; i < venc->nresidues; i++) put_residue_header(&pb, &venc->residues[i]);
459 put_bits(&pb, 6, venc->nmappings - 1);
460 for (i = 0; i < venc->nmappings; i++) {
461 mapping_t * mc = &venc->mappings[i];
463 put_bits(&pb, 16, 0); // mapping type
465 put_bits(&pb, 1, mc->submaps > 1);
466 if (mc->submaps > 1) put_bits(&pb, 4, mc->submaps - 1);
468 put_bits(&pb, 1, 0); // channel coupling
470 put_bits(&pb, 2, 0); // reserved
472 if (mc->submaps > 1) for (j = 0; j < venc->channels; j++) put_bits(&pb, 4, mc->mux[j]);
474 for (j = 0; j < mc->submaps; j++) {
475 put_bits(&pb, 8, 0); // reserved time configuration
476 put_bits(&pb, 8, mc->floor[j]);
477 put_bits(&pb, 8, mc->residue[j]);
482 put_bits(&pb, 6, venc->nmodes - 1);
483 for (i = 0; i < venc->nmodes; i++) {
484 put_bits(&pb, 1, venc->modes[i].blockflag);
485 put_bits(&pb, 16, 0); // reserved window type
486 put_bits(&pb, 16, 0); // reserved transform type
487 put_bits(&pb, 8, venc->modes[i].mapping);
491 hlens[2] = (put_bits_count(&pb) + 7) / 8;
493 len = hlens[0] + hlens[1] + hlens[2];
494 p = *out = av_mallocz(64 + len + len/255);
497 p += av_xiphlacing(p, hlens[0]);
498 p += av_xiphlacing(p, hlens[1]);
500 for (i = 0; i < 3; i++) {
501 memcpy(p, buffer + buffer_len, hlens[i]);
503 buffer_len += hlens[i];
509 static int vorbis_encode_init(AVCodecContext * avccontext)
511 venc_context_t * venc = avccontext->priv_data;
513 create_vorbis_context(venc, avccontext);
515 //if (avccontext->flags & CODEC_FLAG_QSCALE) avccontext->global_quality / (float)FF_QP2LAMBDA); else avccontext->bit_rate;
516 //if(avccontext->cutoff > 0) cfreq = avccontext->cutoff / 1000.0;
518 avccontext->extradata_size = put_main_header(venc, (uint8_t**)&avccontext->extradata);
520 avccontext->frame_size = VORBIS_FRAME_SIZE;
522 avccontext->coded_frame = avcodec_alloc_frame();
523 avccontext->coded_frame->key_frame = 1;
529 static int vorbis_encode_frame(AVCodecContext * avccontext, unsigned char * packets, int buf_size, void *data)
532 venc_context_t * venc = avccontext->priv_data;
533 signed short * audio = data;
534 int samples = data ? VORBIS_FRAME_SIZE : 0;
536 avccontext->coded_frame->pts = av_rescale_q(op2->granulepos, (AVRational){1, avccontext->sample_rate}, avccontext->time_base);
537 memcpy(packets, compressed_frame, l);
539 return data ? 50 : 0;
543 static int vorbis_encode_close(AVCodecContext * avccontext)
545 venc_context_t * venc = avccontext->priv_data;
547 av_freep(&avccontext->coded_frame);
548 av_freep(&avccontext->extradata);
553 AVCodec oggvorbis_encoder = {
557 sizeof(venc_context_t),
561 .capabilities= CODEC_CAP_DELAY,