cosmetics: Add '0' to float constants ending in '.'.
[libav.git] / libavcodec / vorbisenc.c
1 /*
2 * copyright (c) 2006 Oded Shimon <ods15@ods15.dyndns.org>
3 *
4 * This file is part of Libav.
5 *
6 * Libav is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * Libav is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Libav; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 /**
22 * @file
23 * Native Vorbis encoder.
24 * @author Oded Shimon <ods15@ods15.dyndns.org>
25 */
26
27 #include <float.h>
28 #include "avcodec.h"
29 #include "internal.h"
30 #include "fft.h"
31 #include "vorbis.h"
32 #include "vorbis_enc_data.h"
33
34 #define BITSTREAM_WRITER_LE
35 #include "put_bits.h"
36
37 #undef NDEBUG
38 #include <assert.h>
39
40 typedef struct {
41 int nentries;
42 uint8_t *lens;
43 uint32_t *codewords;
44 int ndimensions;
45 float min;
46 float delta;
47 int seq_p;
48 int lookup;
49 int *quantlist;
50 float *dimensions;
51 float *pow2;
52 } vorbis_enc_codebook;
53
54 typedef struct {
55 int dim;
56 int subclass;
57 int masterbook;
58 int *books;
59 } vorbis_enc_floor_class;
60
61 typedef struct {
62 int partitions;
63 int *partition_to_class;
64 int nclasses;
65 vorbis_enc_floor_class *classes;
66 int multiplier;
67 int rangebits;
68 int values;
69 vorbis_floor1_entry *list;
70 } vorbis_enc_floor;
71
72 typedef struct {
73 int type;
74 int begin;
75 int end;
76 int partition_size;
77 int classifications;
78 int classbook;
79 int8_t (*books)[8];
80 float (*maxes)[2];
81 } vorbis_enc_residue;
82
83 typedef struct {
84 int submaps;
85 int *mux;
86 int *floor;
87 int *residue;
88 int coupling_steps;
89 int *magnitude;
90 int *angle;
91 } vorbis_enc_mapping;
92
93 typedef struct {
94 int blockflag;
95 int mapping;
96 } vorbis_enc_mode;
97
98 typedef struct {
99 int channels;
100 int sample_rate;
101 int log2_blocksize[2];
102 FFTContext mdct[2];
103 const float *win[2];
104 int have_saved;
105 float *saved;
106 float *samples;
107 float *floor; // also used for tmp values for mdct
108 float *coeffs; // also used for residue after floor
109 float quality;
110
111 int ncodebooks;
112 vorbis_enc_codebook *codebooks;
113
114 int nfloors;
115 vorbis_enc_floor *floors;
116
117 int nresidues;
118 vorbis_enc_residue *residues;
119
120 int nmappings;
121 vorbis_enc_mapping *mappings;
122
123 int nmodes;
124 vorbis_enc_mode *modes;
125
126 int64_t next_pts;
127 } vorbis_enc_context;
128
129 #define MAX_CHANNELS 2
130 #define MAX_CODEBOOK_DIM 8
131
132 #define MAX_FLOOR_CLASS_DIM 4
133 #define NUM_FLOOR_PARTITIONS 8
134 #define MAX_FLOOR_VALUES (MAX_FLOOR_CLASS_DIM*NUM_FLOOR_PARTITIONS+2)
135
136 #define RESIDUE_SIZE 1600
137 #define RESIDUE_PART_SIZE 32
138 #define NUM_RESIDUE_PARTITIONS (RESIDUE_SIZE/RESIDUE_PART_SIZE)
139
140 static inline int put_codeword(PutBitContext *pb, vorbis_enc_codebook *cb,
141 int entry)
142 {
143 assert(entry >= 0);
144 assert(entry < cb->nentries);
145 assert(cb->lens[entry]);
146 if (pb->size_in_bits - put_bits_count(pb) < cb->lens[entry])
147 return AVERROR(EINVAL);
148 put_bits(pb, cb->lens[entry], cb->codewords[entry]);
149 return 0;
150 }
151
152 static int cb_lookup_vals(int lookup, int dimensions, int entries)
153 {
154 if (lookup == 1)
155 return ff_vorbis_nth_root(entries, dimensions);
156 else if (lookup == 2)
157 return dimensions *entries;
158 return 0;
159 }
160
161 static int ready_codebook(vorbis_enc_codebook *cb)
162 {
163 int i;
164
165 ff_vorbis_len2vlc(cb->lens, cb->codewords, cb->nentries);
166
167 if (!cb->lookup) {
168 cb->pow2 = cb->dimensions = NULL;
169 } else {
170 int vals = cb_lookup_vals(cb->lookup, cb->ndimensions, cb->nentries);
171 cb->dimensions = av_malloc(sizeof(float) * cb->nentries * cb->ndimensions);
172 cb->pow2 = av_mallocz(sizeof(float) * cb->nentries);
173 if (!cb->dimensions || !cb->pow2)
174 return AVERROR(ENOMEM);
175 for (i = 0; i < cb->nentries; i++) {
176 float last = 0;
177 int j;
178 int div = 1;
179 for (j = 0; j < cb->ndimensions; j++) {
180 int off;
181 if (cb->lookup == 1)
182 off = (i / div) % vals; // lookup type 1
183 else
184 off = i * cb->ndimensions + j; // lookup type 2
185
186 cb->dimensions[i * cb->ndimensions + j] = last + cb->min + cb->quantlist[off] * cb->delta;
187 if (cb->seq_p)
188 last = cb->dimensions[i * cb->ndimensions + j];
189 cb->pow2[i] += cb->dimensions[i * cb->ndimensions + j] * cb->dimensions[i * cb->ndimensions + j];
190 div *= vals;
191 }
192 cb->pow2[i] /= 2.0;
193 }
194 }
195 return 0;
196 }
197
198 static int ready_residue(vorbis_enc_residue *rc, vorbis_enc_context *venc)
199 {
200 int i;
201 assert(rc->type == 2);
202 rc->maxes = av_mallocz(sizeof(float[2]) * rc->classifications);
203 if (!rc->maxes)
204 return AVERROR(ENOMEM);
205 for (i = 0; i < rc->classifications; i++) {
206 int j;
207 vorbis_enc_codebook * cb;
208 for (j = 0; j < 8; j++)
209 if (rc->books[i][j] != -1)
210 break;
211 if (j == 8) // zero
212 continue;
213 cb = &venc->codebooks[rc->books[i][j]];
214 assert(cb->ndimensions >= 2);
215 assert(cb->lookup);
216
217 for (j = 0; j < cb->nentries; j++) {
218 float a;
219 if (!cb->lens[j])
220 continue;
221 a = fabs(cb->dimensions[j * cb->ndimensions]);
222 if (a > rc->maxes[i][0])
223 rc->maxes[i][0] = a;
224 a = fabs(cb->dimensions[j * cb->ndimensions + 1]);
225 if (a > rc->maxes[i][1])
226 rc->maxes[i][1] = a;
227 }
228 }
229 // small bias
230 for (i = 0; i < rc->classifications; i++) {
231 rc->maxes[i][0] += 0.8;
232 rc->maxes[i][1] += 0.8;
233 }
234 return 0;
235 }
236
237 static int create_vorbis_context(vorbis_enc_context *venc,
238 AVCodecContext *avctx)
239 {
240 vorbis_enc_floor *fc;
241 vorbis_enc_residue *rc;
242 vorbis_enc_mapping *mc;
243 int i, book, ret;
244
245 venc->channels = avctx->channels;
246 venc->sample_rate = avctx->sample_rate;
247 venc->log2_blocksize[0] = venc->log2_blocksize[1] = 11;
248
249 venc->ncodebooks = FF_ARRAY_ELEMS(cvectors);
250 venc->codebooks = av_malloc(sizeof(vorbis_enc_codebook) * venc->ncodebooks);
251 if (!venc->codebooks)
252 return AVERROR(ENOMEM);
253
254 // codebook 0..14 - floor1 book, values 0..255
255 // codebook 15 residue masterbook
256 // codebook 16..29 residue
257 for (book = 0; book < venc->ncodebooks; book++) {
258 vorbis_enc_codebook *cb = &venc->codebooks[book];
259 int vals;
260 cb->ndimensions = cvectors[book].dim;
261 cb->nentries = cvectors[book].real_len;
262 cb->min = cvectors[book].min;
263 cb->delta = cvectors[book].delta;
264 cb->lookup = cvectors[book].lookup;
265 cb->seq_p = 0;
266
267 cb->lens = av_malloc(sizeof(uint8_t) * cb->nentries);
268 cb->codewords = av_malloc(sizeof(uint32_t) * cb->nentries);
269 if (!cb->lens || !cb->codewords)
270 return AVERROR(ENOMEM);
271 memcpy(cb->lens, cvectors[book].clens, cvectors[book].len);
272 memset(cb->lens + cvectors[book].len, 0, cb->nentries - cvectors[book].len);
273
274 if (cb->lookup) {
275 vals = cb_lookup_vals(cb->lookup, cb->ndimensions, cb->nentries);
276 cb->quantlist = av_malloc(sizeof(int) * vals);
277 if (!cb->quantlist)
278 return AVERROR(ENOMEM);
279 for (i = 0; i < vals; i++)
280 cb->quantlist[i] = cvectors[book].quant[i];
281 } else {
282 cb->quantlist = NULL;
283 }
284 if ((ret = ready_codebook(cb)) < 0)
285 return ret;
286 }
287
288 venc->nfloors = 1;
289 venc->floors = av_malloc(sizeof(vorbis_enc_floor) * venc->nfloors);
290 if (!venc->floors)
291 return AVERROR(ENOMEM);
292
293 // just 1 floor
294 fc = &venc->floors[0];
295 fc->partitions = NUM_FLOOR_PARTITIONS;
296 fc->partition_to_class = av_malloc(sizeof(int) * fc->partitions);
297 if (!fc->partition_to_class)
298 return AVERROR(ENOMEM);
299 fc->nclasses = 0;
300 for (i = 0; i < fc->partitions; i++) {
301 static const int a[] = {0, 1, 2, 2, 3, 3, 4, 4};
302 fc->partition_to_class[i] = a[i];
303 fc->nclasses = FFMAX(fc->nclasses, fc->partition_to_class[i]);
304 }
305 fc->nclasses++;
306 fc->classes = av_malloc(sizeof(vorbis_enc_floor_class) * fc->nclasses);
307 if (!fc->classes)
308 return AVERROR(ENOMEM);
309 for (i = 0; i < fc->nclasses; i++) {
310 vorbis_enc_floor_class * c = &fc->classes[i];
311 int j, books;
312 c->dim = floor_classes[i].dim;
313 c->subclass = floor_classes[i].subclass;
314 c->masterbook = floor_classes[i].masterbook;
315 books = (1 << c->subclass);
316 c->books = av_malloc(sizeof(int) * books);
317 if (!c->books)
318 return AVERROR(ENOMEM);
319 for (j = 0; j < books; j++)
320 c->books[j] = floor_classes[i].nbooks[j];
321 }
322 fc->multiplier = 2;
323 fc->rangebits = venc->log2_blocksize[0] - 1;
324
325 fc->values = 2;
326 for (i = 0; i < fc->partitions; i++)
327 fc->values += fc->classes[fc->partition_to_class[i]].dim;
328
329 fc->list = av_malloc(sizeof(vorbis_floor1_entry) * fc->values);
330 if (!fc->list)
331 return AVERROR(ENOMEM);
332 fc->list[0].x = 0;
333 fc->list[1].x = 1 << fc->rangebits;
334 for (i = 2; i < fc->values; i++) {
335 static const int a[] = {
336 93, 23,372, 6, 46,186,750, 14, 33, 65,
337 130,260,556, 3, 10, 18, 28, 39, 55, 79,
338 111,158,220,312,464,650,850
339 };
340 fc->list[i].x = a[i - 2];
341 }
342 if (ff_vorbis_ready_floor1_list(avctx, fc->list, fc->values))
343 return AVERROR_BUG;
344
345 venc->nresidues = 1;
346 venc->residues = av_malloc(sizeof(vorbis_enc_residue) * venc->nresidues);
347 if (!venc->residues)
348 return AVERROR(ENOMEM);
349
350 // single residue
351 rc = &venc->residues[0];
352 rc->type = 2;
353 rc->begin = 0;
354 rc->end = 1600;
355 rc->partition_size = 32;
356 rc->classifications = 10;
357 rc->classbook = 15;
358 rc->books = av_malloc(sizeof(*rc->books) * rc->classifications);
359 if (!rc->books)
360 return AVERROR(ENOMEM);
361 {
362 static const int8_t a[10][8] = {
363 { -1, -1, -1, -1, -1, -1, -1, -1, },
364 { -1, -1, 16, -1, -1, -1, -1, -1, },
365 { -1, -1, 17, -1, -1, -1, -1, -1, },
366 { -1, -1, 18, -1, -1, -1, -1, -1, },
367 { -1, -1, 19, -1, -1, -1, -1, -1, },
368 { -1, -1, 20, -1, -1, -1, -1, -1, },
369 { -1, -1, 21, -1, -1, -1, -1, -1, },
370 { 22, 23, -1, -1, -1, -1, -1, -1, },
371 { 24, 25, -1, -1, -1, -1, -1, -1, },
372 { 26, 27, 28, -1, -1, -1, -1, -1, },
373 };
374 memcpy(rc->books, a, sizeof a);
375 }
376 if ((ret = ready_residue(rc, venc)) < 0)
377 return ret;
378
379 venc->nmappings = 1;
380 venc->mappings = av_malloc(sizeof(vorbis_enc_mapping) * venc->nmappings);
381 if (!venc->mappings)
382 return AVERROR(ENOMEM);
383
384 // single mapping
385 mc = &venc->mappings[0];
386 mc->submaps = 1;
387 mc->mux = av_malloc(sizeof(int) * venc->channels);
388 if (!mc->mux)
389 return AVERROR(ENOMEM);
390 for (i = 0; i < venc->channels; i++)
391 mc->mux[i] = 0;
392 mc->floor = av_malloc(sizeof(int) * mc->submaps);
393 mc->residue = av_malloc(sizeof(int) * mc->submaps);
394 if (!mc->floor || !mc->residue)
395 return AVERROR(ENOMEM);
396 for (i = 0; i < mc->submaps; i++) {
397 mc->floor[i] = 0;
398 mc->residue[i] = 0;
399 }
400 mc->coupling_steps = venc->channels == 2 ? 1 : 0;
401 mc->magnitude = av_malloc(sizeof(int) * mc->coupling_steps);
402 mc->angle = av_malloc(sizeof(int) * mc->coupling_steps);
403 if (!mc->magnitude || !mc->angle)
404 return AVERROR(ENOMEM);
405 if (mc->coupling_steps) {
406 mc->magnitude[0] = 0;
407 mc->angle[0] = 1;
408 }
409
410 venc->nmodes = 1;
411 venc->modes = av_malloc(sizeof(vorbis_enc_mode) * venc->nmodes);
412 if (!venc->modes)
413 return AVERROR(ENOMEM);
414
415 // single mode
416 venc->modes[0].blockflag = 0;
417 venc->modes[0].mapping = 0;
418
419 venc->have_saved = 0;
420 venc->saved = av_malloc(sizeof(float) * venc->channels * (1 << venc->log2_blocksize[1]) / 2);
421 venc->samples = av_malloc(sizeof(float) * venc->channels * (1 << venc->log2_blocksize[1]));
422 venc->floor = av_malloc(sizeof(float) * venc->channels * (1 << venc->log2_blocksize[1]) / 2);
423 venc->coeffs = av_malloc(sizeof(float) * venc->channels * (1 << venc->log2_blocksize[1]) / 2);
424 if (!venc->saved || !venc->samples || !venc->floor || !venc->coeffs)
425 return AVERROR(ENOMEM);
426
427 venc->win[0] = ff_vorbis_vwin[venc->log2_blocksize[0] - 6];
428 venc->win[1] = ff_vorbis_vwin[venc->log2_blocksize[1] - 6];
429
430 if ((ret = ff_mdct_init(&venc->mdct[0], venc->log2_blocksize[0], 0, 1.0)) < 0)
431 return ret;
432 if ((ret = ff_mdct_init(&venc->mdct[1], venc->log2_blocksize[1], 0, 1.0)) < 0)
433 return ret;
434
435 return 0;
436 }
437
438 static void put_float(PutBitContext *pb, float f)
439 {
440 int exp, mant;
441 uint32_t res = 0;
442 mant = (int)ldexp(frexp(f, &exp), 20);
443 exp += 788 - 20;
444 if (mant < 0) {
445 res |= (1U << 31);
446 mant = -mant;
447 }
448 res |= mant | (exp << 21);
449 put_bits32(pb, res);
450 }
451
452 static void put_codebook_header(PutBitContext *pb, vorbis_enc_codebook *cb)
453 {
454 int i;
455 int ordered = 0;
456
457 put_bits(pb, 24, 0x564342); //magic
458 put_bits(pb, 16, cb->ndimensions);
459 put_bits(pb, 24, cb->nentries);
460
461 for (i = 1; i < cb->nentries; i++)
462 if (cb->lens[i] < cb->lens[i-1])
463 break;
464 if (i == cb->nentries)
465 ordered = 1;
466
467 put_bits(pb, 1, ordered);
468 if (ordered) {
469 int len = cb->lens[0];
470 put_bits(pb, 5, len - 1);
471 i = 0;
472 while (i < cb->nentries) {
473 int j;
474 for (j = 0; j+i < cb->nentries; j++)
475 if (cb->lens[j+i] != len)
476 break;
477 put_bits(pb, ilog(cb->nentries - i), j);
478 i += j;
479 len++;
480 }
481 } else {
482 int sparse = 0;
483 for (i = 0; i < cb->nentries; i++)
484 if (!cb->lens[i])
485 break;
486 if (i != cb->nentries)
487 sparse = 1;
488 put_bits(pb, 1, sparse);
489
490 for (i = 0; i < cb->nentries; i++) {
491 if (sparse)
492 put_bits(pb, 1, !!cb->lens[i]);
493 if (cb->lens[i])
494 put_bits(pb, 5, cb->lens[i] - 1);
495 }
496 }
497
498 put_bits(pb, 4, cb->lookup);
499 if (cb->lookup) {
500 int tmp = cb_lookup_vals(cb->lookup, cb->ndimensions, cb->nentries);
501 int bits = ilog(cb->quantlist[0]);
502
503 for (i = 1; i < tmp; i++)
504 bits = FFMAX(bits, ilog(cb->quantlist[i]));
505
506 put_float(pb, cb->min);
507 put_float(pb, cb->delta);
508
509 put_bits(pb, 4, bits - 1);
510 put_bits(pb, 1, cb->seq_p);
511
512 for (i = 0; i < tmp; i++)
513 put_bits(pb, bits, cb->quantlist[i]);
514 }
515 }
516
517 static void put_floor_header(PutBitContext *pb, vorbis_enc_floor *fc)
518 {
519 int i;
520
521 put_bits(pb, 16, 1); // type, only floor1 is supported
522
523 put_bits(pb, 5, fc->partitions);
524
525 for (i = 0; i < fc->partitions; i++)
526 put_bits(pb, 4, fc->partition_to_class[i]);
527
528 for (i = 0; i < fc->nclasses; i++) {
529 int j, books;
530
531 put_bits(pb, 3, fc->classes[i].dim - 1);
532 put_bits(pb, 2, fc->classes[i].subclass);
533
534 if (fc->classes[i].subclass)
535 put_bits(pb, 8, fc->classes[i].masterbook);
536
537 books = (1 << fc->classes[i].subclass);
538
539 for (j = 0; j < books; j++)
540 put_bits(pb, 8, fc->classes[i].books[j] + 1);
541 }
542
543 put_bits(pb, 2, fc->multiplier - 1);
544 put_bits(pb, 4, fc->rangebits);
545
546 for (i = 2; i < fc->values; i++)
547 put_bits(pb, fc->rangebits, fc->list[i].x);
548 }
549
550 static void put_residue_header(PutBitContext *pb, vorbis_enc_residue *rc)
551 {
552 int i;
553
554 put_bits(pb, 16, rc->type);
555
556 put_bits(pb, 24, rc->begin);
557 put_bits(pb, 24, rc->end);
558 put_bits(pb, 24, rc->partition_size - 1);
559 put_bits(pb, 6, rc->classifications - 1);
560 put_bits(pb, 8, rc->classbook);
561
562 for (i = 0; i < rc->classifications; i++) {
563 int j, tmp = 0;
564 for (j = 0; j < 8; j++)
565 tmp |= (rc->books[i][j] != -1) << j;
566
567 put_bits(pb, 3, tmp & 7);
568 put_bits(pb, 1, tmp > 7);
569
570 if (tmp > 7)
571 put_bits(pb, 5, tmp >> 3);
572 }
573
574 for (i = 0; i < rc->classifications; i++) {
575 int j;
576 for (j = 0; j < 8; j++)
577 if (rc->books[i][j] != -1)
578 put_bits(pb, 8, rc->books[i][j]);
579 }
580 }
581
582 static int put_main_header(vorbis_enc_context *venc, uint8_t **out)
583 {
584 int i;
585 PutBitContext pb;
586 uint8_t buffer[50000] = {0}, *p = buffer;
587 int buffer_len = sizeof buffer;
588 int len, hlens[3];
589
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++)
594 put_bits(&pb, 8, "vorbis"[i]);
595 put_bits32(&pb, 0); // version
596 put_bits(&pb, 8, venc->channels);
597 put_bits32(&pb, venc->sample_rate);
598 put_bits32(&pb, 0); // bitrate
599 put_bits32(&pb, 0); // bitrate
600 put_bits32(&pb, 0); // bitrate
601 put_bits(&pb, 4, venc->log2_blocksize[0]);
602 put_bits(&pb, 4, venc->log2_blocksize[1]);
603 put_bits(&pb, 1, 1); // framing
604
605 flush_put_bits(&pb);
606 hlens[0] = put_bits_count(&pb) >> 3;
607 buffer_len -= hlens[0];
608 p += hlens[0];
609
610 // comment header
611 init_put_bits(&pb, p, buffer_len);
612 put_bits(&pb, 8, 3); //magic
613 for (i = 0; "vorbis"[i]; i++)
614 put_bits(&pb, 8, "vorbis"[i]);
615 put_bits32(&pb, 0); // vendor length TODO
616 put_bits32(&pb, 0); // amount of comments
617 put_bits(&pb, 1, 1); // framing
618
619 flush_put_bits(&pb);
620 hlens[1] = put_bits_count(&pb) >> 3;
621 buffer_len -= hlens[1];
622 p += hlens[1];
623
624 // setup header
625 init_put_bits(&pb, p, buffer_len);
626 put_bits(&pb, 8, 5); //magic
627 for (i = 0; "vorbis"[i]; i++)
628 put_bits(&pb, 8, "vorbis"[i]);
629
630 // codebooks
631 put_bits(&pb, 8, venc->ncodebooks - 1);
632 for (i = 0; i < venc->ncodebooks; i++)
633 put_codebook_header(&pb, &venc->codebooks[i]);
634
635 // time domain, reserved, zero
636 put_bits(&pb, 6, 0);
637 put_bits(&pb, 16, 0);
638
639 // floors
640 put_bits(&pb, 6, venc->nfloors - 1);
641 for (i = 0; i < venc->nfloors; i++)
642 put_floor_header(&pb, &venc->floors[i]);
643
644 // residues
645 put_bits(&pb, 6, venc->nresidues - 1);
646 for (i = 0; i < venc->nresidues; i++)
647 put_residue_header(&pb, &venc->residues[i]);
648
649 // mappings
650 put_bits(&pb, 6, venc->nmappings - 1);
651 for (i = 0; i < venc->nmappings; i++) {
652 vorbis_enc_mapping *mc = &venc->mappings[i];
653 int j;
654 put_bits(&pb, 16, 0); // mapping type
655
656 put_bits(&pb, 1, mc->submaps > 1);
657 if (mc->submaps > 1)
658 put_bits(&pb, 4, mc->submaps - 1);
659
660 put_bits(&pb, 1, !!mc->coupling_steps);
661 if (mc->coupling_steps) {
662 put_bits(&pb, 8, mc->coupling_steps - 1);
663 for (j = 0; j < mc->coupling_steps; j++) {
664 put_bits(&pb, ilog(venc->channels - 1), mc->magnitude[j]);
665 put_bits(&pb, ilog(venc->channels - 1), mc->angle[j]);
666 }
667 }
668
669 put_bits(&pb, 2, 0); // reserved
670
671 if (mc->submaps > 1)
672 for (j = 0; j < venc->channels; j++)
673 put_bits(&pb, 4, mc->mux[j]);
674
675 for (j = 0; j < mc->submaps; j++) {
676 put_bits(&pb, 8, 0); // reserved time configuration
677 put_bits(&pb, 8, mc->floor[j]);
678 put_bits(&pb, 8, mc->residue[j]);
679 }
680 }
681
682 // modes
683 put_bits(&pb, 6, venc->nmodes - 1);
684 for (i = 0; i < venc->nmodes; i++) {
685 put_bits(&pb, 1, venc->modes[i].blockflag);
686 put_bits(&pb, 16, 0); // reserved window type
687 put_bits(&pb, 16, 0); // reserved transform type
688 put_bits(&pb, 8, venc->modes[i].mapping);
689 }
690
691 put_bits(&pb, 1, 1); // framing
692
693 flush_put_bits(&pb);
694 hlens[2] = put_bits_count(&pb) >> 3;
695
696 len = hlens[0] + hlens[1] + hlens[2];
697 p = *out = av_mallocz(64 + len + len/255);
698 if (!p)
699 return AVERROR(ENOMEM);
700
701 *p++ = 2;
702 p += av_xiphlacing(p, hlens[0]);
703 p += av_xiphlacing(p, hlens[1]);
704 buffer_len = 0;
705 for (i = 0; i < 3; i++) {
706 memcpy(p, buffer + buffer_len, hlens[i]);
707 p += hlens[i];
708 buffer_len += hlens[i];
709 }
710
711 return p - *out;
712 }
713
714 static float get_floor_average(vorbis_enc_floor * fc, float *coeffs, int i)
715 {
716 int begin = fc->list[fc->list[FFMAX(i-1, 0)].sort].x;
717 int end = fc->list[fc->list[FFMIN(i+1, fc->values - 1)].sort].x;
718 int j;
719 float average = 0;
720
721 for (j = begin; j < end; j++)
722 average += fabs(coeffs[j]);
723 return average / (end - begin);
724 }
725
726 static void floor_fit(vorbis_enc_context *venc, vorbis_enc_floor *fc,
727 float *coeffs, uint16_t *posts, int samples)
728 {
729 int range = 255 / fc->multiplier + 1;
730 int i;
731 float tot_average = 0.0;
732 float averages[MAX_FLOOR_VALUES];
733 for (i = 0; i < fc->values; i++) {
734 averages[i] = get_floor_average(fc, coeffs, i);
735 tot_average += averages[i];
736 }
737 tot_average /= fc->values;
738 tot_average /= venc->quality;
739
740 for (i = 0; i < fc->values; i++) {
741 int position = fc->list[fc->list[i].sort].x;
742 float average = averages[i];
743 int j;
744
745 average = sqrt(tot_average * average) * pow(1.25f, position*0.005f); // MAGIC!
746 for (j = 0; j < range - 1; j++)
747 if (ff_vorbis_floor1_inverse_db_table[j * fc->multiplier] > average)
748 break;
749 posts[fc->list[i].sort] = j;
750 }
751 }
752
753 static int render_point(int x0, int y0, int x1, int y1, int x)
754 {
755 return y0 + (x - x0) * (y1 - y0) / (x1 - x0);
756 }
757
758 static int floor_encode(vorbis_enc_context *venc, vorbis_enc_floor *fc,
759 PutBitContext *pb, uint16_t *posts,
760 float *floor, int samples)
761 {
762 int range = 255 / fc->multiplier + 1;
763 int coded[MAX_FLOOR_VALUES]; // first 2 values are unused
764 int i, counter;
765
766 if (pb->size_in_bits - put_bits_count(pb) < 1 + 2 * ilog(range - 1))
767 return AVERROR(EINVAL);
768 put_bits(pb, 1, 1); // non zero
769 put_bits(pb, ilog(range - 1), posts[0]);
770 put_bits(pb, ilog(range - 1), posts[1]);
771 coded[0] = coded[1] = 1;
772
773 for (i = 2; i < fc->values; i++) {
774 int predicted = render_point(fc->list[fc->list[i].low].x,
775 posts[fc->list[i].low],
776 fc->list[fc->list[i].high].x,
777 posts[fc->list[i].high],
778 fc->list[i].x);
779 int highroom = range - predicted;
780 int lowroom = predicted;
781 int room = FFMIN(highroom, lowroom);
782 if (predicted == posts[i]) {
783 coded[i] = 0; // must be used later as flag!
784 continue;
785 } else {
786 if (!coded[fc->list[i].low ])
787 coded[fc->list[i].low ] = -1;
788 if (!coded[fc->list[i].high])
789 coded[fc->list[i].high] = -1;
790 }
791 if (posts[i] > predicted) {
792 if (posts[i] - predicted > room)
793 coded[i] = posts[i] - predicted + lowroom;
794 else
795 coded[i] = (posts[i] - predicted) << 1;
796 } else {
797 if (predicted - posts[i] > room)
798 coded[i] = predicted - posts[i] + highroom - 1;
799 else
800 coded[i] = ((predicted - posts[i]) << 1) - 1;
801 }
802 }
803
804 counter = 2;
805 for (i = 0; i < fc->partitions; i++) {
806 vorbis_enc_floor_class * c = &fc->classes[fc->partition_to_class[i]];
807 int k, cval = 0, csub = 1<<c->subclass;
808 if (c->subclass) {
809 vorbis_enc_codebook * book = &venc->codebooks[c->masterbook];
810 int cshift = 0;
811 for (k = 0; k < c->dim; k++) {
812 int l;
813 for (l = 0; l < csub; l++) {
814 int maxval = 1;
815 if (c->books[l] != -1)
816 maxval = venc->codebooks[c->books[l]].nentries;
817 // coded could be -1, but this still works, cause that is 0
818 if (coded[counter + k] < maxval)
819 break;
820 }
821 assert(l != csub);
822 cval |= l << cshift;
823 cshift += c->subclass;
824 }
825 if (put_codeword(pb, book, cval))
826 return AVERROR(EINVAL);
827 }
828 for (k = 0; k < c->dim; k++) {
829 int book = c->books[cval & (csub-1)];
830 int entry = coded[counter++];
831 cval >>= c->subclass;
832 if (book == -1)
833 continue;
834 if (entry == -1)
835 entry = 0;
836 if (put_codeword(pb, &venc->codebooks[book], entry))
837 return AVERROR(EINVAL);
838 }
839 }
840
841 ff_vorbis_floor1_render_list(fc->list, fc->values, posts, coded,
842 fc->multiplier, floor, samples);
843
844 return 0;
845 }
846
847 static float *put_vector(vorbis_enc_codebook *book, PutBitContext *pb,
848 float *num)
849 {
850 int i, entry = -1;
851 float distance = FLT_MAX;
852 assert(book->dimensions);
853 for (i = 0; i < book->nentries; i++) {
854 float * vec = book->dimensions + i * book->ndimensions, d = book->pow2[i];
855 int j;
856 if (!book->lens[i])
857 continue;
858 for (j = 0; j < book->ndimensions; j++)
859 d -= vec[j] * num[j];
860 if (distance > d) {
861 entry = i;
862 distance = d;
863 }
864 }
865 if (put_codeword(pb, book, entry))
866 return NULL;
867 return &book->dimensions[entry * book->ndimensions];
868 }
869
870 static int residue_encode(vorbis_enc_context *venc, vorbis_enc_residue *rc,
871 PutBitContext *pb, float *coeffs, int samples,
872 int real_ch)
873 {
874 int pass, i, j, p, k;
875 int psize = rc->partition_size;
876 int partitions = (rc->end - rc->begin) / psize;
877 int channels = (rc->type == 2) ? 1 : real_ch;
878 int classes[MAX_CHANNELS][NUM_RESIDUE_PARTITIONS];
879 int classwords = venc->codebooks[rc->classbook].ndimensions;
880
881 assert(rc->type == 2);
882 assert(real_ch == 2);
883 for (p = 0; p < partitions; p++) {
884 float max1 = 0.0, max2 = 0.0;
885 int s = rc->begin + p * psize;
886 for (k = s; k < s + psize; k += 2) {
887 max1 = FFMAX(max1, fabs(coeffs[ k / real_ch]));
888 max2 = FFMAX(max2, fabs(coeffs[samples + k / real_ch]));
889 }
890
891 for (i = 0; i < rc->classifications - 1; i++)
892 if (max1 < rc->maxes[i][0] && max2 < rc->maxes[i][1])
893 break;
894 classes[0][p] = i;
895 }
896
897 for (pass = 0; pass < 8; pass++) {
898 p = 0;
899 while (p < partitions) {
900 if (pass == 0)
901 for (j = 0; j < channels; j++) {
902 vorbis_enc_codebook * book = &venc->codebooks[rc->classbook];
903 int entry = 0;
904 for (i = 0; i < classwords; i++) {
905 entry *= rc->classifications;
906 entry += classes[j][p + i];
907 }
908 if (put_codeword(pb, book, entry))
909 return AVERROR(EINVAL);
910 }
911 for (i = 0; i < classwords && p < partitions; i++, p++) {
912 for (j = 0; j < channels; j++) {
913 int nbook = rc->books[classes[j][p]][pass];
914 vorbis_enc_codebook * book = &venc->codebooks[nbook];
915 float *buf = coeffs + samples*j + rc->begin + p*psize;
916 if (nbook == -1)
917 continue;
918
919 assert(rc->type == 0 || rc->type == 2);
920 assert(!(psize % book->ndimensions));
921
922 if (rc->type == 0) {
923 for (k = 0; k < psize; k += book->ndimensions) {
924 int l;
925 float *a = put_vector(book, pb, &buf[k]);
926 if (!a)
927 return AVERROR(EINVAL);
928 for (l = 0; l < book->ndimensions; l++)
929 buf[k + l] -= a[l];
930 }
931 } else {
932 int s = rc->begin + p * psize, a1, b1;
933 a1 = (s % real_ch) * samples;
934 b1 = s / real_ch;
935 s = real_ch * samples;
936 for (k = 0; k < psize; k += book->ndimensions) {
937 int dim, a2 = a1, b2 = b1;
938 float vec[MAX_CODEBOOK_DIM], *pv = vec;
939 for (dim = book->ndimensions; dim--; ) {
940 *pv++ = coeffs[a2 + b2];
941 if ((a2 += samples) == s) {
942 a2 = 0;
943 b2++;
944 }
945 }
946 pv = put_vector(book, pb, vec);
947 if (!pv)
948 return AVERROR(EINVAL);
949 for (dim = book->ndimensions; dim--; ) {
950 coeffs[a1 + b1] -= *pv++;
951 if ((a1 += samples) == s) {
952 a1 = 0;
953 b1++;
954 }
955 }
956 }
957 }
958 }
959 }
960 }
961 }
962 return 0;
963 }
964
965 static int apply_window_and_mdct(vorbis_enc_context *venc,
966 float **audio, int samples)
967 {
968 int i, channel;
969 const float * win = venc->win[0];
970 int window_len = 1 << (venc->log2_blocksize[0] - 1);
971 float n = (float)(1 << venc->log2_blocksize[0]) / 4.0;
972 // FIXME use dsp
973
974 if (!venc->have_saved && !samples)
975 return 0;
976
977 if (venc->have_saved) {
978 for (channel = 0; channel < venc->channels; channel++)
979 memcpy(venc->samples + channel * window_len * 2,
980 venc->saved + channel * window_len, sizeof(float) * window_len);
981 } else {
982 for (channel = 0; channel < venc->channels; channel++)
983 memset(venc->samples + channel * window_len * 2, 0,
984 sizeof(float) * window_len);
985 }
986
987 if (samples) {
988 for (channel = 0; channel < venc->channels; channel++) {
989 float * offset = venc->samples + channel*window_len*2 + window_len;
990 for (i = 0; i < samples; i++)
991 offset[i] = audio[channel][i] / n * win[window_len - i - 1];
992 }
993 } else {
994 for (channel = 0; channel < venc->channels; channel++)
995 memset(venc->samples + channel * window_len * 2 + window_len,
996 0, sizeof(float) * window_len);
997 }
998
999 for (channel = 0; channel < venc->channels; channel++)
1000 venc->mdct[0].mdct_calc(&venc->mdct[0], venc->coeffs + channel * window_len,
1001 venc->samples + channel * window_len * 2);
1002
1003 if (samples) {
1004 for (channel = 0; channel < venc->channels; channel++) {
1005 float *offset = venc->saved + channel * window_len;
1006 for (i = 0; i < samples; i++)
1007 offset[i] = audio[channel][i] / n * win[i];
1008 }
1009 venc->have_saved = 1;
1010 } else {
1011 venc->have_saved = 0;
1012 }
1013 return 1;
1014 }
1015
1016
1017 static int vorbis_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
1018 const AVFrame *frame, int *got_packet_ptr)
1019 {
1020 vorbis_enc_context *venc = avctx->priv_data;
1021 float **audio = frame ? (float **)frame->extended_data : NULL;
1022 int samples = frame ? frame->nb_samples : 0;
1023 vorbis_enc_mode *mode;
1024 vorbis_enc_mapping *mapping;
1025 PutBitContext pb;
1026 int i, ret;
1027
1028 if (!apply_window_and_mdct(venc, audio, samples))
1029 return 0;
1030 samples = 1 << (venc->log2_blocksize[0] - 1);
1031
1032 if ((ret = ff_alloc_packet(avpkt, 8192))) {
1033 av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
1034 return ret;
1035 }
1036
1037 init_put_bits(&pb, avpkt->data, avpkt->size);
1038
1039 if (pb.size_in_bits - put_bits_count(&pb) < 1 + ilog(venc->nmodes - 1)) {
1040 av_log(avctx, AV_LOG_ERROR, "output buffer is too small\n");
1041 return AVERROR(EINVAL);
1042 }
1043
1044 put_bits(&pb, 1, 0); // magic bit
1045
1046 put_bits(&pb, ilog(venc->nmodes - 1), 0); // 0 bits, the mode
1047
1048 mode = &venc->modes[0];
1049 mapping = &venc->mappings[mode->mapping];
1050 if (mode->blockflag) {
1051 put_bits(&pb, 1, 0);
1052 put_bits(&pb, 1, 0);
1053 }
1054
1055 for (i = 0; i < venc->channels; i++) {
1056 vorbis_enc_floor *fc = &venc->floors[mapping->floor[mapping->mux[i]]];
1057 uint16_t posts[MAX_FLOOR_VALUES];
1058 floor_fit(venc, fc, &venc->coeffs[i * samples], posts, samples);
1059 if (floor_encode(venc, fc, &pb, posts, &venc->floor[i * samples], samples)) {
1060 av_log(avctx, AV_LOG_ERROR, "output buffer is too small\n");
1061 return AVERROR(EINVAL);
1062 }
1063 }
1064
1065 for (i = 0; i < venc->channels * samples; i++)
1066 venc->coeffs[i] /= venc->floor[i];
1067
1068 for (i = 0; i < mapping->coupling_steps; i++) {
1069 float *mag = venc->coeffs + mapping->magnitude[i] * samples;
1070 float *ang = venc->coeffs + mapping->angle[i] * samples;
1071 int j;
1072 for (j = 0; j < samples; j++) {
1073 float a = ang[j];
1074 ang[j] -= mag[j];
1075 if (mag[j] > 0)
1076 ang[j] = -ang[j];
1077 if (ang[j] < 0)
1078 mag[j] = a;
1079 }
1080 }
1081
1082 if (residue_encode(venc, &venc->residues[mapping->residue[mapping->mux[0]]],
1083 &pb, venc->coeffs, samples, venc->channels)) {
1084 av_log(avctx, AV_LOG_ERROR, "output buffer is too small\n");
1085 return AVERROR(EINVAL);
1086 }
1087
1088 flush_put_bits(&pb);
1089 avpkt->size = put_bits_count(&pb) >> 3;
1090
1091 avpkt->duration = ff_samples_to_time_base(avctx, avctx->frame_size);
1092 if (frame)
1093 if (frame->pts != AV_NOPTS_VALUE)
1094 avpkt->pts = ff_samples_to_time_base(avctx, frame->pts);
1095 else
1096 avpkt->pts = venc->next_pts;
1097 if (avpkt->pts != AV_NOPTS_VALUE)
1098 venc->next_pts = avpkt->pts + avpkt->duration;
1099
1100 *got_packet_ptr = 1;
1101 return 0;
1102 }
1103
1104
1105 static av_cold int vorbis_encode_close(AVCodecContext *avctx)
1106 {
1107 vorbis_enc_context *venc = avctx->priv_data;
1108 int i;
1109
1110 if (venc->codebooks)
1111 for (i = 0; i < venc->ncodebooks; i++) {
1112 av_freep(&venc->codebooks[i].lens);
1113 av_freep(&venc->codebooks[i].codewords);
1114 av_freep(&venc->codebooks[i].quantlist);
1115 av_freep(&venc->codebooks[i].dimensions);
1116 av_freep(&venc->codebooks[i].pow2);
1117 }
1118 av_freep(&venc->codebooks);
1119
1120 if (venc->floors)
1121 for (i = 0; i < venc->nfloors; i++) {
1122 int j;
1123 if (venc->floors[i].classes)
1124 for (j = 0; j < venc->floors[i].nclasses; j++)
1125 av_freep(&venc->floors[i].classes[j].books);
1126 av_freep(&venc->floors[i].classes);
1127 av_freep(&venc->floors[i].partition_to_class);
1128 av_freep(&venc->floors[i].list);
1129 }
1130 av_freep(&venc->floors);
1131
1132 if (venc->residues)
1133 for (i = 0; i < venc->nresidues; i++) {
1134 av_freep(&venc->residues[i].books);
1135 av_freep(&venc->residues[i].maxes);
1136 }
1137 av_freep(&venc->residues);
1138
1139 if (venc->mappings)
1140 for (i = 0; i < venc->nmappings; i++) {
1141 av_freep(&venc->mappings[i].mux);
1142 av_freep(&venc->mappings[i].floor);
1143 av_freep(&venc->mappings[i].residue);
1144 av_freep(&venc->mappings[i].magnitude);
1145 av_freep(&venc->mappings[i].angle);
1146 }
1147 av_freep(&venc->mappings);
1148
1149 av_freep(&venc->modes);
1150
1151 av_freep(&venc->saved);
1152 av_freep(&venc->samples);
1153 av_freep(&venc->floor);
1154 av_freep(&venc->coeffs);
1155
1156 ff_mdct_end(&venc->mdct[0]);
1157 ff_mdct_end(&venc->mdct[1]);
1158
1159 av_freep(&avctx->extradata);
1160
1161 return 0 ;
1162 }
1163
1164 static av_cold int vorbis_encode_init(AVCodecContext *avctx)
1165 {
1166 vorbis_enc_context *venc = avctx->priv_data;
1167 int ret;
1168
1169 if (avctx->channels != 2) {
1170 av_log(avctx, AV_LOG_ERROR, "Current Libav Vorbis encoder only supports 2 channels.\n");
1171 return -1;
1172 }
1173
1174 if ((ret = create_vorbis_context(venc, avctx)) < 0)
1175 goto error;
1176
1177 avctx->bit_rate = 0;
1178 if (avctx->flags & CODEC_FLAG_QSCALE)
1179 venc->quality = avctx->global_quality / (float)FF_QP2LAMBDA;
1180 else
1181 venc->quality = 3.0;
1182 venc->quality *= venc->quality;
1183
1184 if ((ret = put_main_header(venc, (uint8_t**)&avctx->extradata)) < 0)
1185 goto error;
1186 avctx->extradata_size = ret;
1187
1188 avctx->frame_size = 1 << (venc->log2_blocksize[0] - 1);
1189
1190 return 0;
1191 error:
1192 vorbis_encode_close(avctx);
1193 return ret;
1194 }
1195
1196 AVCodec ff_vorbis_encoder = {
1197 .name = "vorbis",
1198 .type = AVMEDIA_TYPE_AUDIO,
1199 .id = AV_CODEC_ID_VORBIS,
1200 .priv_data_size = sizeof(vorbis_enc_context),
1201 .init = vorbis_encode_init,
1202 .encode2 = vorbis_encode_frame,
1203 .close = vorbis_encode_close,
1204 .capabilities = CODEC_CAP_DELAY | CODEC_CAP_EXPERIMENTAL,
1205 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLTP,
1206 AV_SAMPLE_FMT_NONE },
1207 .long_name = NULL_IF_CONFIG_SMALL("Vorbis"),
1208 };