]> git.sesse.net Git - ffmpeg/blob - libavcodec/vorbis_enc.c
10c723edcd807c1f4d5ad17f4ce8c9faead2f99e
[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     mapping_t * mc;
170     int i, book;
171
172     venc->channels = avccontext->channels;
173     venc->sample_rate = avccontext->sample_rate;
174     venc->blocksize[0] = venc->blocksize[1] = 8;
175
176     venc->ncodebooks = 10;
177     venc->codebooks = av_malloc(sizeof(codebook_t) * venc->ncodebooks);
178
179     // codebook 1 - floor1 book, values 0..255
180     cb = &venc->codebooks[0];
181     cb->nentries = 256;
182     cb->entries = av_malloc(sizeof(cb_entry_t) * cb->nentries);
183     for (i = 0; i < cb->nentries; i++) cb->entries[i].len = 8;
184     cb->ndimentions = 0;
185     cb->min = 0.;
186     cb->delta = 0.;
187     cb->seq_p = 0;
188     cb->lookup = 0;
189     cb->quantlist = NULL;
190     ready_codebook(cb);
191
192     // codebook 2 - residue classbook, values 0..1, dimentions 200
193     cb = &venc->codebooks[1];
194     cb->nentries = 2;
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;
198     cb->min = 0.;
199     cb->delta = 0.;
200     cb->seq_p = 0;
201     cb->lookup = 0;
202     cb->quantlist = NULL;
203     ready_codebook(cb);
204
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];
208         cb->nentries = 5;
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;
211         cb->ndimentions = 1;
212         cb->delta = 1 << ((7 - book) * 2);
213         cb->min = -cb->delta*2;
214         cb->seq_p = 0;
215         cb->lookup = 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;
218         ready_codebook(cb);
219     }
220
221     venc->nfloors = 1;
222     venc->floors = av_malloc(sizeof(floor_t) * venc->nfloors);
223
224     // just 1 floor
225     fc = &venc->floors[0];
226     fc->partitions = 1;
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;
229     fc->nclasses = 1;
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];
233         int j, books;
234         c->dim = 1;
235         c->subclass = 0;
236         c->masterbook = 0;
237         books = (1 << c->subclass);
238         c->books = av_malloc(sizeof(int) * books);
239         for (j = 0; j < books; j++) c->books[j] = 0;
240     }
241     fc->multiplier = 1;
242     fc->rangebits = venc->blocksize[0];
243
244     fc->values = 2;
245     for (i = 0; i < fc->partitions; i++)
246         fc->values += fc->classes[fc->partition_to_class[i]].dim;
247
248     fc->list = av_malloc(sizeof(*fc->list) * fc->values);
249     fc->list[0].x = 0;
250     fc->list[1].x = 1 << fc->rangebits;
251     for (i = 2; i < fc->values; i++) fc->list[i].x = i * 5;
252
253     venc->nresidues = 1;
254     venc->residues = av_malloc(sizeof(residue_t) * venc->nresidues);
255
256     // single residue
257     rc = &venc->residues[0];
258     rc->type = 0;
259     rc->begin = 0;
260     rc->end = 1 << venc->blocksize[0];
261     rc->partition_size = 64;
262     rc->classifications = 1;
263     rc->classbook = 1;
264     rc->books = av_malloc(sizeof(int[8]) * rc->classifications);
265     for (i = 0; i < 8; i++) rc->books[0][i] = 2 + i;
266
267     venc->nmappings = 1;
268     venc->mappings = av_malloc(sizeof(mapping_t) * venc->nmappings);
269
270     // single mapping
271     mc = &venc->mappings[0];
272     mc->submaps = 1;
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++) {
278         mc->floor[i] = 0;
279         mc->residue[i] = 0;
280     }
281
282     venc->nmodes = 1;
283     venc->modes = av_malloc(sizeof(vorbis_mode_t) * venc->nmodes);
284
285     // single mode
286     venc->modes[0].blockflag = 0;
287     venc->modes[0].mapping = 0;
288 }
289
290 static inline int ilog(unsigned int a) {
291     int i;
292     for (i = 0; a >> i; i++);
293     return i;
294 }
295
296 static void put_float(PutBitContext * pb, float f) {
297     int exp, mant;
298     uint32_t res = 0;
299     mant = (int)ldexp(frexp(f, &exp), 20);
300     exp += 788 - 20;
301     if (mant < 0) { res |= (1 << 31); mant = -mant; }
302     res |= mant | (exp << 21);
303     put_bits(pb, 32, res);
304 }
305
306 static void put_codebook_header(PutBitContext * pb, codebook_t * cb) {
307     int i;
308     int ordered = 0;
309
310     put_bits(pb, 24, 0x564342); //magic
311     put_bits(pb, 16, cb->ndimentions);
312     put_bits(pb, 24, cb->nentries);
313
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;
316
317     put_bits(pb, 1, ordered);
318     if (ordered) {
319         int len = cb->entries[0].len;
320         put_bits(pb, 5, len);
321         i = 0;
322         while (i < cb->nentries) {
323             int j;
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);
326             i += j;
327             len++;
328         }
329     } else {
330         int sparse = 0;
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);
334
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);
338         }
339     }
340
341     put_bits(pb, 4, cb->lookup);
342     if (cb->lookup) {
343         int tmp = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries);
344         int bits = ilog(cb->quantlist[0]);
345
346         for (i = 1; i < tmp; i++) bits = FFMAX(bits, ilog(cb->quantlist[i]));
347
348         put_float(pb, cb->min);
349         put_float(pb, cb->delta);
350
351         put_bits(pb, 4, bits - 1);
352         put_bits(pb, 1, cb->seq_p);
353
354         for (i = 0; i < tmp; i++) put_bits(pb, bits, cb->quantlist[i]);
355     }
356 }
357
358 static void put_floor_header(PutBitContext * pb, floor_t * fc) {
359     int i;
360
361     put_bits(pb, 16, 1); // type, only floor1 is supported
362
363     put_bits(pb, 5, fc->partitions);
364
365     for (i = 0; i < fc->partitions; i++) put_bits(pb, 4, fc->partition_to_class[i]);
366
367     for (i = 0; i < fc->nclasses; i++) {
368         int j, books;
369
370         put_bits(pb, 3, fc->classes[i].dim - 1);
371         put_bits(pb, 2, fc->classes[i].subclass);
372
373         if (fc->classes[i].subclass) put_bits(pb, 8, fc->classes[i].masterbook);
374
375         books = (1 << fc->classes[i].subclass);
376
377         for (j = 0; j < books; j++) put_bits(pb, 8, fc->classes[i].books[j] + 1);
378     }
379
380     put_bits(pb, 2, fc->multiplier - 1);
381     put_bits(pb, 4, fc->rangebits);
382
383     for (i = 2; i < fc->values; i++) put_bits(pb, fc->rangebits, fc->list[i].x);
384 }
385
386 static void put_residue_header(PutBitContext * pb, residue_t * rc) {
387     int i;
388
389     put_bits(pb, 16, rc->type);
390
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);
396
397     for (i = 0; i < rc->classifications; i++) {
398         int j, tmp = 0;
399         for (j = 0; j < 8; j++) tmp |= (!!rc->books[i][j]) << j;
400
401         put_bits(pb, 3, tmp & 7);
402         put_bits(pb, 1, tmp > 7);
403
404         if (tmp > 7) put_bits(pb, 5, tmp >> 3);
405     }
406
407     for (i = 0; i < rc->classifications; i++) {
408         int j;
409         for (j = 0; j < 8; j++)
410             if (rc->books[i][j])
411                 put_bits(pb, 8, rc->books[i][j]);
412     }
413 }
414
415 static int put_main_header(venc_context_t * venc, uint8_t ** out) {
416     int i;
417     PutBitContext pb;
418     uint8_t buffer[50000] = {0}, * p = buffer;
419     int buffer_len = sizeof buffer;
420     int len, hlens[3];
421
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
435
436     flush_put_bits(&pb);
437     hlens[0] = (put_bits_count(&pb) + 7) / 8;
438     buffer_len -= hlens[0];
439     p += hlens[0];
440
441     // comment header
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
448
449     flush_put_bits(&pb);
450     hlens[1] = (put_bits_count(&pb) + 7) / 8;
451     buffer_len -= hlens[1];
452     p += hlens[1];
453
454     // setup header
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]);
458
459     // codebooks
460     put_bits(&pb, 8, venc->ncodebooks - 1);
461     for (i = 0; i < venc->ncodebooks; i++) put_codebook_header(&pb, &venc->codebooks[i]);
462
463     // time domain, reserved, zero
464     put_bits(&pb, 6, 0);
465     put_bits(&pb, 16, 0);
466
467     // floors
468     put_bits(&pb, 6, venc->nfloors - 1);
469     for (i = 0; i < venc->nfloors; i++) put_floor_header(&pb, &venc->floors[i]);
470
471     // residues
472     put_bits(&pb, 6, venc->nresidues - 1);
473     for (i = 0; i < venc->nresidues; i++) put_residue_header(&pb, &venc->residues[i]);
474
475     // mappings
476     put_bits(&pb, 6, venc->nmappings - 1);
477     for (i = 0; i < venc->nmappings; i++) {
478         mapping_t * mc = &venc->mappings[i];
479         int j;
480         put_bits(&pb, 16, 0); // mapping type
481
482         put_bits(&pb, 1, mc->submaps > 1);
483         if (mc->submaps > 1) put_bits(&pb, 4, mc->submaps - 1);
484
485         put_bits(&pb, 1, 0); // channel coupling
486
487         put_bits(&pb, 2, 0); // reserved
488
489         if (mc->submaps > 1) for (j = 0; j < venc->channels; j++) put_bits(&pb, 4, mc->mux[j]);
490
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]);
495         }
496     }
497
498     // modes
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);
505     }
506
507     flush_put_bits(&pb);
508     hlens[2] = (put_bits_count(&pb) + 7) / 8;
509
510     len = hlens[0] + hlens[1] + hlens[2];
511     p = *out = av_mallocz(64 + len + len/255);
512
513     *p++ = 2;
514     p += av_xiphlacing(p, hlens[0]);
515     p += av_xiphlacing(p, hlens[1]);
516     buffer_len = 0;
517     for (i = 0; i < 3; i++) {
518         memcpy(p, buffer + buffer_len, hlens[i]);
519         p += hlens[i];
520         buffer_len += hlens[i];
521     }
522
523     return p - *out;
524 }
525
526 static int vorbis_encode_init(AVCodecContext * avccontext)
527 {
528     venc_context_t * venc = avccontext->priv_data;
529
530     create_vorbis_context(venc, avccontext);
531
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;
534
535     avccontext->extradata_size = put_main_header(venc, (uint8_t**)&avccontext->extradata);
536
537     avccontext->frame_size = 1 << venc->blocksize[0];
538
539     avccontext->coded_frame = avcodec_alloc_frame();
540     avccontext->coded_frame->key_frame = 1;
541
542     return 0;
543 }
544
545
546 static int vorbis_encode_frame(AVCodecContext * avccontext, unsigned char * packets, int buf_size, void *data)
547 {
548 #if 0
549     venc_context_t * venc = avccontext->priv_data;
550     signed short * audio = data;
551     int samples = data ? avccontext->frame_size : 0;
552
553     avccontext->coded_frame->pts = av_rescale_q(op2->granulepos, (AVRational){1, avccontext->sample_rate}, avccontext->time_base);
554     memcpy(packets, compressed_frame, l);
555 #endif
556     return data ? 50 : 0;
557 }
558
559
560 static int vorbis_encode_close(AVCodecContext * avccontext)
561 {
562     venc_context_t * venc = avccontext->priv_data;
563     int i;
564
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);
569     }
570     av_freep(&venc->codebooks);
571
572     if (venc->floors) for (i = 0; i < venc->nfloors; i++) {
573         int j;
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);
580     }
581     av_freep(&venc->floors);
582
583     if (venc->residues) for (i = 0; i < venc->nresidues; i++) {
584         av_freep(&venc->residues[i].books);
585     }
586     av_freep(&venc->residues);
587
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);
592     }
593     av_freep(&venc->mappings);
594
595     av_freep(&venc->modes);
596
597     av_freep(&avccontext->coded_frame);
598     av_freep(&avccontext->extradata);
599
600     return 0 ;
601 }
602
603 AVCodec oggvorbis_encoder = {
604     "vorbis",
605     CODEC_TYPE_AUDIO,
606     CODEC_ID_VORBIS,
607     sizeof(venc_context_t),
608     vorbis_encode_init,
609     vorbis_encode_frame,
610     vorbis_encode_close,
611     .capabilities= CODEC_CAP_DELAY,
612 };