]> git.sesse.net Git - ffmpeg/blob - libavcodec/vorbis_enc.c
Original Commit: r22 | ods15 | 2006-09-22 13:49:56 +0300 (Fri, 22 Sep 2006) | 2 lines
[ffmpeg] / libavcodec / vorbis_enc.c
1 /*
2  * copyright (c) 2006 Oded Shimon <ods15@ods15.dyndns.org>
3  *
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.
8  *
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.
13  *
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
17  */
18
19 /**
20  * @file vorbis_enc.c
21  * Native Vorbis encoder.
22  * @author Oded Shimon <ods15@ods15.dyndns.org>
23  */
24
25 #include "avcodec.h"
26
27 #undef NDEBUG
28 #include <assert.h>
29
30 #define ALT_BITSTREAM_READER_LE
31 #include "bitstream.h"
32
33 #define VORBIS_FRAME_SIZE 64
34
35 #define BUFFER_SIZE (1024*64)
36
37 typedef struct {
38     int len;
39     uint32_t codeword;
40 } cb_entry_t;
41
42 typedef struct {
43     int nentries;
44     cb_entry_t * entries;
45     int ndimentions;
46     float min;
47     float delta;
48     int seq_p;
49     int lookup;
50     int * quantlist;
51     float * dimentions;
52 } codebook_t;
53
54 typedef struct {
55     int dim;
56     int subclass;
57     int masterbook;
58     int * books;
59 } floor_class_t;
60
61 typedef struct {
62     int partitions;
63     int * partition_to_class;
64     int nclasses;
65     floor_class_t * classes;
66     int multiplier;
67     int rangebits;
68     int values;
69     struct { int x; } * list;
70 } floor_t;
71
72 typedef struct {
73     int type;
74     int begin;
75     int end;
76     int partition_size;
77     int classifications;
78     int classbook;
79     int (*books)[8];
80 } residue_t;
81
82 typedef struct {
83     int submaps;
84     int * mux;
85     int * floor;
86     int * residue;
87 } mapping_t;
88
89 typedef struct {
90     int blockflag;
91     int mapping;
92 } vorbis_mode_t;
93
94 typedef struct {
95     int channels;
96     int sample_rate;
97     int blocksize[2]; // in (1<<n) format
98
99     int ncodebooks;
100     codebook_t * codebooks;
101
102     int nfloors;
103     floor_t * floors;
104
105     int nresidues;
106     residue_t * residues;
107
108     int nmappings;
109     mapping_t * mappings;
110
111     int nmodes;
112     vorbis_mode_t * modes;
113 } venc_context_t;
114
115 static int cb_lookup_vals(int lookup, int dimentions, int entries) {
116     if (lookup == 1) {
117         int tmp, i;
118         for (tmp = 0; ; tmp++) {
119                 int n = 1;
120                 for (i = 0; i < dimentions; i++) n *= tmp;
121                 if (n > entries) break;
122         }
123         return tmp - 1;
124     } else if (lookup == 2) return dimentions * entries;
125     return 0;
126 }
127
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 };
130     int i;
131
132     for (i = 0; i < cb->nentries; i++) {
133         cb_entry_t * e = &cb->entries[i];
134         int j = 0;
135         if (h[0]) h[0] = 0;
136         else for (j = e->len; !h[j]; j--) assert(j);
137         e->codeword = h[j];
138         h[j] = 0;
139         for (j++; j <= e->len; j++) h[j] = e->codeword | (1 << (j - 1));
140     }
141     for (i = 0; i < 33; i++) assert(!h[i]);
142
143     if (!cb->lookup) cb->dimentions = NULL;
144     else {
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++) {
148             float last = 0;
149             int j;
150             int div = 1;
151             for (j = 0; j < cb->ndimentions; j++) {
152                 int off;
153                 if (cb->lookup == 1) off = (i / div) % vals; // lookup type 1
154                 else off = i * cb->ndimentions + j; // lookup type 2
155
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];
158                 div *= vals;
159             }
160         }
161     }
162
163 }
164
165 static void create_vorbis_context(venc_context_t * venc, AVCodecContext * avccontext) {
166     codebook_t * cb;
167     floor_t * fc;
168     residue_t * rc;
169     int i, book;
170
171     venc->channels = avccontext->channels;
172     venc->sample_rate = avccontext->sample_rate;
173     venc->blocksize[0] = venc->blocksize[1] = 8;
174
175     venc->ncodebooks = 10;
176     venc->codebooks = av_malloc(sizeof(codebook_t) * venc->ncodebooks);
177
178     // codebook 1 - floor1 book, values 0..255
179     cb = &venc->codebooks[0];
180     cb->nentries = 256;
181     cb->entries = av_malloc(sizeof(cb_entry_t) * cb->nentries);
182     for (i = 0; i < cb->nentries; i++) cb->entries[i].len = 8;
183     cb->ndimentions = 0;
184     cb->min = 0.;
185     cb->delta = 0.;
186     cb->seq_p = 0;
187     cb->lookup = 0;
188     cb->quantlist = NULL;
189     ready_codebook(cb);
190
191     // codebook 2 - residue classbook, values 0..1, dimentions 200
192     cb = &venc->codebooks[1];
193     cb->nentries = 2;
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;
197     cb->min = 0.;
198     cb->delta = 0.;
199     cb->seq_p = 0;
200     cb->lookup = 0;
201     cb->quantlist = NULL;
202     ready_codebook(cb);
203
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];
207         cb->nentries = 5;
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;
210         cb->ndimentions = 1;
211         cb->delta = 1 << ((7 - book) * 2);
212         cb->min = -cb->delta*2;
213         cb->seq_p = 0;
214         cb->lookup = 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;
217         ready_codebook(cb);
218     }
219
220     venc->nfloors = 1;
221     venc->floors = av_malloc(sizeof(floor_t) * venc->nfloors);
222
223     // just 1 floor
224     fc = &venc->floors[0];
225     fc->partitions = 1;
226     fc->partition_to_class = av_malloc(sizeof(int) * fc->partitions);
227     for (i = 0; i < fc->partitions; i++) fc->partition_to_class = 0;
228     fc->nclasses = 1;
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];
232         int j, books;
233         c->dim = 1;
234         c->subclass = 0;
235         c->masterbook = 0;
236         books = (1 << c->subclass);
237         c->books = av_malloc(sizeof(int) * books);
238         for (j = 0; j < books; j++) c->books[j] = 0;
239     }
240     fc->multiplier = 1;
241     fc->rangebits = venc->blocksize[0];
242
243     fc->values = 2;
244     for (i = 0; i < fc->partitions; i++)
245         fc->values += fc->classes[fc->partition_to_class[i]].dim;
246
247     fc->list = av_malloc(sizeof(*fc->list) * fc->values);
248     fc->list[0].x = 0;
249     fc->list[1].x = 1 << fc->rangebits;
250     for (i = 2; i < fc->values; i++) fc->list[i].x = i * 5;
251
252     venc->nresidues = 1;
253     venc->residues = av_malloc(sizeof(residue_t) * venc->nresidues);
254
255     // single residue
256     rc = &venc->residues[0];
257     rc->type = 0;
258     rc->begin = 0;
259     rc->end = 1 << venc->blocksize[0];
260     rc->partition_size = 64;
261     rc->classifications = 1;
262     rc->classbook = 1;
263     rc->books = av_malloc(sizeof(int[8]) * rc->classifications);
264     for (i = 0; i < 8; i++) rc->books[0][i] = 2 + i;
265
266     venc->nmappings = 1;
267     venc->mappings = av_malloc(sizeof(mapping_t) * venc->nmappings);
268
269     venc->nmodes = 1;
270     venc->modes = av_malloc(sizeof(vorbis_mode_t) * venc->nmodes);
271 }
272
273 static inline int ilog(unsigned int a) {
274     int i;
275     for (i = 0; a >> i; i++);
276     return i;
277 }
278
279 static void put_float(PutBitContext * pb, float f) {
280     int exp, mant;
281     uint32_t res = 0;
282     mant = (int)ldexp(frexp(f, &exp), 20);
283     exp += 788 - 20;
284     if (mant < 0) { res |= (1 << 31); mant = -mant; }
285     res |= mant | (exp << 21);
286     put_bits(pb, 32, res);
287 }
288
289 static void put_codebook_header(PutBitContext * pb, codebook_t * cb) {
290     int i;
291     int ordered = 0;
292
293     put_bits(pb, 24, 0x564342); //magic
294     put_bits(pb, 16, cb->ndimentions);
295     put_bits(pb, 24, cb->nentries);
296
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;
299
300     put_bits(pb, 1, ordered);
301     if (ordered) {
302         int len = cb->entries[0].len;
303         put_bits(pb, 5, len);
304         i = 0;
305         while (i < cb->nentries) {
306             int j;
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);
309             i += j;
310             len++;
311         }
312     } else {
313         int sparse = 0;
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);
317
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);
321         }
322     }
323
324     put_bits(pb, 4, cb->lookup);
325     if (cb->lookup) {
326         int tmp = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries);
327         int bits = ilog(cb->quantlist[0]);
328
329         for (i = 1; i < tmp; i++) bits = FFMAX(bits, ilog(cb->quantlist[i]));
330
331         put_float(pb, cb->min);
332         put_float(pb, cb->delta);
333
334         put_bits(pb, 4, bits - 1);
335         put_bits(pb, 1, cb->seq_p);
336
337         for (i = 0; i < tmp; i++) put_bits(pb, bits, cb->quantlist[i]);
338     }
339 }
340
341 static void put_floor_header(PutBitContext * pb, floor_t * fc) {
342     int i;
343
344     put_bits(pb, 16, 1); // type, only floor1 is supported
345
346     put_bits(pb, 5, fc->partitions);
347
348     for (i = 0; i < fc->partitions; i++) put_bits(pb, 4, fc->partition_to_class[i]);
349
350     for (i = 0; i < fc->nclasses; i++) {
351         int j, books;
352
353         put_bits(pb, 3, fc->classes[i].dim - 1);
354         put_bits(pb, 2, fc->classes[i].subclass);
355
356         if (fc->classes[i].subclass) put_bits(pb, 8, fc->classes[i].masterbook);
357
358         books = (1 << fc->classes[i].subclass);
359
360         for (j = 0; j < books; j++) put_bits(pb, 8, fc->classes[i].books[j] + 1);
361     }
362
363     put_bits(pb, 2, fc->multiplier - 1);
364     put_bits(pb, 4, fc->rangebits);
365
366     for (i = 2; i < fc->values; i++) put_bits(pb, fc->rangebits, fc->list[i].x);
367 }
368
369 static void put_residue_header(PutBitContext * pb, residue_t * rc) {
370     int i;
371
372     put_bits(pb, 16, rc->type);
373
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);
379
380     for (i = 0; i < rc->classifications; i++) {
381         int j, tmp = 0;
382         for (j = 0; j < 8; j++) tmp |= (!!rc->books[i][j]) << j;
383
384         put_bits(pb, 3, tmp & 7);
385         put_bits(pb, 1, tmp > 7);
386
387         if (tmp > 7) put_bits(pb, 5, tmp >> 3);
388     }
389
390     for (i = 0; i < rc->classifications; i++) {
391         int j;
392         for (j = 0; j < 8; j++)
393             if (rc->books[i][j])
394                 put_bits(pb, 8, rc->books[i][j]);
395     }
396 }
397
398 static int put_main_header(venc_context_t * venc, uint8_t ** out) {
399     int i;
400     PutBitContext pb;
401     uint8_t buffer[50000] = {0}, * p = buffer;
402     int buffer_len = sizeof buffer;
403     int len, hlens[3];
404
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
418
419     flush_put_bits(&pb);
420     hlens[0] = (put_bits_count(&pb) + 7) / 8;
421     buffer_len -= hlens[0];
422     p += hlens[0];
423
424     // comment header
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
431
432     flush_put_bits(&pb);
433     hlens[1] = (put_bits_count(&pb) + 7) / 8;
434     buffer_len -= hlens[1];
435     p += hlens[1];
436
437     // setup header
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]);
441
442     // codebooks
443     put_bits(&pb, 8, venc->ncodebooks - 1);
444     for (i = 0; i < venc->ncodebooks; i++) put_codebook_header(&pb, &venc->codebooks[i]);
445
446     // time domain, reserved, zero
447     put_bits(&pb, 6, 0);
448     put_bits(&pb, 16, 0);
449
450     // floors
451     put_bits(&pb, 6, venc->nfloors - 1);
452     for (i = 0; i < venc->nfloors; i++) put_floor_header(&pb, &venc->floors[i]);
453
454     // residues
455     put_bits(&pb, 6, venc->nresidues - 1);
456     for (i = 0; i < venc->nresidues; i++) put_residue_header(&pb, &venc->residues[i]);
457
458     // mappings
459     put_bits(&pb, 6, venc->nmappings - 1);
460     for (i = 0; i < venc->nmappings; i++) {
461         mapping_t * mc = &venc->mappings[i];
462         int j;
463         put_bits(&pb, 16, 0); // mapping type
464
465         put_bits(&pb, 1, mc->submaps > 1);
466         if (mc->submaps > 1) put_bits(&pb, 4, mc->submaps - 1);
467
468         put_bits(&pb, 1, 0); // channel coupling
469
470         put_bits(&pb, 2, 0); // reserved
471
472         if (mc->submaps > 1) for (j = 0; j < venc->channels; j++) put_bits(&pb, 4, mc->mux[j]);
473
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]);
478         }
479     }
480
481     // modes
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);
488     }
489
490     flush_put_bits(&pb);
491     hlens[2] = (put_bits_count(&pb) + 7) / 8;
492
493     len = hlens[0] + hlens[1] + hlens[2];
494     p = *out = av_mallocz(64 + len + len/255);
495
496     *p++ = 2;
497     p += av_xiphlacing(p, hlens[0]);
498     p += av_xiphlacing(p, hlens[1]);
499     buffer_len = 0;
500     for (i = 0; i < 3; i++) {
501         memcpy(p, buffer + buffer_len, hlens[i]);
502         p += hlens[i];
503         buffer_len += hlens[i];
504     }
505
506     return p - *out;
507 }
508
509 static int vorbis_encode_init(AVCodecContext * avccontext)
510 {
511     venc_context_t * venc = avccontext->priv_data;
512
513     create_vorbis_context(venc, avccontext);
514
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;
517
518     avccontext->extradata_size = put_main_header(venc, (uint8_t**)&avccontext->extradata);
519
520     avccontext->frame_size = VORBIS_FRAME_SIZE;
521
522     avccontext->coded_frame = avcodec_alloc_frame();
523     avccontext->coded_frame->key_frame = 1;
524
525     return 0;
526 }
527
528
529 static int vorbis_encode_frame(AVCodecContext * avccontext, unsigned char * packets, int buf_size, void *data)
530 {
531 #if 0
532     venc_context_t * venc = avccontext->priv_data;
533     signed short * audio = data;
534     int samples = data ? VORBIS_FRAME_SIZE : 0;
535
536     avccontext->coded_frame->pts = av_rescale_q(op2->granulepos, (AVRational){1, avccontext->sample_rate}, avccontext->time_base);
537     memcpy(packets, compressed_frame, l);
538 #endif
539     return data ? 50 : 0;
540 }
541
542
543 static int vorbis_encode_close(AVCodecContext * avccontext)
544 {
545     venc_context_t * venc = avccontext->priv_data;
546
547     av_freep(&avccontext->coded_frame);
548     av_freep(&avccontext->extradata);
549
550     return 0 ;
551 }
552
553 AVCodec oggvorbis_encoder = {
554     "vorbis",
555     CODEC_TYPE_AUDIO,
556     CODEC_ID_VORBIS,
557     sizeof(venc_context_t),
558     vorbis_encode_init,
559     vorbis_encode_frame,
560     vorbis_encode_close,
561     .capabilities= CODEC_CAP_DELAY,
562 };