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