FFmpeg
Loading...
Searching...
No Matches
vorbisenc.c
Go to the documentation of this file.
1/*
2 * copyright (c) 2006 Oded Shimon <ods15@ods15.dyndns.org>
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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 "libavutil/float_dsp.h"
29#include "libavutil/mem.h"
30#include "libavutil/tx.h"
31
32#include "avcodec.h"
33#include "codec_internal.h"
34#include "encode.h"
35#include "mathops.h"
36#include "vorbis.h"
37#include "vorbis_data.h"
38#include "vorbis_enc_data.h"
39
40#include "audio_frame_queue.h"
42
43#define BITSTREAM_WRITER_LE
44#include "put_bits.h"
45
46#undef NDEBUG
47#include <assert.h>
48
49typedef struct vorbis_enc_codebook {
51 uint8_t *lens;
52 uint32_t *codewords;
54 float min;
55 float delta;
56 int seq_p;
57 int lookup;
59 float *dimensions;
60 float *pow2;
62
69
80
91
101
106
107typedef struct vorbis_enc_context {
113 const float *win[2];
115 float *saved;
116 float *samples;
117 float *floor; // also used for tmp values for mdct
118 float *coeffs; // also used for residue after floor
119 float *scratch; // used for tmp values for psy model
120 float quality;
121
124
127
130
133
136
139
141
144
145#define MAX_CHANNELS 2
146#define MAX_CODEBOOK_DIM 8
147
148#define MAX_FLOOR_CLASS_DIM 4
149#define NUM_FLOOR_PARTITIONS 8
150#define MAX_FLOOR_VALUES (MAX_FLOOR_CLASS_DIM*NUM_FLOOR_PARTITIONS+2)
151
152#define RESIDUE_SIZE 1600
153#define RESIDUE_PART_SIZE 32
154#define NUM_RESIDUE_PARTITIONS (RESIDUE_SIZE/RESIDUE_PART_SIZE)
155
157 int entry)
158{
159 av_assert2(entry >= 0);
160 av_assert2(entry < cb->nentries);
161 av_assert2(cb->lens[entry]);
162 if (put_bits_left(pb) < cb->lens[entry])
163 return AVERROR(EINVAL);
164 put_bits(pb, cb->lens[entry], cb->codewords[entry]);
165 return 0;
166}
167
168static int cb_lookup_vals(int lookup, int dimensions, int entries)
169{
170 if (lookup == 1)
171 return ff_vorbis_nth_root(entries, dimensions);
172 else if (lookup == 2)
173 return dimensions *entries;
174 return 0;
175}
176
178{
179 int i;
180
181 ff_vorbis_len2vlc(cb->lens, cb->codewords, cb->nentries);
182
183 if (!cb->lookup) {
184 cb->pow2 = cb->dimensions = NULL;
185 } else {
186 int vals = cb_lookup_vals(cb->lookup, cb->ndimensions, cb->nentries);
187 cb->dimensions = av_malloc_array(cb->nentries, sizeof(float) * cb->ndimensions);
188 cb->pow2 = av_calloc(cb->nentries, sizeof(*cb->pow2));
189 if (!cb->dimensions || !cb->pow2)
190 return AVERROR(ENOMEM);
191 for (i = 0; i < cb->nentries; i++) {
192 float last = 0;
193 int j;
194 int div = 1;
195 for (j = 0; j < cb->ndimensions; j++) {
196 int off;
197 if (cb->lookup == 1)
198 off = (i / div) % vals; // lookup type 1
199 else
200 off = i * cb->ndimensions + j; // lookup type 2
201
202 cb->dimensions[i * cb->ndimensions + j] = last + cb->min + cb->quantlist[off] * cb->delta;
203 if (cb->seq_p)
204 last = cb->dimensions[i * cb->ndimensions + j];
205 cb->pow2[i] += cb->dimensions[i * cb->ndimensions + j] * cb->dimensions[i * cb->ndimensions + j];
206 div *= vals;
207 }
208 cb->pow2[i] /= 2.0;
209 }
210 }
211 return 0;
212}
213
215{
216 int i;
217 av_assert0(rc->type == 2);
218 rc->maxes = av_calloc(rc->classifications, sizeof(*rc->maxes));
219 if (!rc->maxes)
220 return AVERROR(ENOMEM);
221 for (i = 0; i < rc->classifications; i++) {
222 int j;
224 for (j = 0; j < 8; j++)
225 if (rc->books[i][j] != -1)
226 break;
227 if (j == 8) // zero
228 continue;
229 cb = &venc->codebooks[rc->books[i][j]];
230 assert(cb->ndimensions >= 2);
231 assert(cb->lookup);
232
233 for (j = 0; j < cb->nentries; j++) {
234 float a;
235 if (!cb->lens[j])
236 continue;
237 a = fabs(cb->dimensions[j * cb->ndimensions]);
238 if (a > rc->maxes[i][0])
239 rc->maxes[i][0] = a;
240 a = fabs(cb->dimensions[j * cb->ndimensions + 1]);
241 if (a > rc->maxes[i][1])
242 rc->maxes[i][1] = a;
243 }
244 }
245 // small bias
246 for (i = 0; i < rc->classifications; i++) {
247 rc->maxes[i][0] += 0.8;
248 rc->maxes[i][1] += 0.8;
249 }
250 return 0;
251}
252
254{
255 int ret = 0;
256 float scale = 1.0f;
257
259 if (!venc->fdsp)
260 return AVERROR(ENOMEM);
261
262 // init windows
263 venc->win[0] = ff_vorbis_vwin[venc->log2_blocksize[0] - 6];
264 venc->win[1] = ff_vorbis_vwin[venc->log2_blocksize[1] - 6];
265
266 if ((ret = av_tx_init(&venc->mdct[0], &venc->mdct_fn[0], AV_TX_FLOAT_MDCT,
267 0, 1 << (venc->log2_blocksize[0] - 1), &scale, 0)) < 0)
268 return ret;
269 if ((ret = av_tx_init(&venc->mdct[1], &venc->mdct_fn[1], AV_TX_FLOAT_MDCT,
270 0, 1 << (venc->log2_blocksize[1] - 1), &scale, 0)) < 0)
271 return ret;
272
273 return 0;
274}
275
277 AVCodecContext *avctx)
278{
282 const uint8_t *clens, *quant;
283 int i, book, ret;
284
285 venc->channels = avctx->ch_layout.nb_channels;
286 venc->sample_rate = avctx->sample_rate;
287 venc->log2_blocksize[0] = venc->log2_blocksize[1] = 11;
288
290 venc->codebooks = av_mallocz(sizeof(vorbis_enc_codebook) * venc->ncodebooks);
291 if (!venc->codebooks)
292 return AVERROR(ENOMEM);
293
294 // codebook 0..14 - floor1 book, values 0..255
295 // codebook 15 residue masterbook
296 // codebook 16..29 residue
297 clens = codebooks;
299 for (book = 0; book < venc->ncodebooks; book++) {
300 vorbis_enc_codebook *cb = &venc->codebooks[book];
301 int vals;
302 cb->ndimensions = cvectors[book].dim;
303 cb->nentries = cvectors[book].real_len;
304 cb->min = cvectors[book].min;
305 cb->delta = cvectors[book].delta;
306 cb->lookup = cvectors[book].lookup;
307 cb->seq_p = 0;
308
309 cb->lens = av_malloc_array(cb->nentries, sizeof(uint8_t));
310 cb->codewords = av_malloc_array(cb->nentries, sizeof(uint32_t));
311 if (!cb->lens || !cb->codewords)
312 return AVERROR(ENOMEM);
313 memcpy(cb->lens, clens, cvectors[book].len);
314 memset(cb->lens + cvectors[book].len, 0, cb->nentries - cvectors[book].len);
315 clens += cvectors[book].len;
316
317 if (cb->lookup) {
318 vals = cb_lookup_vals(cb->lookup, cb->ndimensions, cb->nentries);
319 cb->quantlist = av_malloc_array(vals, sizeof(int));
320 if (!cb->quantlist)
321 return AVERROR(ENOMEM);
322 for (i = 0; i < vals; i++)
323 cb->quantlist[i] = *quant++;
324 } else {
325 cb->quantlist = NULL;
326 }
327 if ((ret = ready_codebook(cb)) < 0)
328 return ret;
329 }
330
331 venc->nfloors = 1;
332 venc->floors = av_mallocz(sizeof(vorbis_enc_floor) * venc->nfloors);
333 if (!venc->floors)
334 return AVERROR(ENOMEM);
335
336 // just 1 floor
337 fc = &venc->floors[0];
338 fc->partitions = NUM_FLOOR_PARTITIONS;
339 fc->partition_to_class = av_malloc(sizeof(int) * fc->partitions);
340 if (!fc->partition_to_class)
341 return AVERROR(ENOMEM);
342 fc->nclasses = 0;
343 for (i = 0; i < fc->partitions; i++) {
344 static const int a[] = {0, 1, 2, 2, 3, 3, 4, 4};
345 fc->partition_to_class[i] = a[i];
346 fc->nclasses = FFMAX(fc->nclasses, fc->partition_to_class[i]);
347 }
348 fc->nclasses++;
349 fc->classes = av_calloc(fc->nclasses, sizeof(vorbis_enc_floor_class));
350 if (!fc->classes)
351 return AVERROR(ENOMEM);
352 for (i = 0; i < fc->nclasses; i++) {
353 vorbis_enc_floor_class * c = &fc->classes[i];
354 int j, books;
355 c->dim = floor_classes[i].dim;
356 c->subclass = floor_classes[i].subclass;
357 c->masterbook = floor_classes[i].masterbook;
358 books = (1 << c->subclass);
359 c->books = av_malloc_array(books, sizeof(int));
360 if (!c->books)
361 return AVERROR(ENOMEM);
362 for (j = 0; j < books; j++)
363 c->books[j] = floor_classes[i].nbooks[j];
364 }
365 fc->multiplier = 2;
366 fc->rangebits = venc->log2_blocksize[1] - 1;
367
368 fc->values = 2;
369 for (i = 0; i < fc->partitions; i++)
370 fc->values += fc->classes[fc->partition_to_class[i]].dim;
371
372 fc->list = av_malloc_array(fc->values, sizeof(vorbis_floor1_entry));
373 if (!fc->list)
374 return AVERROR(ENOMEM);
375 fc->list[0].x = 0;
376 fc->list[1].x = 1 << fc->rangebits;
377 for (i = 2; i < fc->values; i++) {
378 static const int a[] = {
379 93, 23,372, 6, 46,186,750, 14, 33, 65,
380 130,260,556, 3, 10, 18, 28, 39, 55, 79,
381 111,158,220,312,464,650,850
382 };
383 fc->list[i].x = a[i - 2];
384 }
385 if (ff_vorbis_ready_floor1_list(avctx, fc->list, fc->values))
386 return AVERROR_BUG;
387
388 venc->nresidues = 1;
389 venc->residues = av_mallocz(sizeof(vorbis_enc_residue) * venc->nresidues);
390 if (!venc->residues)
391 return AVERROR(ENOMEM);
392
393 // single residue
394 rc = &venc->residues[0];
395 rc->type = 2;
396 rc->begin = 0;
397 rc->end = 1600;
398 rc->partition_size = 32;
399 rc->classifications = 10;
400 rc->classbook = 15;
401 rc->books = av_malloc(sizeof(*rc->books) * rc->classifications);
402 if (!rc->books)
403 return AVERROR(ENOMEM);
404 {
405 static const int8_t a[10][8] = {
406 { -1, -1, -1, -1, -1, -1, -1, -1, },
407 { -1, -1, 16, -1, -1, -1, -1, -1, },
408 { -1, -1, 17, -1, -1, -1, -1, -1, },
409 { -1, -1, 18, -1, -1, -1, -1, -1, },
410 { -1, -1, 19, -1, -1, -1, -1, -1, },
411 { -1, -1, 20, -1, -1, -1, -1, -1, },
412 { -1, -1, 21, -1, -1, -1, -1, -1, },
413 { 22, 23, -1, -1, -1, -1, -1, -1, },
414 { 24, 25, -1, -1, -1, -1, -1, -1, },
415 { 26, 27, 28, -1, -1, -1, -1, -1, },
416 };
417 memcpy(rc->books, a, sizeof a);
418 }
419 if ((ret = ready_residue(rc, venc)) < 0)
420 return ret;
421
422 venc->nmappings = 1;
423 venc->mappings = av_mallocz(sizeof(vorbis_enc_mapping) * venc->nmappings);
424 if (!venc->mappings)
425 return AVERROR(ENOMEM);
426
427 // single mapping
428 mc = &venc->mappings[0];
429 mc->submaps = 1;
430 mc->mux = av_malloc(sizeof(int) * venc->channels);
431 if (!mc->mux)
432 return AVERROR(ENOMEM);
433 for (i = 0; i < venc->channels; i++)
434 mc->mux[i] = 0;
435 mc->floor = av_malloc(sizeof(int) * mc->submaps);
436 mc->residue = av_malloc(sizeof(int) * mc->submaps);
437 if (!mc->floor || !mc->residue)
438 return AVERROR(ENOMEM);
439 for (i = 0; i < mc->submaps; i++) {
440 mc->floor[i] = 0;
441 mc->residue[i] = 0;
442 }
443 mc->coupling_steps = venc->channels == 2 ? 1 : 0;
444 mc->magnitude = av_malloc(sizeof(int) * mc->coupling_steps);
445 mc->angle = av_malloc(sizeof(int) * mc->coupling_steps);
446 if (!mc->magnitude || !mc->angle)
447 return AVERROR(ENOMEM);
448 if (mc->coupling_steps) {
449 mc->magnitude[0] = 0;
450 mc->angle[0] = 1;
451 }
452
453 venc->nmodes = 2;
454 venc->modes = av_malloc(sizeof(vorbis_enc_mode) * venc->nmodes);
455 if (!venc->modes)
456 return AVERROR(ENOMEM);
457
458 // Short block
459 venc->modes[0].blockflag = 0;
460 venc->modes[0].mapping = 0;
461 // Long block
462 venc->modes[1].blockflag = 1;
463 venc->modes[1].mapping = 0;
464
465 venc->have_saved = 0;
466 venc->saved = av_malloc_array((1 << venc->log2_blocksize[1]) / 2, sizeof(float) * venc->channels);
467 venc->samples = av_malloc_array((1 << venc->log2_blocksize[1]), sizeof(float) * venc->channels);
468 venc->floor = av_malloc_array((1 << venc->log2_blocksize[1]) / 2, sizeof(float) * venc->channels);
469 venc->coeffs = av_malloc_array((1 << venc->log2_blocksize[1]) / 2, sizeof(float) * venc->channels);
470 venc->scratch = av_malloc_array((1 << venc->log2_blocksize[1]), sizeof(float) * venc->channels);
471
472 if (!venc->saved || !venc->samples || !venc->floor || !venc->coeffs || !venc->scratch)
473 return AVERROR(ENOMEM);
474
475 if ((ret = dsp_init(avctx, venc)) < 0)
476 return ret;
477
478 return 0;
479}
480
481static void put_float(PutBitContext *pb, float f)
482{
483 int exp, mant;
484 uint32_t res = 0;
485 mant = (int)ldexp(frexp(f, &exp), 20);
486 exp += 788 - 20;
487 if (mant < 0) {
488 res |= (1U << 31);
489 mant = -mant;
490 }
491 res |= mant | (exp << 21);
492 put_bits32(pb, res);
493}
494
496{
497 int i;
498 int ordered = 0;
499
500 put_bits(pb, 24, 0x564342); //magic
501 put_bits(pb, 16, cb->ndimensions);
502 put_bits(pb, 24, cb->nentries);
503
504 for (i = 1; i < cb->nentries; i++)
505 if (cb->lens[i] < cb->lens[i-1])
506 break;
507 if (i == cb->nentries)
508 ordered = 1;
509
510 put_bits(pb, 1, ordered);
511 if (ordered) {
512 int len = cb->lens[0];
513 put_bits(pb, 5, len - 1);
514 i = 0;
515 while (i < cb->nentries) {
516 int j;
517 for (j = 0; j+i < cb->nentries; j++)
518 if (cb->lens[j+i] != len)
519 break;
520 put_bits(pb, ilog(cb->nentries - i), j);
521 i += j;
522 len++;
523 }
524 } else {
525 int sparse = 0;
526 for (i = 0; i < cb->nentries; i++)
527 if (!cb->lens[i])
528 break;
529 if (i != cb->nentries)
530 sparse = 1;
531 put_bits(pb, 1, sparse);
532
533 for (i = 0; i < cb->nentries; i++) {
534 if (sparse)
535 put_bits(pb, 1, !!cb->lens[i]);
536 if (cb->lens[i])
537 put_bits(pb, 5, cb->lens[i] - 1);
538 }
539 }
540
541 put_bits(pb, 4, cb->lookup);
542 if (cb->lookup) {
543 int tmp = cb_lookup_vals(cb->lookup, cb->ndimensions, cb->nentries);
544 int bits = ilog(cb->quantlist[0]);
545
546 for (i = 1; i < tmp; i++)
547 bits = FFMAX(bits, ilog(cb->quantlist[i]));
548
549 put_float(pb, cb->min);
550 put_float(pb, cb->delta);
551
552 put_bits(pb, 4, bits - 1);
553 put_bits(pb, 1, cb->seq_p);
554
555 for (i = 0; i < tmp; i++)
556 put_bits(pb, bits, cb->quantlist[i]);
557 }
558}
559
561{
562 int i;
563
564 put_bits(pb, 16, 1); // type, only floor1 is supported
565
566 put_bits(pb, 5, fc->partitions);
567
568 for (i = 0; i < fc->partitions; i++)
569 put_bits(pb, 4, fc->partition_to_class[i]);
570
571 for (i = 0; i < fc->nclasses; i++) {
572 int j, books;
573
574 put_bits(pb, 3, fc->classes[i].dim - 1);
575 put_bits(pb, 2, fc->classes[i].subclass);
576
577 if (fc->classes[i].subclass)
578 put_bits(pb, 8, fc->classes[i].masterbook);
579
580 books = (1 << fc->classes[i].subclass);
581
582 for (j = 0; j < books; j++)
583 put_bits(pb, 8, fc->classes[i].books[j] + 1);
584 }
585
586 put_bits(pb, 2, fc->multiplier - 1);
587 put_bits(pb, 4, fc->rangebits);
588
589 for (i = 2; i < fc->values; i++)
590 put_bits(pb, fc->rangebits, fc->list[i].x);
591}
592
594{
595 int i;
596
597 put_bits(pb, 16, rc->type);
598
599 put_bits(pb, 24, rc->begin);
600 put_bits(pb, 24, rc->end);
601 put_bits(pb, 24, rc->partition_size - 1);
602 put_bits(pb, 6, rc->classifications - 1);
603 put_bits(pb, 8, rc->classbook);
604
605 for (i = 0; i < rc->classifications; i++) {
606 int j, tmp = 0;
607 for (j = 0; j < 8; j++)
608 tmp |= (rc->books[i][j] != -1) << j;
609
610 put_bits(pb, 3, tmp & 7);
611 put_bits(pb, 1, tmp > 7);
612
613 if (tmp > 7)
614 put_bits(pb, 5, tmp >> 3);
615 }
616
617 for (i = 0; i < rc->classifications; i++) {
618 int j;
619 for (j = 0; j < 8; j++)
620 if (rc->books[i][j] != -1)
621 put_bits(pb, 8, rc->books[i][j]);
622 }
623}
624
625static int put_main_header(vorbis_enc_context *venc, uint8_t **out)
626{
627 int i;
628 PutBitContext pb;
629 int len, hlens[3];
630 int buffer_len = 50000;
631 uint8_t *buffer = av_mallocz(buffer_len), *p = buffer;
632 if (!buffer)
633 return AVERROR(ENOMEM);
634
635 // identification header
636 init_put_bits(&pb, p, buffer_len);
637 put_bits(&pb, 8, 1); //magic
638 for (i = 0; "vorbis"[i]; i++)
639 put_bits(&pb, 8, "vorbis"[i]);
640 put_bits32(&pb, 0); // version
641 put_bits(&pb, 8, venc->channels);
642 put_bits32(&pb, venc->sample_rate);
643 put_bits32(&pb, 0); // bitrate
644 put_bits32(&pb, 0); // bitrate
645 put_bits32(&pb, 0); // bitrate
646 put_bits(&pb, 4, venc->log2_blocksize[0]);
647 put_bits(&pb, 4, venc->log2_blocksize[1]);
648 put_bits(&pb, 1, 1); // framing
649
650 flush_put_bits(&pb);
651 hlens[0] = put_bytes_output(&pb);
652 buffer_len -= hlens[0];
653 p += hlens[0];
654
655 // comment header
656 init_put_bits(&pb, p, buffer_len);
657 put_bits(&pb, 8, 3); //magic
658 for (i = 0; "vorbis"[i]; i++)
659 put_bits(&pb, 8, "vorbis"[i]);
660 put_bits32(&pb, 0); // vendor length TODO
661 put_bits32(&pb, 0); // amount of comments
662 put_bits(&pb, 1, 1); // framing
663
664 flush_put_bits(&pb);
665 hlens[1] = put_bytes_output(&pb);
666 buffer_len -= hlens[1];
667 p += hlens[1];
668
669 // setup header
670 init_put_bits(&pb, p, buffer_len);
671 put_bits(&pb, 8, 5); //magic
672 for (i = 0; "vorbis"[i]; i++)
673 put_bits(&pb, 8, "vorbis"[i]);
674
675 // codebooks
676 put_bits(&pb, 8, venc->ncodebooks - 1);
677 for (i = 0; i < venc->ncodebooks; i++)
678 put_codebook_header(&pb, &venc->codebooks[i]);
679
680 // time domain, reserved, zero
681 put_bits(&pb, 6, 0);
682 put_bits(&pb, 16, 0);
683
684 // floors
685 put_bits(&pb, 6, venc->nfloors - 1);
686 for (i = 0; i < venc->nfloors; i++)
687 put_floor_header(&pb, &venc->floors[i]);
688
689 // residues
690 put_bits(&pb, 6, venc->nresidues - 1);
691 for (i = 0; i < venc->nresidues; i++)
692 put_residue_header(&pb, &venc->residues[i]);
693
694 // mappings
695 put_bits(&pb, 6, venc->nmappings - 1);
696 for (i = 0; i < venc->nmappings; i++) {
697 vorbis_enc_mapping *mc = &venc->mappings[i];
698 int j;
699 put_bits(&pb, 16, 0); // mapping type
700
701 put_bits(&pb, 1, mc->submaps > 1);
702 if (mc->submaps > 1)
703 put_bits(&pb, 4, mc->submaps - 1);
704
705 put_bits(&pb, 1, !!mc->coupling_steps);
706 if (mc->coupling_steps) {
707 put_bits(&pb, 8, mc->coupling_steps - 1);
708 for (j = 0; j < mc->coupling_steps; j++) {
709 put_bits(&pb, ilog(venc->channels - 1), mc->magnitude[j]);
710 put_bits(&pb, ilog(venc->channels - 1), mc->angle[j]);
711 }
712 }
713
714 put_bits(&pb, 2, 0); // reserved
715
716 if (mc->submaps > 1)
717 for (j = 0; j < venc->channels; j++)
718 put_bits(&pb, 4, mc->mux[j]);
719
720 for (j = 0; j < mc->submaps; j++) {
721 put_bits(&pb, 8, 0); // reserved time configuration
722 put_bits(&pb, 8, mc->floor[j]);
723 put_bits(&pb, 8, mc->residue[j]);
724 }
725 }
726
727 // modes
728 put_bits(&pb, 6, venc->nmodes - 1);
729 for (i = 0; i < venc->nmodes; i++) {
730 put_bits(&pb, 1, venc->modes[i].blockflag);
731 put_bits(&pb, 16, 0); // reserved window type
732 put_bits(&pb, 16, 0); // reserved transform type
733 put_bits(&pb, 8, venc->modes[i].mapping);
734 }
735
736 put_bits(&pb, 1, 1); // framing
737
738 flush_put_bits(&pb);
739 hlens[2] = put_bytes_output(&pb);
740
741 len = hlens[0] + hlens[1] + hlens[2];
742 p = *out = av_mallocz(64 + len + len/255);
743 if (!p) {
745 return AVERROR(ENOMEM);
746 }
747
748 *p++ = 2;
749 p += av_xiphlacing(p, hlens[0]);
750 p += av_xiphlacing(p, hlens[1]);
751 buffer_len = 0;
752 for (i = 0; i < 3; i++) {
753 memcpy(p, buffer + buffer_len, hlens[i]);
754 p += hlens[i];
755 buffer_len += hlens[i];
756 }
757
759 return p - *out;
760}
761
762static float get_floor_average(vorbis_enc_floor * fc, float *coeffs, int i)
763{
764 int begin = fc->list[fc->list[FFMAX(i-1, 0)].sort].x;
765 int end = fc->list[fc->list[FFMIN(i+1, fc->values - 1)].sort].x;
766 int j;
767 float average = 0;
768
769 for (j = begin; j < end; j++)
770 average += fabs(coeffs[j]);
771 return average / (end - begin);
772}
773
775 float *coeffs, uint16_t *posts, int samples)
776{
777 int range = 255 / fc->multiplier + 1;
778 int i;
779 float tot_average = 0.0;
780 float averages[MAX_FLOOR_VALUES];
781 for (i = 0; i < fc->values; i++) {
782 averages[i] = get_floor_average(fc, coeffs, i);
783 tot_average += averages[i];
784 }
785 tot_average /= fc->values;
786 tot_average /= venc->quality;
787
788 for (i = 0; i < fc->values; i++) {
789 int position = fc->list[fc->list[i].sort].x;
790 float average = averages[i];
791 int j;
792
793 average = sqrt(tot_average * average) * pow(1.25f, position*0.005f); // MAGIC!
794 for (j = 0; j < range - 1; j++)
795 if (ff_vorbis_floor1_inverse_db_table[j * fc->multiplier] > average)
796 break;
797 posts[fc->list[i].sort] = j;
798 }
799}
800
801static int render_point(int x0, int y0, int x1, int y1, int x)
802{
803 return y0 + (x - x0) * (y1 - y0) / (x1 - x0);
804}
805
807 PutBitContext *pb, uint16_t *posts,
808 float *floor, int samples)
809{
810 int range = 255 / fc->multiplier + 1;
811 int coded[MAX_FLOOR_VALUES]; // first 2 values are unused
812 int i, counter;
813
814 if (put_bits_left(pb) < 1 + 2 * ilog(range - 1))
815 return AVERROR(EINVAL);
816 put_bits(pb, 1, 1); // non zero
817 put_bits(pb, ilog(range - 1), posts[0]);
818 put_bits(pb, ilog(range - 1), posts[1]);
819 coded[0] = coded[1] = 1;
820
821 for (i = 2; i < fc->values; i++) {
822 int predicted = render_point(fc->list[fc->list[i].low].x,
823 posts[fc->list[i].low],
824 fc->list[fc->list[i].high].x,
825 posts[fc->list[i].high],
826 fc->list[i].x);
827 int highroom = range - predicted;
828 int lowroom = predicted;
829 int room = FFMIN(highroom, lowroom);
830 if (predicted == posts[i]) {
831 coded[i] = 0; // must be used later as flag!
832 continue;
833 } else {
834 if (!coded[fc->list[i].low ])
835 coded[fc->list[i].low ] = -1;
836 if (!coded[fc->list[i].high])
837 coded[fc->list[i].high] = -1;
838 }
839 if (posts[i] > predicted) {
840 if (posts[i] - predicted > room)
841 coded[i] = posts[i] - predicted + lowroom;
842 else
843 coded[i] = (posts[i] - predicted) << 1;
844 } else {
845 if (predicted - posts[i] > room)
846 coded[i] = predicted - posts[i] + highroom - 1;
847 else
848 coded[i] = ((predicted - posts[i]) << 1) - 1;
849 }
850 }
851
852 counter = 2;
853 for (i = 0; i < fc->partitions; i++) {
854 vorbis_enc_floor_class * c = &fc->classes[fc->partition_to_class[i]];
855 int k, cval = 0, csub = 1<<c->subclass;
856 if (c->subclass) {
857 vorbis_enc_codebook * book = &venc->codebooks[c->masterbook];
858 int cshift = 0;
859 for (k = 0; k < c->dim; k++) {
860 int l;
861 for (l = 0; l < csub; l++) {
862 int maxval = 1;
863 if (c->books[l] != -1)
864 maxval = venc->codebooks[c->books[l]].nentries;
865 // coded could be -1, but this still works, cause that is 0
866 if (coded[counter + k] < maxval)
867 break;
868 }
869 assert(l != csub);
870 cval |= l << cshift;
871 cshift += c->subclass;
872 }
873 if (put_codeword(pb, book, cval))
874 return AVERROR(EINVAL);
875 }
876 for (k = 0; k < c->dim; k++) {
877 int book = c->books[cval & (csub-1)];
878 int entry = coded[counter++];
879 cval >>= c->subclass;
880 if (book == -1)
881 continue;
882 if (entry == -1)
883 entry = 0;
884 if (put_codeword(pb, &venc->codebooks[book], entry))
885 return AVERROR(EINVAL);
886 }
887 }
888
889 ff_vorbis_floor1_render_list(fc->list, fc->values, posts, coded,
890 fc->multiplier, floor, samples);
891
892 return 0;
893}
894
896 float *num)
897{
898 int i, entry = -1;
899 float distance = FLT_MAX;
900 assert(book->dimensions);
901 for (i = 0; i < book->nentries; i++) {
902 float * vec = book->dimensions + i * book->ndimensions, d = book->pow2[i];
903 int j;
904 if (!book->lens[i])
905 continue;
906 for (j = 0; j < book->ndimensions; j++)
907 d -= vec[j] * num[j];
908 if (distance > d) {
909 entry = i;
910 distance = d;
911 }
912 }
913 if (put_codeword(pb, book, entry))
914 return NULL;
915 return &book->dimensions[entry * book->ndimensions];
916}
917
919 PutBitContext *pb, float *coeffs, int samples,
920 int real_ch)
921{
922 int pass, i, j, p, k;
923 int psize = rc->partition_size;
924 int partitions = (rc->end - rc->begin) / psize;
925 int channels = (rc->type == 2) ? 1 : real_ch;
927 int classwords = venc->codebooks[rc->classbook].ndimensions;
928
929 av_assert0(rc->type == 2);
930 av_assert0(real_ch == 2);
931 for (p = 0; p < partitions; p++) {
932 float max1 = 0.0, max2 = 0.0;
933 int s = rc->begin + p * psize;
934 for (k = s; k < s + psize; k += 2) {
935 max1 = FFMAX(max1, fabs(coeffs[ k / real_ch]));
936 max2 = FFMAX(max2, fabs(coeffs[samples + k / real_ch]));
937 }
938
939 for (i = 0; i < rc->classifications - 1; i++)
940 if (max1 < rc->maxes[i][0] && max2 < rc->maxes[i][1])
941 break;
942 classes[0][p] = i;
943 }
944
945 for (pass = 0; pass < 8; pass++) {
946 p = 0;
947 while (p < partitions) {
948 if (pass == 0)
949 for (j = 0; j < channels; j++) {
950 vorbis_enc_codebook * book = &venc->codebooks[rc->classbook];
951 int entry = 0;
952 for (i = 0; i < classwords; i++) {
953 entry *= rc->classifications;
954 entry += classes[j][p + i];
955 }
956 if (put_codeword(pb, book, entry))
957 return AVERROR(EINVAL);
958 }
959 for (i = 0; i < classwords && p < partitions; i++, p++) {
960 for (j = 0; j < channels; j++) {
961 int nbook = rc->books[classes[j][p]][pass];
962 vorbis_enc_codebook * book = &venc->codebooks[nbook];
963 float *buf = coeffs + samples*j + rc->begin + p*psize;
964 if (nbook == -1)
965 continue;
966
967 assert(rc->type == 0 || rc->type == 2);
968 assert(!(psize % book->ndimensions));
969
970 if (rc->type == 0) {
971 for (k = 0; k < psize; k += book->ndimensions) {
972 int l;
973 float *a = put_vector(book, pb, &buf[k]);
974 if (!a)
975 return AVERROR(EINVAL);
976 for (l = 0; l < book->ndimensions; l++)
977 buf[k + l] -= a[l];
978 }
979 } else {
980 int s = rc->begin + p * psize, a1, b1;
981 a1 = (s % real_ch) * samples;
982 b1 = s / real_ch;
983 s = real_ch * samples;
984 for (k = 0; k < psize; k += book->ndimensions) {
985 int dim, a2 = a1, b2 = b1;
986 float vec[MAX_CODEBOOK_DIM], *pv = vec;
987 for (dim = book->ndimensions; dim--; ) {
988 *pv++ = coeffs[a2 + b2];
989 if ((a2 += samples) == s) {
990 a2 = 0;
991 b2++;
992 }
993 }
994 pv = put_vector(book, pb, vec);
995 if (!pv)
996 return AVERROR(EINVAL);
997 for (dim = book->ndimensions; dim--; ) {
998 coeffs[a1 + b1] -= *pv++;
999 if ((a1 += samples) == s) {
1000 a1 = 0;
1001 b1++;
1002 }
1003 }
1004 }
1005 }
1006 }
1007 }
1008 }
1009 }
1010 return 0;
1011}
1012
1014{
1015 int channel;
1016 const float * win = venc->win[1];
1017 int window_len = 1 << (venc->log2_blocksize[1] - 1);
1018 float n = (float)(1 << venc->log2_blocksize[1]) / 4.0;
1019 AVFloatDSPContext *fdsp = venc->fdsp;
1020
1021 for (channel = 0; channel < venc->channels; channel++) {
1022 float *offset = venc->samples + channel * window_len * 2;
1023
1024 fdsp->vector_fmul(offset, offset, win, window_len);
1025 fdsp->vector_fmul_scalar(offset, offset, 1/n, window_len);
1026
1027 offset += window_len;
1028
1029 fdsp->vector_fmul_reverse(offset, offset, win, window_len);
1030 fdsp->vector_fmul_scalar(offset, offset, 1/n, window_len);
1031
1032 venc->mdct_fn[1](venc->mdct[1], venc->coeffs + channel * window_len,
1033 venc->samples + channel * window_len * 2, sizeof(float));
1034 }
1035 return 1;
1036}
1037
1038/* Used for padding the last encoded packet */
1040{
1042 int ch;
1043
1044 if (!f)
1045 return NULL;
1046
1047 f->format = avctx->sample_fmt;
1048 f->nb_samples = avctx->frame_size;
1049 f->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC;
1050 f->ch_layout.nb_channels = channels;
1051
1052 if (av_frame_get_buffer(f, 4)) {
1053 av_frame_free(&f);
1054 return NULL;
1055 }
1056
1057 for (ch = 0; ch < channels; ch++) {
1058 size_t bps = av_get_bytes_per_sample(f->format);
1059 memset(f->extended_data[ch], 0, bps * f->nb_samples);
1060 }
1061 return f;
1062}
1063
1064/* Set up audio samples for psy analysis and window/mdct */
1065static void move_audio(vorbis_enc_context *venc, int sf_size)
1066{
1067 AVFrame *cur = NULL;
1068 int frame_size = 1 << (venc->log2_blocksize[1] - 1);
1069 int subframes = frame_size / sf_size;
1070 int sf, ch;
1071
1072 /* Copy samples from last frame into current frame */
1073 if (venc->have_saved)
1074 for (ch = 0; ch < venc->channels; ch++)
1075 memcpy(venc->samples + 2 * ch * frame_size,
1076 venc->saved + ch * frame_size, sizeof(float) * frame_size);
1077 else
1078 for (ch = 0; ch < venc->channels; ch++)
1079 memset(venc->samples + 2 * ch * frame_size, 0, sizeof(float) * frame_size);
1080
1081 for (sf = 0; sf < subframes; sf++) {
1082 cur = ff_bufqueue_get(&venc->bufqueue);
1083
1084 for (ch = 0; ch < venc->channels; ch++) {
1085 float *offset = venc->samples + 2 * ch * frame_size + frame_size;
1086 float *save = venc->saved + ch * frame_size;
1087 const float *input = (float *) cur->extended_data[ch];
1088 const size_t len = cur->nb_samples * sizeof(float);
1089
1090 memcpy(offset + sf*sf_size, input, len);
1091 memcpy(save + sf*sf_size, input, len); // Move samples for next frame
1092 }
1093 av_frame_free(&cur);
1094 }
1095 venc->have_saved = 1;
1096 memcpy(venc->scratch, venc->samples, 2 * venc->channels * frame_size);
1097}
1098
1100 const AVFrame *frame, int *got_packet_ptr)
1101{
1102 vorbis_enc_context *venc = avctx->priv_data;
1103 int i, ret, need_more;
1104 int frame_size = 1 << (venc->log2_blocksize[1] - 1);
1106 vorbis_enc_mapping *mapping;
1107 PutBitContext pb;
1108
1109 if (frame) {
1110 AVFrame *clone;
1111 if ((ret = ff_af_queue_add(&venc->afq, frame)) < 0)
1112 return ret;
1113 clone = av_frame_clone(frame);
1114 if (!clone)
1115 return AVERROR(ENOMEM);
1116 ff_bufqueue_add(avctx, &venc->bufqueue, clone);
1117 } else
1118 if (!venc->afq.remaining_samples)
1119 return 0;
1120
1121 need_more = venc->bufqueue.available * avctx->frame_size < frame_size;
1122 need_more = frame && need_more;
1123 if (need_more)
1124 return 0;
1125
1126 /* Pad the bufqueue with empty frames for encoding the last packet. */
1127 if (!frame) {
1128 if (venc->bufqueue.available * avctx->frame_size < frame_size) {
1129 int frames_needed = (frame_size/avctx->frame_size) - venc->bufqueue.available;
1130 int i;
1131
1132 for (i = 0; i < frames_needed; i++) {
1133 AVFrame *empty = spawn_empty_frame(avctx, venc->channels);
1134 if (!empty)
1135 return AVERROR(ENOMEM);
1136
1137 ff_bufqueue_add(avctx, &venc->bufqueue, empty);
1138 }
1139 }
1140 }
1141
1142 move_audio(venc, avctx->frame_size);
1143
1144 if (!apply_window_and_mdct(venc))
1145 return 0;
1146
1147 if ((ret = ff_alloc_packet(avctx, avpkt, 8192)) < 0)
1148 return ret;
1149
1150 init_put_bits(&pb, avpkt->data, avpkt->size);
1151
1152 put_bits(&pb, 1, 0); // magic bit
1153
1154 put_bits(&pb, ilog(venc->nmodes - 1), 1); // Mode for current frame
1155
1156 mode = &venc->modes[1];
1157 mapping = &venc->mappings[mode->mapping];
1158 if (mode->blockflag) {
1159 put_bits(&pb, 1, 1); // Previous windowflag
1160 put_bits(&pb, 1, 1); // Next windowflag
1161 }
1162
1163 for (i = 0; i < venc->channels; i++) {
1164 vorbis_enc_floor *fc = &venc->floors[mapping->floor[mapping->mux[i]]];
1165 uint16_t posts[MAX_FLOOR_VALUES];
1166 floor_fit(venc, fc, &venc->coeffs[i * frame_size], posts, frame_size);
1167 if (floor_encode(venc, fc, &pb, posts, &venc->floor[i * frame_size], frame_size)) {
1168 av_log(avctx, AV_LOG_ERROR, "output buffer is too small\n");
1169 return AVERROR(EINVAL);
1170 }
1171 }
1172
1173 for (i = 0; i < venc->channels * frame_size; i++)
1174 venc->coeffs[i] /= venc->floor[i];
1175
1176 for (i = 0; i < mapping->coupling_steps; i++) {
1177 float *mag = venc->coeffs + mapping->magnitude[i] * frame_size;
1178 float *ang = venc->coeffs + mapping->angle[i] * frame_size;
1179 int j;
1180 for (j = 0; j < frame_size; j++) {
1181 float a = ang[j];
1182 ang[j] -= mag[j];
1183 if (mag[j] > 0)
1184 ang[j] = -ang[j];
1185 if (ang[j] < 0)
1186 mag[j] = a;
1187 }
1188 }
1189
1190 if (residue_encode(venc, &venc->residues[mapping->residue[mapping->mux[0]]],
1191 &pb, venc->coeffs, frame_size, venc->channels)) {
1192 av_log(avctx, AV_LOG_ERROR, "output buffer is too small\n");
1193 return AVERROR(EINVAL);
1194 }
1195
1196 flush_put_bits(&pb);
1197 avpkt->size = put_bytes_output(&pb);
1198
1199 ret = ff_af_queue_remove(&venc->afq, frame_size, avpkt);
1200 if (ret < 0)
1201 return ret;
1202
1203 if (frame_size > avpkt->duration) {
1204 uint8_t *side = av_packet_new_side_data(avpkt, AV_PKT_DATA_SKIP_SAMPLES, 10);
1205 if (!side)
1206 return AVERROR(ENOMEM);
1207 AV_WL32(&side[4], frame_size - avpkt->duration);
1208 }
1209
1210 *got_packet_ptr = 1;
1211 return 0;
1212}
1213
1214
1216{
1217 vorbis_enc_context *venc = avctx->priv_data;
1218 int i;
1219
1220 if (venc->codebooks)
1221 for (i = 0; i < venc->ncodebooks; i++) {
1222 av_freep(&venc->codebooks[i].lens);
1223 av_freep(&venc->codebooks[i].codewords);
1224 av_freep(&venc->codebooks[i].quantlist);
1225 av_freep(&venc->codebooks[i].dimensions);
1226 av_freep(&venc->codebooks[i].pow2);
1227 }
1228 av_freep(&venc->codebooks);
1229
1230 if (venc->floors)
1231 for (i = 0; i < venc->nfloors; i++) {
1232 int j;
1233 if (venc->floors[i].classes)
1234 for (j = 0; j < venc->floors[i].nclasses; j++)
1235 av_freep(&venc->floors[i].classes[j].books);
1236 av_freep(&venc->floors[i].classes);
1238 av_freep(&venc->floors[i].list);
1239 }
1240 av_freep(&venc->floors);
1241
1242 if (venc->residues)
1243 for (i = 0; i < venc->nresidues; i++) {
1244 av_freep(&venc->residues[i].books);
1245 av_freep(&venc->residues[i].maxes);
1246 }
1247 av_freep(&venc->residues);
1248
1249 if (venc->mappings)
1250 for (i = 0; i < venc->nmappings; i++) {
1251 av_freep(&venc->mappings[i].mux);
1252 av_freep(&venc->mappings[i].floor);
1253 av_freep(&venc->mappings[i].residue);
1254 av_freep(&venc->mappings[i].magnitude);
1255 av_freep(&venc->mappings[i].angle);
1256 }
1257 av_freep(&venc->mappings);
1258
1259 av_freep(&venc->modes);
1260
1261 av_freep(&venc->saved);
1262 av_freep(&venc->samples);
1263 av_freep(&venc->floor);
1264 av_freep(&venc->coeffs);
1265 av_freep(&venc->scratch);
1266 av_freep(&venc->fdsp);
1267
1268 av_tx_uninit(&venc->mdct[0]);
1269 av_tx_uninit(&venc->mdct[1]);
1270 ff_af_queue_close(&venc->afq);
1272
1273 return 0 ;
1274}
1275
1277{
1278 vorbis_enc_context *venc = avctx->priv_data;
1279 int ret;
1280
1281 if (avctx->ch_layout.nb_channels != 2) {
1282 av_log(avctx, AV_LOG_ERROR, "Current FFmpeg Vorbis encoder only supports 2 channels.\n");
1283 return -1;
1284 }
1285
1286 if ((ret = create_vorbis_context(venc, avctx)) < 0)
1287 return ret;
1288
1289 avctx->bit_rate = 0;
1290 if (avctx->flags & AV_CODEC_FLAG_QSCALE)
1291 venc->quality = avctx->global_quality / (float)FF_QP2LAMBDA;
1292 else
1293 venc->quality = 8;
1294 venc->quality *= venc->quality;
1295
1296 if ((ret = put_main_header(venc, (uint8_t**)&avctx->extradata)) < 0)
1297 return ret;
1298 avctx->extradata_size = ret;
1299
1300 avctx->frame_size = 64;
1301 avctx->initial_padding = 1 << (venc->log2_blocksize[1] - 1);
1302
1303 ff_af_queue_init(avctx, &venc->afq);
1304
1305 return 0;
1306}
1307
1309 .p.name = "vorbis",
1310 CODEC_LONG_NAME("Vorbis"),
1311 .p.type = AVMEDIA_TYPE_AUDIO,
1312 .p.id = AV_CODEC_ID_VORBIS,
1313 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
1315 .priv_data_size = sizeof(vorbis_enc_context),
1318 .close = vorbis_encode_close,
1320 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
1321};
#define MAX_CHANNELS
Definition aac.h:33
static float win(SuperEqualizerContext *s, float n, int N)
const FFCodec ff_vorbis_encoder
Definition vorbisenc.c:1308
#define entry
channels
Definition aptx.h:31
av_cold void ff_af_queue_close(AudioFrameQueue *afq)
Close AudioFrameQueue.
av_cold void ff_af_queue_init(AVCodecContext *avctx, AudioFrameQueue *afq)
Initialize AudioFrameQueue.
int ff_af_queue_remove(AudioFrameQueue *afq, int nb_samples, AVPacket *pkt)
Remove frame(s) from the queue.
int ff_af_queue_add(AudioFrameQueue *afq, const AVFrame *f)
Add a frame to the queue.
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition avassert.h:68
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
Libavcodec external API header.
static void ff_bufqueue_add(void *log, struct FFBufQueue *queue, AVFrame *buf)
Add a buffer to the queue.
Definition bufferqueue.h:71
static void ff_bufqueue_discard_all(struct FFBufQueue *queue)
Unref and remove all buffers from the queue.
static AVFrame * ff_bufqueue_get(struct FFBufQueue *queue)
Get the first buffer from the queue and remove it.
Definition bufferqueue.h:98
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define f(width, name)
Definition cbs_vp8.c:236
#define s(width, name)
Definition cbs_vp9.c:198
#define FF_CODEC_ENCODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
#define CODEC_SAMPLEFMTS(...)
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static __device__ float fabs(float a)
static __device__ float floor(float a)
static const uint16_t fc[]
Definition dcaenc.h:43
unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
Encode extradata length to a buffer.
Definition utils.c:836
static AVFrame * frame
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
channel
Use these values when setting the channel map with ebur128_set_channel().
Definition ebur128.h:39
int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and allocate data.
Definition encode.c:62
int8_t exp
Definition eval.c:76
static const uint8_t bits[8]
Definition fastaudio.c:100
static const uint8_t frame_size[4]
Definition g723_1.h:222
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition avcodec.h:322
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition codec.h:79
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
#define AV_CODEC_FLAG_QSCALE
Use fixed qscale.
Definition avcodec.h:213
#define AV_CODEC_CAP_EXPERIMENTAL
Codec is experimental and is thus avoided in favor of non experimental encoders.
Definition codec.h:90
@ AV_CODEC_ID_VORBIS
Definition codec_id.h:458
@ AV_PKT_DATA_SKIP_SAMPLES
Recommends skipping the specified number of samples.
Definition packet.h:153
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, size_t size)
Allocate new information of a packet.
Definition packet.c:231
@ AV_CHANNEL_ORDER_UNSPEC
Only the channel count is specified, without any further information about the channel order.
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
Definition avutil.h:226
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition error.h:52
#define AVERROR(e)
Definition error.h:45
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
Definition frame.c:206
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition frame.c:52
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition frame.c:483
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
Definition samplefmt.c:108
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition samplefmt.h:66
int a
static void scale(int *out, const int *in, const int w, const int h, const int shift)
Definition intra.c:278
#define AV_WL32(p, v)
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition j2kenc.c:154
unsigned offset
Definition libaomenc.c:763
#define av_cold
Definition attributes.h:117
av_cold AVFloatDSPContext * avpriv_float_dsp_alloc(int bit_exact)
Allocate a float DSP context.
Definition float_dsp.c:135
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
enum AVColorRange range
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:264
Memory handling functions.
unsigned bps
Definition movenc.c:2074
static float distance(float x, float y, int band)
#define av_malloc(s)
Definition ops_static.c:52
bitstream writer API
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition put_bits.h:62
static int put_bits_left(PutBitContext *s)
Definition put_bits.h:135
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition put_bits.h:153
static int put_bytes_output(const PutBitContext *s)
Definition put_bits.h:99
static av_unused void put_bits32(PutBitContext *s, uint32_t value)
Write exactly 32 bits into a bitstream.
Definition put_bits.h:301
#define FF_ARRAY_ELEMS(a)
int nb_channels
Number of channels in this layout.
main external API structure.
Definition avcodec.h:443
AVChannelLayout ch_layout
Audio channel layout.
Definition avcodec.h:1055
enum AVSampleFormat sample_fmt
audio sample format
Definition avcodec.h:1047
int global_quality
Global quality for codecs which cannot change it per frame.
Definition avcodec.h:1235
int64_t bit_rate
the average bitrate
Definition avcodec.h:493
int initial_padding
Audio only.
Definition avcodec.h:1114
int sample_rate
samples per second
Definition avcodec.h:1040
int flags
AV_CODEC_FLAG_*.
Definition avcodec.h:500
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
Definition avcodec.h:526
int extradata_size
Definition avcodec.h:527
int frame_size
Number of samples per channel in an audio frame.
Definition avcodec.h:1068
void * priv_data
Definition avcodec.h:470
void(* vector_fmul_reverse)(float *dst, const float *src0, const float *src1, int len)
Calculate the entry wise product of two vectors of floats, and store the result in a vector of floats...
Definition float_dsp.h:154
void(* vector_fmul_scalar)(float *dst, const float *src, float mul, int len)
Multiply a vector of floats by a scalar float.
Definition float_dsp.h:85
void(* vector_fmul)(float *dst, const float *src0, const float *src1, int len)
Calculate the entry wise product of two vectors of floats and store the result in a vector of floats.
Definition float_dsp.h:38
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
int nb_samples
number of audio samples (per channel) described by this frame
Definition frame.h:552
uint8_t ** extended_data
pointers to the data planes/channels.
Definition frame.h:533
This structure stores compressed data.
Definition packet.h:580
int size
Definition packet.h:604
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition packet.h:621
uint8_t * data
Definition packet.h:603
Structure holding the queue.
Definition bufferqueue.h:49
unsigned short available
number of available buffers
Definition bufferqueue.h:52
Definition swscale.c:71
uint32_t * codewords
Definition vorbisenc.c:52
const float * win[2]
Definition vorbisenc.c:113
vorbis_enc_codebook * codebooks
Definition vorbisenc.c:126
av_tx_fn mdct_fn[2]
Definition vorbisenc.c:112
vorbis_enc_residue * residues
Definition vorbisenc.c:132
AVTXContext * mdct[2]
Definition vorbisenc.c:111
AudioFrameQueue afq
Definition vorbisenc.c:122
AVFloatDSPContext * fdsp
Definition vorbisenc.c:142
vorbis_enc_mapping * mappings
Definition vorbisenc.c:135
struct FFBufQueue bufqueue
Definition vorbisenc.c:123
vorbis_enc_mode * modes
Definition vorbisenc.c:138
vorbis_enc_floor * floors
Definition vorbisenc.c:129
int * partition_to_class
Definition vorbisenc.c:72
vorbis_enc_floor_class * classes
Definition vorbisenc.c:74
vorbis_floor1_entry * list
Definition vorbisenc.c:78
float(* maxes)[2]
Definition vorbisenc.c:89
int8_t(* books)[8]
Definition vorbisenc.c:88
Definition vorbis.h:26
#define av_malloc_array(a, b)
#define av_mallocz(s)
#define av_freep(p)
#define av_log(a,...)
static uint8_t tmp[40]
Definition aes_ctr.c:52
static FILE * out
Definition movenc.c:55
static char buffer[20]
Definition seek.c:32
av_cold void av_tx_uninit(AVTXContext **ctx)
Frees a context and sets *ctx to NULL, does nothing when *ctx == NULL.
Definition tx.c:295
av_cold int av_tx_init(AVTXContext **ctx, av_tx_fn *tx, enum AVTXType type, int inv, int len, const void *scale, uint64_t flags)
Initialize a transform context with the given configuration (i)MDCTs with an odd length are currently...
Definition tx.c:903
@ AV_TX_FLOAT_MDCT
Standard MDCT with a sample data type of float, double or int32_t, respectively.
Definition tx.h:68
void(* av_tx_fn)(AVTXContext *s, void *out, void *in, ptrdiff_t stride)
Function pointer to a function to perform the transform.
Definition tx.h:151
#define mc
static double cb(void *priv, double x, double y)
Definition vf_geq.c:247
static double b1(void *priv, double x, double y)
Definition vf_xfade.c:2034
static double b2(void *priv, double x, double y)
Definition vf_xfade.c:2035
static double a2(void *priv, double x, double y)
Definition vf_xfade.c:2030
static double a1(void *priv, double x, double y)
Definition vf_xfade.c:2029
static const uint8_t quant[64]
Definition vmixdec.c:71
unsigned int ff_vorbis_nth_root(unsigned int x, unsigned int n)
Definition vorbis.c:41
void ff_vorbis_floor1_render_list(vorbis_floor1_entry *list, int values, uint16_t *y_list, int *flag, int multiplier, float *out, int samples)
Definition vorbis.c:199
int ff_vorbis_len2vlc(uint8_t *bits, uint32_t *codes, unsigned num)
Definition vorbis.c:59
int ff_vorbis_ready_floor1_list(void *logctx, vorbis_floor1_entry *list, int values)
Definition vorbis.c:109
#define ilog(i)
Definition vorbis.h:41
const float ff_vorbis_floor1_inverse_db_table[256]
const float *const ff_vorbis_vwin[8]
int lookup
static const uint8_t quant_tables[]
static const struct @015006164144220275366232250062243134312011102000 cvectors[]
int dim
static const uint8_t codebooks[]
static const struct @151266026250113322224256156040007311307363203375 floor_classes[]
int len
static void put_float(PutBitContext *pb, float f)
Definition vorbisenc.c:481
static int put_codeword(PutBitContext *pb, vorbis_enc_codebook *cb, int entry)
Definition vorbisenc.c:156
#define MAX_FLOOR_VALUES
Definition vorbisenc.c:150
static av_cold int dsp_init(AVCodecContext *avctx, vorbis_enc_context *venc)
Definition vorbisenc.c:253
#define MAX_CODEBOOK_DIM
Definition vorbisenc.c:146
static void move_audio(vorbis_enc_context *venc, int sf_size)
Definition vorbisenc.c:1065
static void floor_fit(vorbis_enc_context *venc, vorbis_enc_floor *fc, float *coeffs, uint16_t *posts, int samples)
Definition vorbisenc.c:774
static float * put_vector(vorbis_enc_codebook *book, PutBitContext *pb, float *num)
Definition vorbisenc.c:895
static void put_codebook_header(PutBitContext *pb, vorbis_enc_codebook *cb)
Definition vorbisenc.c:495
static int create_vorbis_context(vorbis_enc_context *venc, AVCodecContext *avctx)
Definition vorbisenc.c:276
#define NUM_FLOOR_PARTITIONS
Definition vorbisenc.c:149
static void put_residue_header(PutBitContext *pb, vorbis_enc_residue *rc)
Definition vorbisenc.c:593
static void put_floor_header(PutBitContext *pb, vorbis_enc_floor *fc)
Definition vorbisenc.c:560
static av_cold int vorbis_encode_close(AVCodecContext *avctx)
Definition vorbisenc.c:1215
#define NUM_RESIDUE_PARTITIONS
Definition vorbisenc.c:154
static int render_point(int x0, int y0, int x1, int y1, int x)
Definition vorbisenc.c:801
static int put_main_header(vorbis_enc_context *venc, uint8_t **out)
Definition vorbisenc.c:625
static int ready_residue(vorbis_enc_residue *rc, vorbis_enc_context *venc)
Definition vorbisenc.c:214
static int ready_codebook(vorbis_enc_codebook *cb)
Definition vorbisenc.c:177
static int apply_window_and_mdct(vorbis_enc_context *venc)
Definition vorbisenc.c:1013
static int residue_encode(vorbis_enc_context *venc, vorbis_enc_residue *rc, PutBitContext *pb, float *coeffs, int samples, int real_ch)
Definition vorbisenc.c:918
static float get_floor_average(vorbis_enc_floor *fc, float *coeffs, int i)
Definition vorbisenc.c:762
static av_cold int vorbis_encode_init(AVCodecContext *avctx)
Definition vorbisenc.c:1276
static AVFrame * spawn_empty_frame(AVCodecContext *avctx, int channels)
Definition vorbisenc.c:1039
static int vorbis_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition vorbisenc.c:1099
static int floor_encode(vorbis_enc_context *venc, vorbis_enc_floor *fc, PutBitContext *pb, uint16_t *posts, float *floor, int samples)
Definition vorbisenc.c:806
static int cb_lookup_vals(int lookup, int dimensions, int entries)
Definition vorbisenc.c:168
static double c[64]