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