FFmpeg coverage


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