FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/vorbisenc.c
Date: 2021-09-16 08:47:15
Exec Total Coverage
Lines: 672 755 89.0%
Branches: 337 444 75.9%

Line Branch Exec Source
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 "encode.h"
32 #include "internal.h"
33 #include "fft.h"
34 #include "mathops.h"
35 #include "vorbis.h"
36 #include "vorbis_enc_data.h"
37
38 #include "audio_frame_queue.h"
39 #include "libavfilter/bufferqueue.h"
40
41 #define BITSTREAM_WRITER_LE
42 #include "put_bits.h"
43
44 #undef NDEBUG
45 #include <assert.h>
46
47 typedef struct vorbis_enc_codebook {
48 int nentries;
49 uint8_t *lens;
50 uint32_t *codewords;
51 int ndimensions;
52 float min;
53 float delta;
54 int seq_p;
55 int lookup;
56 int *quantlist;
57 float *dimensions;
58 float *pow2;
59 } vorbis_enc_codebook;
60
61 typedef struct vorbis_enc_floor_class {
62 int dim;
63 int subclass;
64 int masterbook;
65 int *books;
66 } vorbis_enc_floor_class;
67
68 typedef struct vorbis_enc_floor {
69 int partitions;
70 int *partition_to_class;
71 int nclasses;
72 vorbis_enc_floor_class *classes;
73 int multiplier;
74 int rangebits;
75 int values;
76 vorbis_floor1_entry *list;
77 } vorbis_enc_floor;
78
79 typedef struct vorbis_enc_residue {
80 int type;
81 int begin;
82 int end;
83 int partition_size;
84 int classifications;
85 int classbook;
86 int8_t (*books)[8];
87 float (*maxes)[2];
88 } vorbis_enc_residue;
89
90 typedef struct vorbis_enc_mapping {
91 int submaps;
92 int *mux;
93 int *floor;
94 int *residue;
95 int coupling_steps;
96 int *magnitude;
97 int *angle;
98 } vorbis_enc_mapping;
99
100 typedef struct vorbis_enc_mode {
101 int blockflag;
102 int mapping;
103 } vorbis_enc_mode;
104
105 typedef struct vorbis_enc_context {
106 int channels;
107 int sample_rate;
108 int log2_blocksize[2];
109 FFTContext mdct[2];
110 const float *win[2];
111 int have_saved;
112 float *saved;
113 float *samples;
114 float *floor; // also used for tmp values for mdct
115 float *coeffs; // also used for residue after floor
116 float *scratch; // used for tmp values for psy model
117 float quality;
118
119 AudioFrameQueue afq;
120 struct FFBufQueue bufqueue;
121
122 int ncodebooks;
123 vorbis_enc_codebook *codebooks;
124
125 int nfloors;
126 vorbis_enc_floor *floors;
127
128 int nresidues;
129 vorbis_enc_residue *residues;
130
131 int nmappings;
132 vorbis_enc_mapping *mappings;
133
134 int nmodes;
135 vorbis_enc_mode *modes;
136
137 int64_t next_pts;
138
139 AVFloatDSPContext *fdsp;
140 } vorbis_enc_context;
141
142 #define MAX_CHANNELS 2
143 #define MAX_CODEBOOK_DIM 8
144
145 #define MAX_FLOOR_CLASS_DIM 4
146 #define NUM_FLOOR_PARTITIONS 8
147 #define MAX_FLOOR_VALUES (MAX_FLOOR_CLASS_DIM*NUM_FLOOR_PARTITIONS+2)
148
149 #define RESIDUE_SIZE 1600
150 #define RESIDUE_PART_SIZE 32
151 #define NUM_RESIDUE_PARTITIONS (RESIDUE_SIZE/RESIDUE_PART_SIZE)
152
153 399604 static inline int put_codeword(PutBitContext *pb, vorbis_enc_codebook *cb,
154 int entry)
155 {
156 av_assert2(entry >= 0);
157 av_assert2(entry < cb->nentries);
158 av_assert2(cb->lens[entry]);
159
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 399604 times.
399604 if (put_bits_left(pb) < cb->lens[entry])
160 return AVERROR(EINVAL);
161 399604 put_bits(pb, cb->lens[entry], cb->codewords[entry]);
162 399604 return 0;
163 }
164
165 39 static int cb_lookup_vals(int lookup, int dimensions, int entries)
166 {
167
1/2
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
39 if (lookup == 1)
168 39 return ff_vorbis_nth_root(entries, dimensions);
169 else if (lookup == 2)
170 return dimensions *entries;
171 return 0;
172 }
173
174 29 static int ready_codebook(vorbis_enc_codebook *cb)
175 {
176 int i;
177
178 29 ff_vorbis_len2vlc(cb->lens, cb->codewords, cb->nentries);
179
180
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 13 times.
29 if (!cb->lookup) {
181 16 cb->pow2 = cb->dimensions = NULL;
182 } else {
183 13 int vals = cb_lookup_vals(cb->lookup, cb->ndimensions, cb->nentries);
184 13 cb->dimensions = av_malloc_array(cb->nentries, sizeof(float) * cb->ndimensions);
185 13 cb->pow2 = av_mallocz_array(cb->nentries, sizeof(float));
186
2/4
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 13 times.
13 if (!cb->dimensions || !cb->pow2)
187 return AVERROR(ENOMEM);
188
2/2
✓ Branch 0 taken 9341 times.
✓ Branch 1 taken 13 times.
9354 for (i = 0; i < cb->nentries; i++) {
189 9341 float last = 0;
190 int j;
191 9341 int div = 1;
192
2/2
✓ Branch 0 taken 60710 times.
✓ Branch 1 taken 9341 times.
70051 for (j = 0; j < cb->ndimensions; j++) {
193 int off;
194
1/2
✓ Branch 0 taken 60710 times.
✗ Branch 1 not taken.
60710 if (cb->lookup == 1)
195 60710 off = (i / div) % vals; // lookup type 1
196 else
197 off = i * cb->ndimensions + j; // lookup type 2
198
199 60710 cb->dimensions[i * cb->ndimensions + j] = last + cb->min + cb->quantlist[off] * cb->delta;
200
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60710 times.
60710 if (cb->seq_p)
201 last = cb->dimensions[i * cb->ndimensions + j];
202 60710 cb->pow2[i] += cb->dimensions[i * cb->ndimensions + j] * cb->dimensions[i * cb->ndimensions + j];
203 60710 div *= vals;
204 }
205 9341 cb->pow2[i] /= 2.0;
206 }
207 }
208 29 return 0;
209 }
210
211 1 static int ready_residue(vorbis_enc_residue *rc, vorbis_enc_context *venc)
212 {
213 int i;
214
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 av_assert0(rc->type == 2);
215 1 rc->maxes = av_mallocz_array(rc->classifications, sizeof(float[2]));
216
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!rc->maxes)
217 return AVERROR(ENOMEM);
218
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 1 times.
11 for (i = 0; i < rc->classifications; i++) {
219 int j;
220 vorbis_enc_codebook * cb;
221
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 1 times.
30 for (j = 0; j < 8; j++)
222
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 20 times.
29 if (rc->books[i][j] != -1)
223 9 break;
224
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 9 times.
10 if (j == 8) // zero
225 1 continue;
226 9 cb = &venc->codebooks[rc->books[i][j]];
227
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 assert(cb->ndimensions >= 2);
228
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 assert(cb->lookup);
229
230
2/2
✓ Branch 0 taken 8681 times.
✓ Branch 1 taken 9 times.
8690 for (j = 0; j < cb->nentries; j++) {
231 float a;
232
2/2
✓ Branch 0 taken 7798 times.
✓ Branch 1 taken 883 times.
8681 if (!cb->lens[j])
233 7798 continue;
234 883 a = fabs(cb->dimensions[j * cb->ndimensions]);
235
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 852 times.
883 if (a > rc->maxes[i][0])
236 31 rc->maxes[i][0] = a;
237 883 a = fabs(cb->dimensions[j * cb->ndimensions + 1]);
238
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 852 times.
883 if (a > rc->maxes[i][1])
239 31 rc->maxes[i][1] = a;
240 }
241 }
242 // small bias
243
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 1 times.
11 for (i = 0; i < rc->classifications; i++) {
244 10 rc->maxes[i][0] += 0.8;
245 10 rc->maxes[i][1] += 0.8;
246 }
247 1 return 0;
248 }
249
250 1 static av_cold int dsp_init(AVCodecContext *avctx, vorbis_enc_context *venc)
251 {
252 1 int ret = 0;
253
254 1 venc->fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
255
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!venc->fdsp)
256 return AVERROR(ENOMEM);
257
258 // init windows
259 1 venc->win[0] = ff_vorbis_vwin[venc->log2_blocksize[0] - 6];
260 1 venc->win[1] = ff_vorbis_vwin[venc->log2_blocksize[1] - 6];
261
262
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if ((ret = ff_mdct_init(&venc->mdct[0], venc->log2_blocksize[0], 0, 1.0)) < 0)
263 return ret;
264
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if ((ret = ff_mdct_init(&venc->mdct[1], venc->log2_blocksize[1], 0, 1.0)) < 0)
265 return ret;
266
267 1 return 0;
268 }
269
270 1 static int create_vorbis_context(vorbis_enc_context *venc,
271 AVCodecContext *avctx)
272 {
273 vorbis_enc_floor *fc;
274 vorbis_enc_residue *rc;
275 vorbis_enc_mapping *mc;
276 const uint8_t *clens, *quant;
277 int i, book, ret;
278
279 1 venc->channels = avctx->channels;
280 1 venc->sample_rate = avctx->sample_rate;
281 1 venc->log2_blocksize[0] = venc->log2_blocksize[1] = 11;
282
283 1 venc->ncodebooks = FF_ARRAY_ELEMS(cvectors);
284 1 venc->codebooks = av_mallocz(sizeof(vorbis_enc_codebook) * venc->ncodebooks);
285
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!venc->codebooks)
286 return AVERROR(ENOMEM);
287
288 // codebook 0..14 - floor1 book, values 0..255
289 // codebook 15 residue masterbook
290 // codebook 16..29 residue
291 1 clens = codebooks;
292 1 quant = quant_tables;
293
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 1 times.
30 for (book = 0; book < venc->ncodebooks; book++) {
294 29 vorbis_enc_codebook *cb = &venc->codebooks[book];
295 int vals;
296 29 cb->ndimensions = cvectors[book].dim;
297 29 cb->nentries = cvectors[book].real_len;
298 29 cb->min = cvectors[book].min;
299 29 cb->delta = cvectors[book].delta;
300 29 cb->lookup = cvectors[book].lookup;
301 29 cb->seq_p = 0;
302
303 29 cb->lens = av_malloc_array(cb->nentries, sizeof(uint8_t));
304 29 cb->codewords = av_malloc_array(cb->nentries, sizeof(uint32_t));
305
2/4
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 29 times.
29 if (!cb->lens || !cb->codewords)
306 return AVERROR(ENOMEM);
307 29 memcpy(cb->lens, clens, cvectors[book].len);
308 29 memset(cb->lens + cvectors[book].len, 0, cb->nentries - cvectors[book].len);
309 29 clens += cvectors[book].len;
310
311
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 16 times.
29 if (cb->lookup) {
312 13 vals = cb_lookup_vals(cb->lookup, cb->ndimensions, cb->nentries);
313 13 cb->quantlist = av_malloc_array(vals, sizeof(int));
314
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (!cb->quantlist)
315 return AVERROR(ENOMEM);
316
2/2
✓ Branch 0 taken 125 times.
✓ Branch 1 taken 13 times.
138 for (i = 0; i < vals; i++)
317 125 cb->quantlist[i] = *quant++;
318 } else {
319 16 cb->quantlist = NULL;
320 }
321
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
29 if ((ret = ready_codebook(cb)) < 0)
322 return ret;
323 }
324
325 1 venc->nfloors = 1;
326 1 venc->floors = av_mallocz(sizeof(vorbis_enc_floor) * venc->nfloors);
327
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!venc->floors)
328 return AVERROR(ENOMEM);
329
330 // just 1 floor
331 1 fc = &venc->floors[0];
332 1 fc->partitions = NUM_FLOOR_PARTITIONS;
333 1 fc->partition_to_class = av_malloc(sizeof(int) * fc->partitions);
334
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!fc->partition_to_class)
335 return AVERROR(ENOMEM);
336 1 fc->nclasses = 0;
337
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1 times.
9 for (i = 0; i < fc->partitions; i++) {
338 static const int a[] = {0, 1, 2, 2, 3, 3, 4, 4};
339 8 fc->partition_to_class[i] = a[i];
340 8 fc->nclasses = FFMAX(fc->nclasses, fc->partition_to_class[i]);
341 }
342 1 fc->nclasses++;
343 1 fc->classes = av_calloc(fc->nclasses, sizeof(vorbis_enc_floor_class));
344
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!fc->classes)
345 return AVERROR(ENOMEM);
346
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
6 for (i = 0; i < fc->nclasses; i++) {
347 5 vorbis_enc_floor_class * c = &fc->classes[i];
348 int j, books;
349 5 c->dim = floor_classes[i].dim;
350 5 c->subclass = floor_classes[i].subclass;
351 5 c->masterbook = floor_classes[i].masterbook;
352 5 books = (1 << c->subclass);
353 5 c->books = av_malloc_array(books, sizeof(int));
354
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (!c->books)
355 return AVERROR(ENOMEM);
356
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 5 times.
18 for (j = 0; j < books; j++)
357 13 c->books[j] = floor_classes[i].nbooks[j];
358 }
359 1 fc->multiplier = 2;
360 1 fc->rangebits = venc->log2_blocksize[1] - 1;
361
362 1 fc->values = 2;
363
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1 times.
9 for (i = 0; i < fc->partitions; i++)
364 8 fc->values += fc->classes[fc->partition_to_class[i]].dim;
365
366 1 fc->list = av_malloc_array(fc->values, sizeof(vorbis_floor1_entry));
367
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!fc->list)
368 return AVERROR(ENOMEM);
369 1 fc->list[0].x = 0;
370 1 fc->list[1].x = 1 << fc->rangebits;
371
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 1 times.
28 for (i = 2; i < fc->values; i++) {
372 static const int a[] = {
373 93, 23,372, 6, 46,186,750, 14, 33, 65,
374 130,260,556, 3, 10, 18, 28, 39, 55, 79,
375 111,158,220,312,464,650,850
376 };
377 27 fc->list[i].x = a[i - 2];
378 }
379
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (ff_vorbis_ready_floor1_list(avctx, fc->list, fc->values))
380 return AVERROR_BUG;
381
382 1 venc->nresidues = 1;
383 1 venc->residues = av_mallocz(sizeof(vorbis_enc_residue) * venc->nresidues);
384
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!venc->residues)
385 return AVERROR(ENOMEM);
386
387 // single residue
388 1 rc = &venc->residues[0];
389 1 rc->type = 2;
390 1 rc->begin = 0;
391 1 rc->end = 1600;
392 1 rc->partition_size = 32;
393 1 rc->classifications = 10;
394 1 rc->classbook = 15;
395 1 rc->books = av_malloc(sizeof(*rc->books) * rc->classifications);
396
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!rc->books)
397 return AVERROR(ENOMEM);
398 {
399 static const int8_t a[10][8] = {
400 { -1, -1, -1, -1, -1, -1, -1, -1, },
401 { -1, -1, 16, -1, -1, -1, -1, -1, },
402 { -1, -1, 17, -1, -1, -1, -1, -1, },
403 { -1, -1, 18, -1, -1, -1, -1, -1, },
404 { -1, -1, 19, -1, -1, -1, -1, -1, },
405 { -1, -1, 20, -1, -1, -1, -1, -1, },
406 { -1, -1, 21, -1, -1, -1, -1, -1, },
407 { 22, 23, -1, -1, -1, -1, -1, -1, },
408 { 24, 25, -1, -1, -1, -1, -1, -1, },
409 { 26, 27, 28, -1, -1, -1, -1, -1, },
410 };
411 1 memcpy(rc->books, a, sizeof a);
412 }
413
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if ((ret = ready_residue(rc, venc)) < 0)
414 return ret;
415
416 1 venc->nmappings = 1;
417 1 venc->mappings = av_mallocz(sizeof(vorbis_enc_mapping) * venc->nmappings);
418
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!venc->mappings)
419 return AVERROR(ENOMEM);
420
421 // single mapping
422 1 mc = &venc->mappings[0];
423 1 mc->submaps = 1;
424 1 mc->mux = av_malloc(sizeof(int) * venc->channels);
425
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!mc->mux)
426 return AVERROR(ENOMEM);
427
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 for (i = 0; i < venc->channels; i++)
428 2 mc->mux[i] = 0;
429 1 mc->floor = av_malloc(sizeof(int) * mc->submaps);
430 1 mc->residue = av_malloc(sizeof(int) * mc->submaps);
431
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (!mc->floor || !mc->residue)
432 return AVERROR(ENOMEM);
433
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 for (i = 0; i < mc->submaps; i++) {
434 1 mc->floor[i] = 0;
435 1 mc->residue[i] = 0;
436 }
437 1 mc->coupling_steps = venc->channels == 2 ? 1 : 0;
438 1 mc->magnitude = av_malloc(sizeof(int) * mc->coupling_steps);
439 1 mc->angle = av_malloc(sizeof(int) * mc->coupling_steps);
440
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (!mc->magnitude || !mc->angle)
441 return AVERROR(ENOMEM);
442
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (mc->coupling_steps) {
443 1 mc->magnitude[0] = 0;
444 1 mc->angle[0] = 1;
445 }
446
447 1 venc->nmodes = 2;
448 1 venc->modes = av_malloc(sizeof(vorbis_enc_mode) * venc->nmodes);
449
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!venc->modes)
450 return AVERROR(ENOMEM);
451
452 // Short block
453 1 venc->modes[0].blockflag = 0;
454 1 venc->modes[0].mapping = 0;
455 // Long block
456 1 venc->modes[1].blockflag = 1;
457 1 venc->modes[1].mapping = 0;
458
459 1 venc->have_saved = 0;
460 1 venc->saved = av_malloc_array(sizeof(float) * venc->channels, (1 << venc->log2_blocksize[1]) / 2);
461 1 venc->samples = av_malloc_array(sizeof(float) * venc->channels, (1 << venc->log2_blocksize[1]));
462 1 venc->floor = av_malloc_array(sizeof(float) * venc->channels, (1 << venc->log2_blocksize[1]) / 2);
463 1 venc->coeffs = av_malloc_array(sizeof(float) * venc->channels, (1 << venc->log2_blocksize[1]) / 2);
464 1 venc->scratch = av_malloc_array(sizeof(float) * venc->channels, (1 << venc->log2_blocksize[1]));
465
466
5/10
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 1 times.
1 if (!venc->saved || !venc->samples || !venc->floor || !venc->coeffs || !venc->scratch)
467 return AVERROR(ENOMEM);
468
469
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if ((ret = dsp_init(avctx, venc)) < 0)
470 return ret;
471
472 1 return 0;
473 }
474
475 26 static void put_float(PutBitContext *pb, float f)
476 {
477 int exp, mant;
478 26 uint32_t res = 0;
479 26 mant = (int)ldexp(frexp(f, &exp), 20);
480 26 exp += 788 - 20;
481
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 13 times.
26 if (mant < 0) {
482 13 res |= (1U << 31);
483 13 mant = -mant;
484 }
485 26 res |= mant | (exp << 21);
486 26 put_bits32(pb, res);
487 26 }
488
489 29 static void put_codebook_header(PutBitContext *pb, vorbis_enc_codebook *cb)
490 {
491 int i;
492 29 int ordered = 0;
493
494 29 put_bits(pb, 24, 0x564342); //magic
495 29 put_bits(pb, 16, cb->ndimensions);
496 29 put_bits(pb, 24, cb->nentries);
497
498
1/2
✓ Branch 0 taken 161 times.
✗ Branch 1 not taken.
161 for (i = 1; i < cb->nentries; i++)
499
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 132 times.
161 if (cb->lens[i] < cb->lens[i-1])
500 29 break;
501
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (i == cb->nentries)
502 ordered = 1;
503
504 29 put_bits(pb, 1, ordered);
505
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (ordered) {
506 int len = cb->lens[0];
507 put_bits(pb, 5, len - 1);
508 i = 0;
509 while (i < cb->nentries) {
510 int j;
511 for (j = 0; j+i < cb->nentries; j++)
512 if (cb->lens[j+i] != len)
513 break;
514 put_bits(pb, ilog(cb->nentries - i), j);
515 i += j;
516 len++;
517 }
518 } else {
519 29 int sparse = 0;
520
2/2
✓ Branch 0 taken 2080 times.
✓ Branch 1 taken 22 times.
2102 for (i = 0; i < cb->nentries; i++)
521
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 2073 times.
2080 if (!cb->lens[i])
522 7 break;
523
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 22 times.
29 if (i != cb->nentries)
524 7 sparse = 1;
525 29 put_bits(pb, 1, sparse);
526
527
2/2
✓ Branch 0 taken 10423 times.
✓ Branch 1 taken 29 times.
10452 for (i = 0; i < cb->nentries; i++) {
528
2/2
✓ Branch 0 taken 8431 times.
✓ Branch 1 taken 1992 times.
10423 if (sparse)
529 8431 put_bits(pb, 1, !!cb->lens[i]);
530
2/2
✓ Branch 0 taken 2625 times.
✓ Branch 1 taken 7798 times.
10423 if (cb->lens[i])
531 2625 put_bits(pb, 5, cb->lens[i] - 1);
532 }
533 }
534
535 29 put_bits(pb, 4, cb->lookup);
536
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 16 times.
29 if (cb->lookup) {
537 13 int tmp = cb_lookup_vals(cb->lookup, cb->ndimensions, cb->nentries);
538 13 int bits = ilog(cb->quantlist[0]);
539
540
2/2
✓ Branch 0 taken 112 times.
✓ Branch 1 taken 13 times.
125 for (i = 1; i < tmp; i++)
541 112 bits = FFMAX(bits, ilog(cb->quantlist[i]));
542
543 13 put_float(pb, cb->min);
544 13 put_float(pb, cb->delta);
545
546 13 put_bits(pb, 4, bits - 1);
547 13 put_bits(pb, 1, cb->seq_p);
548
549
2/2
✓ Branch 0 taken 125 times.
✓ Branch 1 taken 13 times.
138 for (i = 0; i < tmp; i++)
550 125 put_bits(pb, bits, cb->quantlist[i]);
551 }
552 29 }
553
554 1 static void put_floor_header(PutBitContext *pb, vorbis_enc_floor *fc)
555 {
556 int i;
557
558 1 put_bits(pb, 16, 1); // type, only floor1 is supported
559
560 1 put_bits(pb, 5, fc->partitions);
561
562
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1 times.
9 for (i = 0; i < fc->partitions; i++)
563 8 put_bits(pb, 4, fc->partition_to_class[i]);
564
565
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
6 for (i = 0; i < fc->nclasses; i++) {
566 int j, books;
567
568 5 put_bits(pb, 3, fc->classes[i].dim - 1);
569 5 put_bits(pb, 2, fc->classes[i].subclass);
570
571
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
5 if (fc->classes[i].subclass)
572 4 put_bits(pb, 8, fc->classes[i].masterbook);
573
574 5 books = (1 << fc->classes[i].subclass);
575
576
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 5 times.
18 for (j = 0; j < books; j++)
577 13 put_bits(pb, 8, fc->classes[i].books[j] + 1);
578 }
579
580 1 put_bits(pb, 2, fc->multiplier - 1);
581 1 put_bits(pb, 4, fc->rangebits);
582
583
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 1 times.
28 for (i = 2; i < fc->values; i++)
584 27 put_bits(pb, fc->rangebits, fc->list[i].x);
585 1 }
586
587 1 static void put_residue_header(PutBitContext *pb, vorbis_enc_residue *rc)
588 {
589 int i;
590
591 1 put_bits(pb, 16, rc->type);
592
593 1 put_bits(pb, 24, rc->begin);
594 1 put_bits(pb, 24, rc->end);
595 1 put_bits(pb, 24, rc->partition_size - 1);
596 1 put_bits(pb, 6, rc->classifications - 1);
597 1 put_bits(pb, 8, rc->classbook);
598
599
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 1 times.
11 for (i = 0; i < rc->classifications; i++) {
600 10 int j, tmp = 0;
601
2/2
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 10 times.
90 for (j = 0; j < 8; j++)
602 80 tmp |= (rc->books[i][j] != -1) << j;
603
604 10 put_bits(pb, 3, tmp & 7);
605 10 put_bits(pb, 1, tmp > 7);
606
607
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (tmp > 7)
608 put_bits(pb, 5, tmp >> 3);
609 }
610
611
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 1 times.
11 for (i = 0; i < rc->classifications; i++) {
612 int j;
613
2/2
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 10 times.
90 for (j = 0; j < 8; j++)
614
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 67 times.
80 if (rc->books[i][j] != -1)
615 13 put_bits(pb, 8, rc->books[i][j]);
616 }
617 1 }
618
619 1 static int put_main_header(vorbis_enc_context *venc, uint8_t **out)
620 {
621 int i;
622 PutBitContext pb;
623 int len, hlens[3];
624 1 int buffer_len = 50000;
625 1 uint8_t *buffer = av_mallocz(buffer_len), *p = buffer;
626
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!buffer)
627 return AVERROR(ENOMEM);
628
629 // identification header
630 1 init_put_bits(&pb, p, buffer_len);
631 1 put_bits(&pb, 8, 1); //magic
632
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1 times.
7 for (i = 0; "vorbis"[i]; i++)
633 6 put_bits(&pb, 8, "vorbis"[i]);
634 1 put_bits32(&pb, 0); // version
635 1 put_bits(&pb, 8, venc->channels);
636 1 put_bits32(&pb, venc->sample_rate);
637 1 put_bits32(&pb, 0); // bitrate
638 1 put_bits32(&pb, 0); // bitrate
639 1 put_bits32(&pb, 0); // bitrate
640 1 put_bits(&pb, 4, venc->log2_blocksize[0]);
641 1 put_bits(&pb, 4, venc->log2_blocksize[1]);
642 1 put_bits(&pb, 1, 1); // framing
643
644 1 flush_put_bits(&pb);
645 1 hlens[0] = put_bytes_output(&pb);
646 1 buffer_len -= hlens[0];
647 1 p += hlens[0];
648
649 // comment header
650 1 init_put_bits(&pb, p, buffer_len);
651 1 put_bits(&pb, 8, 3); //magic
652
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1 times.
7 for (i = 0; "vorbis"[i]; i++)
653 6 put_bits(&pb, 8, "vorbis"[i]);
654 1 put_bits32(&pb, 0); // vendor length TODO
655 1 put_bits32(&pb, 0); // amount of comments
656 1 put_bits(&pb, 1, 1); // framing
657
658 1 flush_put_bits(&pb);
659 1 hlens[1] = put_bytes_output(&pb);
660 1 buffer_len -= hlens[1];
661 1 p += hlens[1];
662
663 // setup header
664 1 init_put_bits(&pb, p, buffer_len);
665 1 put_bits(&pb, 8, 5); //magic
666
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1 times.
7 for (i = 0; "vorbis"[i]; i++)
667 6 put_bits(&pb, 8, "vorbis"[i]);
668
669 // codebooks
670 1 put_bits(&pb, 8, venc->ncodebooks - 1);
671
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 1 times.
30 for (i = 0; i < venc->ncodebooks; i++)
672 29 put_codebook_header(&pb, &venc->codebooks[i]);
673
674 // time domain, reserved, zero
675 1 put_bits(&pb, 6, 0);
676 1 put_bits(&pb, 16, 0);
677
678 // floors
679 1 put_bits(&pb, 6, venc->nfloors - 1);
680
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 for (i = 0; i < venc->nfloors; i++)
681 1 put_floor_header(&pb, &venc->floors[i]);
682
683 // residues
684 1 put_bits(&pb, 6, venc->nresidues - 1);
685
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 for (i = 0; i < venc->nresidues; i++)
686 1 put_residue_header(&pb, &venc->residues[i]);
687
688 // mappings
689 1 put_bits(&pb, 6, venc->nmappings - 1);
690
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 for (i = 0; i < venc->nmappings; i++) {
691 1 vorbis_enc_mapping *mc = &venc->mappings[i];
692 int j;
693 1 put_bits(&pb, 16, 0); // mapping type
694
695 1 put_bits(&pb, 1, mc->submaps > 1);
696
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (mc->submaps > 1)
697 put_bits(&pb, 4, mc->submaps - 1);
698
699 1 put_bits(&pb, 1, !!mc->coupling_steps);
700
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (mc->coupling_steps) {
701 1 put_bits(&pb, 8, mc->coupling_steps - 1);
702
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 for (j = 0; j < mc->coupling_steps; j++) {
703 1 put_bits(&pb, ilog(venc->channels - 1), mc->magnitude[j]);
704 1 put_bits(&pb, ilog(venc->channels - 1), mc->angle[j]);
705 }
706 }
707
708 1 put_bits(&pb, 2, 0); // reserved
709
710
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (mc->submaps > 1)
711 for (j = 0; j < venc->channels; j++)
712 put_bits(&pb, 4, mc->mux[j]);
713
714
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 for (j = 0; j < mc->submaps; j++) {
715 1 put_bits(&pb, 8, 0); // reserved time configuration
716 1 put_bits(&pb, 8, mc->floor[j]);
717 1 put_bits(&pb, 8, mc->residue[j]);
718 }
719 }
720
721 // modes
722 1 put_bits(&pb, 6, venc->nmodes - 1);
723
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 for (i = 0; i < venc->nmodes; i++) {
724 2 put_bits(&pb, 1, venc->modes[i].blockflag);
725 2 put_bits(&pb, 16, 0); // reserved window type
726 2 put_bits(&pb, 16, 0); // reserved transform type
727 2 put_bits(&pb, 8, venc->modes[i].mapping);
728 }
729
730 1 put_bits(&pb, 1, 1); // framing
731
732 1 flush_put_bits(&pb);
733 1 hlens[2] = put_bytes_output(&pb);
734
735 1 len = hlens[0] + hlens[1] + hlens[2];
736 1 p = *out = av_mallocz(64 + len + len/255);
737
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!p)
738 return AVERROR(ENOMEM);
739
740 1 *p++ = 2;
741 1 p += av_xiphlacing(p, hlens[0]);
742 1 p += av_xiphlacing(p, hlens[1]);
743 1 buffer_len = 0;
744
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 for (i = 0; i < 3; i++) {
745 3 memcpy(p, buffer + buffer_len, hlens[i]);
746 3 p += hlens[i];
747 3 buffer_len += hlens[i];
748 }
749
750 1 av_freep(&buffer);
751 1 return p - *out;
752 }
753
754 23838 static float get_floor_average(vorbis_enc_floor * fc, float *coeffs, int i)
755 {
756
2/2
✓ Branch 0 taken 22194 times.
✓ Branch 1 taken 1644 times.
23838 int begin = fc->list[fc->list[FFMAX(i-1, 0)].sort].x;
757
2/2
✓ Branch 0 taken 822 times.
✓ Branch 1 taken 23016 times.
23838 int end = fc->list[fc->list[FFMIN(i+1, fc->values - 1)].sort].x;
758 int j;
759 23838 float average = 0;
760
761
2/2
✓ Branch 0 taken 1683456 times.
✓ Branch 1 taken 23838 times.
1707294 for (j = begin; j < end; j++)
762 1683456 average += fabs(coeffs[j]);
763 23838 return average / (end - begin);
764 }
765
766 822 static void floor_fit(vorbis_enc_context *venc, vorbis_enc_floor *fc,
767 float *coeffs, uint16_t *posts, int samples)
768 {
769 822 int range = 255 / fc->multiplier + 1;
770 int i;
771 822 float tot_average = 0.0;
772 float averages[MAX_FLOOR_VALUES];
773
2/2
✓ Branch 0 taken 23838 times.
✓ Branch 1 taken 822 times.
24660 for (i = 0; i < fc->values; i++) {
774 23838 averages[i] = get_floor_average(fc, coeffs, i);
775 23838 tot_average += averages[i];
776 }
777 822 tot_average /= fc->values;
778 822 tot_average /= venc->quality;
779
780
2/2
✓ Branch 0 taken 23838 times.
✓ Branch 1 taken 822 times.
24660 for (i = 0; i < fc->values; i++) {
781 23838 int position = fc->list[fc->list[i].sort].x;
782 23838 float average = averages[i];
783 int j;
784
785 23838 average = sqrt(tot_average * average) * pow(1.25f, position*0.005f); // MAGIC!
786
1/2
✓ Branch 0 taken 1577852 times.
✗ Branch 1 not taken.
1577852 for (j = 0; j < range - 1; j++)
787
2/2
✓ Branch 0 taken 23838 times.
✓ Branch 1 taken 1554014 times.
1577852 if (ff_vorbis_floor1_inverse_db_table[j * fc->multiplier] > average)
788 23838 break;
789 23838 posts[fc->list[i].sort] = j;
790 }
791 822 }
792
793 22194 static int render_point(int x0, int y0, int x1, int y1, int x)
794 {
795 22194 return y0 + (x - x0) * (y1 - y0) / (x1 - x0);
796 }
797
798 822 static int floor_encode(vorbis_enc_context *venc, vorbis_enc_floor *fc,
799 PutBitContext *pb, uint16_t *posts,
800 float *floor, int samples)
801 {
802 822 int range = 255 / fc->multiplier + 1;
803 int coded[MAX_FLOOR_VALUES]; // first 2 values are unused
804 int i, counter;
805
806
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 822 times.
822 if (put_bits_left(pb) < 1 + 2 * ilog(range - 1))
807 return AVERROR(EINVAL);
808 822 put_bits(pb, 1, 1); // non zero
809 822 put_bits(pb, ilog(range - 1), posts[0]);
810 822 put_bits(pb, ilog(range - 1), posts[1]);
811 822 coded[0] = coded[1] = 1;
812
813
2/2
✓ Branch 0 taken 22194 times.
✓ Branch 1 taken 822 times.
23016 for (i = 2; i < fc->values; i++) {
814 22194 int predicted = render_point(fc->list[fc->list[i].low].x,
815 22194 posts[fc->list[i].low],
816 22194 fc->list[fc->list[i].high].x,
817 22194 posts[fc->list[i].high],
818 22194 fc->list[i].x);
819 22194 int highroom = range - predicted;
820 22194 int lowroom = predicted;
821 22194 int room = FFMIN(highroom, lowroom);
822
2/2
✓ Branch 0 taken 5981 times.
✓ Branch 1 taken 16213 times.
22194 if (predicted == posts[i]) {
823 5981 coded[i] = 0; // must be used later as flag!
824 5981 continue;
825 } else {
826
2/2
✓ Branch 0 taken 488 times.
✓ Branch 1 taken 15725 times.
16213 if (!coded[fc->list[i].low ])
827 488 coded[fc->list[i].low ] = -1;
828
2/2
✓ Branch 0 taken 1012 times.
✓ Branch 1 taken 15201 times.
16213 if (!coded[fc->list[i].high])
829 1012 coded[fc->list[i].high] = -1;
830 }
831
2/2
✓ Branch 0 taken 6159 times.
✓ Branch 1 taken 10054 times.
16213 if (posts[i] > predicted) {
832
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6159 times.
6159 if (posts[i] - predicted > room)
833 coded[i] = posts[i] - predicted + lowroom;
834 else
835 6159 coded[i] = (posts[i] - predicted) << 1;
836 } else {
837
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10054 times.
10054 if (predicted - posts[i] > room)
838 coded[i] = predicted - posts[i] + highroom - 1;
839 else
840 10054 coded[i] = ((predicted - posts[i]) << 1) - 1;
841 }
842 }
843
844 822 counter = 2;
845
2/2
✓ Branch 0 taken 6576 times.
✓ Branch 1 taken 822 times.
7398 for (i = 0; i < fc->partitions; i++) {
846 6576 vorbis_enc_floor_class * c = &fc->classes[fc->partition_to_class[i]];
847 6576 int k, cval = 0, csub = 1<<c->subclass;
848
2/2
✓ Branch 0 taken 5754 times.
✓ Branch 1 taken 822 times.
6576 if (c->subclass) {
849 5754 vorbis_enc_codebook * book = &venc->codebooks[c->masterbook];
850 5754 int cshift = 0;
851
2/2
✓ Branch 0 taken 19728 times.
✓ Branch 1 taken 5754 times.
25482 for (k = 0; k < c->dim; k++) {
852 int l;
853
1/2
✓ Branch 0 taken 27010 times.
✗ Branch 1 not taken.
27010 for (l = 0; l < csub; l++) {
854 27010 int maxval = 1;
855
2/2
✓ Branch 0 taken 15502 times.
✓ Branch 1 taken 11508 times.
27010 if (c->books[l] != -1)
856 15502 maxval = venc->codebooks[c->books[l]].nentries;
857 // coded could be -1, but this still works, cause that is 0
858
2/2
✓ Branch 0 taken 19728 times.
✓ Branch 1 taken 7282 times.
27010 if (coded[counter + k] < maxval)
859 19728 break;
860 }
861
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19728 times.
19728 assert(l != csub);
862 19728 cval |= l << cshift;
863 19728 cshift += c->subclass;
864 }
865
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5754 times.
5754 if (put_codeword(pb, book, cval))
866 return AVERROR(EINVAL);
867 }
868
2/2
✓ Branch 0 taken 22194 times.
✓ Branch 1 taken 6576 times.
28770 for (k = 0; k < c->dim; k++) {
869 22194 int book = c->books[cval & (csub-1)];
870 22194 int entry = coded[counter++];
871 22194 cval >>= c->subclass;
872
2/2
✓ Branch 0 taken 4227 times.
✓ Branch 1 taken 17967 times.
22194 if (book == -1)
873 4227 continue;
874
2/2
✓ Branch 0 taken 1500 times.
✓ Branch 1 taken 16467 times.
17967 if (entry == -1)
875 1500 entry = 0;
876
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 17967 times.
17967 if (put_codeword(pb, &venc->codebooks[book], entry))
877 return AVERROR(EINVAL);
878 }
879 }
880
881 822 ff_vorbis_floor1_render_list(fc->list, fc->values, posts, coded,
882 fc->multiplier, floor, samples);
883
884 822 return 0;
885 }
886
887 365608 static float *put_vector(vorbis_enc_codebook *book, PutBitContext *pb,
888 float *num)
889 {
890 365608 int i, entry = -1;
891 365608 float distance = FLT_MAX;
892
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 365608 times.
365608 assert(book->dimensions);
893
2/2
✓ Branch 0 taken 123219496 times.
✓ Branch 1 taken 365608 times.
123585104 for (i = 0; i < book->nentries; i++) {
894 123219496 float * vec = book->dimensions + i * book->ndimensions, d = book->pow2[i];
895 int j;
896
2/2
✓ Branch 0 taken 78436032 times.
✓ Branch 1 taken 44783464 times.
123219496 if (!book->lens[i])
897 78436032 continue;
898
2/2
✓ Branch 0 taken 98504832 times.
✓ Branch 1 taken 44783464 times.
143288296 for (j = 0; j < book->ndimensions; j++)
899 98504832 d -= vec[j] * num[j];
900
2/2
✓ Branch 0 taken 1516706 times.
✓ Branch 1 taken 43266758 times.
44783464 if (distance > d) {
901 1516706 entry = i;
902 1516706 distance = d;
903 }
904 }
905
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 365608 times.
365608 if (put_codeword(pb, book, entry))
906 return NULL;
907 365608 return &book->dimensions[entry * book->ndimensions];
908 }
909
910 411 static int residue_encode(vorbis_enc_context *venc, vorbis_enc_residue *rc,
911 PutBitContext *pb, float *coeffs, int samples,
912 int real_ch)
913 {
914 int pass, i, j, p, k;
915 411 int psize = rc->partition_size;
916 411 int partitions = (rc->end - rc->begin) / psize;
917
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 411 times.
411 int channels = (rc->type == 2) ? 1 : real_ch;
918 int classes[MAX_CHANNELS][NUM_RESIDUE_PARTITIONS];
919 411 int classwords = venc->codebooks[rc->classbook].ndimensions;
920
921
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 411 times.
411 av_assert0(rc->type == 2);
922
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 411 times.
411 av_assert0(real_ch == 2);
923
2/2
✓ Branch 0 taken 20550 times.
✓ Branch 1 taken 411 times.
20961 for (p = 0; p < partitions; p++) {
924 20550 float max1 = 0.0, max2 = 0.0;
925 20550 int s = rc->begin + p * psize;
926
2/2
✓ Branch 0 taken 328800 times.
✓ Branch 1 taken 20550 times.
349350 for (k = s; k < s + psize; k += 2) {
927
2/2
✓ Branch 0 taken 258832 times.
✓ Branch 1 taken 69968 times.
328800 max1 = FFMAX(max1, fabs(coeffs[ k / real_ch]));
928
2/2
✓ Branch 0 taken 258489 times.
✓ Branch 1 taken 70311 times.
328800 max2 = FFMAX(max2, fabs(coeffs[samples + k / real_ch]));
929 }
930
931
2/2
✓ Branch 0 taken 120752 times.
✓ Branch 1 taken 1243 times.
121995 for (i = 0; i < rc->classifications - 1; i++)
932
4/4
✓ Branch 0 taken 24546 times.
✓ Branch 1 taken 96206 times.
✓ Branch 2 taken 19307 times.
✓ Branch 3 taken 5239 times.
120752 if (max1 < rc->maxes[i][0] && max2 < rc->maxes[i][1])
933 19307 break;
934 20550 classes[0][p] = i;
935 }
936
937
2/2
✓ Branch 0 taken 3288 times.
✓ Branch 1 taken 411 times.
3699 for (pass = 0; pass < 8; pass++) {
938 3288 p = 0;
939
2/2
✓ Branch 0 taken 82200 times.
✓ Branch 1 taken 3288 times.
85488 while (p < partitions) {
940
2/2
✓ Branch 0 taken 10275 times.
✓ Branch 1 taken 71925 times.
82200 if (pass == 0)
941
2/2
✓ Branch 0 taken 10275 times.
✓ Branch 1 taken 10275 times.
20550 for (j = 0; j < channels; j++) {
942 10275 vorbis_enc_codebook * book = &venc->codebooks[rc->classbook];
943 10275 int entry = 0;
944
2/2
✓ Branch 0 taken 20550 times.
✓ Branch 1 taken 10275 times.
30825 for (i = 0; i < classwords; i++) {
945 20550 entry *= rc->classifications;
946 20550 entry += classes[j][p + i];
947 }
948
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10275 times.
10275 if (put_codeword(pb, book, entry))
949 return AVERROR(EINVAL);
950 }
951
3/4
✓ Branch 0 taken 164400 times.
✓ Branch 1 taken 82200 times.
✓ Branch 2 taken 164400 times.
✗ Branch 3 not taken.
246600 for (i = 0; i < classwords && p < partitions; i++, p++) {
952
2/2
✓ Branch 0 taken 164400 times.
✓ Branch 1 taken 164400 times.
328800 for (j = 0; j < channels; j++) {
953 164400 int nbook = rc->books[classes[j][p]][pass];
954 164400 vorbis_enc_codebook * book = &venc->codebooks[nbook];
955 164400 float *buf = coeffs + samples*j + rc->begin + p*psize;
956
2/2
✓ Branch 0 taken 137852 times.
✓ Branch 1 taken 26548 times.
164400 if (nbook == -1)
957 137852 continue;
958
959
2/4
✓ Branch 0 taken 26548 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 26548 times.
26548 assert(rc->type == 0 || rc->type == 2);
960
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26548 times.
26548 assert(!(psize % book->ndimensions));
961
962
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26548 times.
26548 if (rc->type == 0) {
963 for (k = 0; k < psize; k += book->ndimensions) {
964 int l;
965 float *a = put_vector(book, pb, &buf[k]);
966 if (!a)
967 return AVERROR(EINVAL);
968 for (l = 0; l < book->ndimensions; l++)
969 buf[k + l] -= a[l];
970 }
971 } else {
972 26548 int s = rc->begin + p * psize, a1, b1;
973 26548 a1 = (s % real_ch) * samples;
974 26548 b1 = s / real_ch;
975 26548 s = real_ch * samples;
976
2/2
✓ Branch 0 taken 365608 times.
✓ Branch 1 taken 26548 times.
392156 for (k = 0; k < psize; k += book->ndimensions) {
977 365608 int dim, a2 = a1, b2 = b1;
978 365608 float vec[MAX_CODEBOOK_DIM], *pv = vec;
979
2/2
✓ Branch 0 taken 849536 times.
✓ Branch 1 taken 365608 times.
1215144 for (dim = book->ndimensions; dim--; ) {
980 849536 *pv++ = coeffs[a2 + b2];
981
2/2
✓ Branch 0 taken 424768 times.
✓ Branch 1 taken 424768 times.
849536 if ((a2 += samples) == s) {
982 424768 a2 = 0;
983 424768 b2++;
984 }
985 }
986 365608 pv = put_vector(book, pb, vec);
987
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 365608 times.
365608 if (!pv)
988 return AVERROR(EINVAL);
989
2/2
✓ Branch 0 taken 849536 times.
✓ Branch 1 taken 365608 times.
1215144 for (dim = book->ndimensions; dim--; ) {
990 849536 coeffs[a1 + b1] -= *pv++;
991
2/2
✓ Branch 0 taken 424768 times.
✓ Branch 1 taken 424768 times.
849536 if ((a1 += samples) == s) {
992 424768 a1 = 0;
993 424768 b1++;
994 }
995 }
996 }
997 }
998 }
999 }
1000 }
1001 }
1002 411 return 0;
1003 }
1004
1005 411 static int apply_window_and_mdct(vorbis_enc_context *venc)
1006 {
1007 int channel;
1008 411 const float * win = venc->win[1];
1009 411 int window_len = 1 << (venc->log2_blocksize[1] - 1);
1010 411 float n = (float)(1 << venc->log2_blocksize[1]) / 4.0;
1011 411 AVFloatDSPContext *fdsp = venc->fdsp;
1012
1013
2/2
✓ Branch 0 taken 822 times.
✓ Branch 1 taken 411 times.
1233 for (channel = 0; channel < venc->channels; channel++) {
1014 822 float *offset = venc->samples + channel * window_len * 2;
1015
1016 822 fdsp->vector_fmul(offset, offset, win, window_len);
1017 822 fdsp->vector_fmul_scalar(offset, offset, 1/n, window_len);
1018
1019 822 offset += window_len;
1020
1021 822 fdsp->vector_fmul_reverse(offset, offset, win, window_len);
1022 822 fdsp->vector_fmul_scalar(offset, offset, 1/n, window_len);
1023
1024 822 venc->mdct[1].mdct_calc(&venc->mdct[1], venc->coeffs + channel * window_len,
1025 822 venc->samples + channel * window_len * 2);
1026 }
1027 411 return 1;
1028 }
1029
1030 /* Used for padding the last encoded packet */
1031 29 static AVFrame *spawn_empty_frame(AVCodecContext *avctx, int channels)
1032 {
1033 29 AVFrame *f = av_frame_alloc();
1034 int ch;
1035
1036
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (!f)
1037 return NULL;
1038
1039 29 f->format = avctx->sample_fmt;
1040 29 f->nb_samples = avctx->frame_size;
1041 29 f->channel_layout = avctx->channel_layout;
1042
1043
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
29 if (av_frame_get_buffer(f, 4)) {
1044 av_frame_free(&f);
1045 return NULL;
1046 }
1047
1048
2/2
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 29 times.
87 for (ch = 0; ch < channels; ch++) {
1049 58 size_t bps = av_get_bytes_per_sample(f->format);
1050 58 memset(f->extended_data[ch], 0, bps * f->nb_samples);
1051 }
1052 29 return f;
1053 }
1054
1055 /* Set up audio samples for psy analysis and window/mdct */
1056 411 static void move_audio(vorbis_enc_context *venc, int sf_size)
1057 {
1058 411 AVFrame *cur = NULL;
1059 411 int frame_size = 1 << (venc->log2_blocksize[1] - 1);
1060 411 int subframes = frame_size / sf_size;
1061 int sf, ch;
1062
1063 /* Copy samples from last frame into current frame */
1064
2/2
✓ Branch 0 taken 410 times.
✓ Branch 1 taken 1 times.
411 if (venc->have_saved)
1065
2/2
✓ Branch 0 taken 820 times.
✓ Branch 1 taken 410 times.
1230 for (ch = 0; ch < venc->channels; ch++)
1066 820 memcpy(venc->samples + 2 * ch * frame_size,
1067 820 venc->saved + ch * frame_size, sizeof(float) * frame_size);
1068 else
1069
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 for (ch = 0; ch < venc->channels; ch++)
1070 2 memset(venc->samples + 2 * ch * frame_size, 0, sizeof(float) * frame_size);
1071
1072
2/2
✓ Branch 0 taken 6576 times.
✓ Branch 1 taken 411 times.
6987 for (sf = 0; sf < subframes; sf++) {
1073 6576 cur = ff_bufqueue_get(&venc->bufqueue);
1074
1075
2/2
✓ Branch 0 taken 13152 times.
✓ Branch 1 taken 6576 times.
19728 for (ch = 0; ch < venc->channels; ch++) {
1076 13152 float *offset = venc->samples + 2 * ch * frame_size + frame_size;
1077 13152 float *save = venc->saved + ch * frame_size;
1078 13152 const float *input = (float *) cur->extended_data[ch];
1079 13152 const size_t len = cur->nb_samples * sizeof(float);
1080
1081 13152 memcpy(offset + sf*sf_size, input, len);
1082 13152 memcpy(save + sf*sf_size, input, len); // Move samples for next frame
1083 }
1084 6576 av_frame_free(&cur);
1085 }
1086 411 venc->have_saved = 1;
1087 411 memcpy(venc->scratch, venc->samples, 2 * venc->channels * frame_size);
1088 411 }
1089
1090 6550 static int vorbis_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
1091 const AVFrame *frame, int *got_packet_ptr)
1092 {
1093 6550 vorbis_enc_context *venc = avctx->priv_data;
1094 int i, ret, need_more;
1095 6550 int frame_size = 1 << (venc->log2_blocksize[1] - 1);
1096 vorbis_enc_mode *mode;
1097 vorbis_enc_mapping *mapping;
1098 PutBitContext pb;
1099
1100
2/2
✓ Branch 0 taken 6547 times.
✓ Branch 1 taken 3 times.
6550 if (frame) {
1101 AVFrame *clone;
1102
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6547 times.
6547 if ((ret = ff_af_queue_add(&venc->afq, frame)) < 0)
1103 return ret;
1104 6547 clone = av_frame_clone(frame);
1105
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6547 times.
6547 if (!clone)
1106 return AVERROR(ENOMEM);
1107 6547 ff_bufqueue_add(avctx, &venc->bufqueue, clone);
1108 } else
1109
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 if (!venc->afq.remaining_samples)
1110 1 return 0;
1111
1112 6549 need_more = venc->bufqueue.available * avctx->frame_size < frame_size;
1113
4/4
✓ Branch 0 taken 6547 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 6138 times.
✓ Branch 3 taken 409 times.
6549 need_more = frame && need_more;
1114
2/2
✓ Branch 0 taken 6138 times.
✓ Branch 1 taken 411 times.
6549 if (need_more)
1115 6138 return 0;
1116
1117 /* Pad the bufqueue with empty frames for encoding the last packet. */
1118
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 409 times.
411 if (!frame) {
1119
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (venc->bufqueue.available * avctx->frame_size < frame_size) {
1120 2 int frames_needed = (frame_size/avctx->frame_size) - venc->bufqueue.available;
1121 int i;
1122
1123
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 2 times.
31 for (i = 0; i < frames_needed; i++) {
1124 29 AVFrame *empty = spawn_empty_frame(avctx, venc->channels);
1125
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (!empty)
1126 return AVERROR(ENOMEM);
1127
1128 29 ff_bufqueue_add(avctx, &venc->bufqueue, empty);
1129 }
1130 }
1131 }
1132
1133 411 move_audio(venc, avctx->frame_size);
1134
1135
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 411 times.
411 if (!apply_window_and_mdct(venc))
1136 return 0;
1137
1138
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 411 times.
411 if ((ret = ff_alloc_packet(avctx, avpkt, 8192)) < 0)
1139 return ret;
1140
1141 411 init_put_bits(&pb, avpkt->data, avpkt->size);
1142
1143 411 put_bits(&pb, 1, 0); // magic bit
1144
1145 411 put_bits(&pb, ilog(venc->nmodes - 1), 1); // Mode for current frame
1146
1147 411 mode = &venc->modes[1];
1148 411 mapping = &venc->mappings[mode->mapping];
1149
1/2
✓ Branch 0 taken 411 times.
✗ Branch 1 not taken.
411 if (mode->blockflag) {
1150 411 put_bits(&pb, 1, 1); // Previous windowflag
1151 411 put_bits(&pb, 1, 1); // Next windowflag
1152 }
1153
1154
2/2
✓ Branch 0 taken 822 times.
✓ Branch 1 taken 411 times.
1233 for (i = 0; i < venc->channels; i++) {
1155 822 vorbis_enc_floor *fc = &venc->floors[mapping->floor[mapping->mux[i]]];
1156 uint16_t posts[MAX_FLOOR_VALUES];
1157 822 floor_fit(venc, fc, &venc->coeffs[i * frame_size], posts, frame_size);
1158
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 822 times.
822 if (floor_encode(venc, fc, &pb, posts, &venc->floor[i * frame_size], frame_size)) {
1159 av_log(avctx, AV_LOG_ERROR, "output buffer is too small\n");
1160 return AVERROR(EINVAL);
1161 }
1162 }
1163
1164
2/2
✓ Branch 0 taken 841728 times.
✓ Branch 1 taken 411 times.
842139 for (i = 0; i < venc->channels * frame_size; i++)
1165 841728 venc->coeffs[i] /= venc->floor[i];
1166
1167
2/2
✓ Branch 0 taken 411 times.
✓ Branch 1 taken 411 times.
822 for (i = 0; i < mapping->coupling_steps; i++) {
1168 411 float *mag = venc->coeffs + mapping->magnitude[i] * frame_size;
1169 411 float *ang = venc->coeffs + mapping->angle[i] * frame_size;
1170 int j;
1171
2/2
✓ Branch 0 taken 420864 times.
✓ Branch 1 taken 411 times.
421275 for (j = 0; j < frame_size; j++) {
1172 420864 float a = ang[j];
1173 420864 ang[j] -= mag[j];
1174
2/2
✓ Branch 0 taken 210263 times.
✓ Branch 1 taken 210601 times.
420864 if (mag[j] > 0)
1175 210263 ang[j] = -ang[j];
1176
2/2
✓ Branch 0 taken 166489 times.
✓ Branch 1 taken 254375 times.
420864 if (ang[j] < 0)
1177 166489 mag[j] = a;
1178 }
1179 }
1180
1181
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 411 times.
411 if (residue_encode(venc, &venc->residues[mapping->residue[mapping->mux[0]]],
1182 &pb, venc->coeffs, frame_size, venc->channels)) {
1183 av_log(avctx, AV_LOG_ERROR, "output buffer is too small\n");
1184 return AVERROR(EINVAL);
1185 }
1186
1187 411 flush_put_bits(&pb);
1188 411 avpkt->size = put_bytes_output(&pb);
1189
1190 411 ff_af_queue_remove(&venc->afq, frame_size, &avpkt->pts, &avpkt->duration);
1191
1192
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 410 times.
411 if (frame_size > avpkt->duration) {
1193 1 uint8_t *side = av_packet_new_side_data(avpkt, AV_PKT_DATA_SKIP_SAMPLES, 10);
1194
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!side)
1195 return AVERROR(ENOMEM);
1196 1 AV_WL32(&side[4], frame_size - avpkt->duration);
1197 }
1198
1199 411 *got_packet_ptr = 1;
1200 411 return 0;
1201 }
1202
1203
1204 1 static av_cold int vorbis_encode_close(AVCodecContext *avctx)
1205 {
1206 1 vorbis_enc_context *venc = avctx->priv_data;
1207 int i;
1208
1209
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (venc->codebooks)
1210
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 1 times.
30 for (i = 0; i < venc->ncodebooks; i++) {
1211 29 av_freep(&venc->codebooks[i].lens);
1212 29 av_freep(&venc->codebooks[i].codewords);
1213 29 av_freep(&venc->codebooks[i].quantlist);
1214 29 av_freep(&venc->codebooks[i].dimensions);
1215 29 av_freep(&venc->codebooks[i].pow2);
1216 }
1217 1 av_freep(&venc->codebooks);
1218
1219
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (venc->floors)
1220
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 for (i = 0; i < venc->nfloors; i++) {
1221 int j;
1222
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (venc->floors[i].classes)
1223
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
6 for (j = 0; j < venc->floors[i].nclasses; j++)
1224 5 av_freep(&venc->floors[i].classes[j].books);
1225 1 av_freep(&venc->floors[i].classes);
1226 1 av_freep(&venc->floors[i].partition_to_class);
1227 1 av_freep(&venc->floors[i].list);
1228 }
1229 1 av_freep(&venc->floors);
1230
1231
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (venc->residues)
1232
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 for (i = 0; i < venc->nresidues; i++) {
1233 1 av_freep(&venc->residues[i].books);
1234 1 av_freep(&venc->residues[i].maxes);
1235 }
1236 1 av_freep(&venc->residues);
1237
1238
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (venc->mappings)
1239
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 for (i = 0; i < venc->nmappings; i++) {
1240 1 av_freep(&venc->mappings[i].mux);
1241 1 av_freep(&venc->mappings[i].floor);
1242 1 av_freep(&venc->mappings[i].residue);
1243 1 av_freep(&venc->mappings[i].magnitude);
1244 1 av_freep(&venc->mappings[i].angle);
1245 }
1246 1 av_freep(&venc->mappings);
1247
1248 1 av_freep(&venc->modes);
1249
1250 1 av_freep(&venc->saved);
1251 1 av_freep(&venc->samples);
1252 1 av_freep(&venc->floor);
1253 1 av_freep(&venc->coeffs);
1254 1 av_freep(&venc->scratch);
1255 1 av_freep(&venc->fdsp);
1256
1257 1 ff_mdct_end(&venc->mdct[0]);
1258 1 ff_mdct_end(&venc->mdct[1]);
1259 1 ff_af_queue_close(&venc->afq);
1260 1 ff_bufqueue_discard_all(&venc->bufqueue);
1261
1262 1 return 0 ;
1263 }
1264
1265 1 static av_cold int vorbis_encode_init(AVCodecContext *avctx)
1266 {
1267 1 vorbis_enc_context *venc = avctx->priv_data;
1268 int ret;
1269
1270
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (avctx->channels != 2) {
1271 av_log(avctx, AV_LOG_ERROR, "Current FFmpeg Vorbis encoder only supports 2 channels.\n");
1272 return -1;
1273 }
1274
1275
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if ((ret = create_vorbis_context(venc, avctx)) < 0)
1276 goto error;
1277
1278 1 avctx->bit_rate = 0;
1279
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (avctx->flags & AV_CODEC_FLAG_QSCALE)
1280 venc->quality = avctx->global_quality / (float)FF_QP2LAMBDA;
1281 else
1282 1 venc->quality = 8;
1283 1 venc->quality *= venc->quality;
1284
1285
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if ((ret = put_main_header(venc, (uint8_t**)&avctx->extradata)) < 0)
1286 goto error;
1287 1 avctx->extradata_size = ret;
1288
1289 1 avctx->frame_size = 64;
1290 1 avctx->initial_padding = 1 << (venc->log2_blocksize[1] - 1);
1291
1292 1 ff_af_queue_init(avctx, &venc->afq);
1293
1294 1 return 0;
1295 error:
1296 vorbis_encode_close(avctx);
1297 return ret;
1298 }
1299
1300 const AVCodec ff_vorbis_encoder = {
1301 .name = "vorbis",
1302 .long_name = NULL_IF_CONFIG_SMALL("Vorbis"),
1303 .type = AVMEDIA_TYPE_AUDIO,
1304 .id = AV_CODEC_ID_VORBIS,
1305 .priv_data_size = sizeof(vorbis_enc_context),
1306 .init = vorbis_encode_init,
1307 .encode2 = vorbis_encode_frame,
1308 .close = vorbis_encode_close,
1309 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_EXPERIMENTAL,
1310 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLTP,
1311 AV_SAMPLE_FMT_NONE },
1312 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
1313 };
1314