2 * copyright (c) 2006 Oded Shimon <ods15@ods15.dyndns.org>
4 * This file is part of FFmpeg.
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.
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.
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
23 * Native Vorbis encoder.
24 * @author Oded Shimon <ods15@ods15.dyndns.org>
28 #include "libavutil/float_dsp.h"
29 #include "libavutil/mem.h"
30 #include "libavutil/tx.h"
33 #include "codec_internal.h"
37 #include "vorbis_data.h"
38 #include "vorbis_enc_data.h"
40 #include "audio_frame_queue.h"
41 #include "libavfilter/bufferqueue.h"
43 #define BITSTREAM_WRITER_LE
49 typedef struct vorbis_enc_codebook
{
61 } vorbis_enc_codebook
;
63 typedef struct vorbis_enc_floor_class
{
68 } vorbis_enc_floor_class
;
70 typedef struct vorbis_enc_floor
{
72 int *partition_to_class
;
74 vorbis_enc_floor_class
*classes
;
78 vorbis_floor1_entry
*list
;
81 typedef struct vorbis_enc_residue
{
92 typedef struct vorbis_enc_mapping
{
100 } vorbis_enc_mapping
;
102 typedef struct vorbis_enc_mode
{
107 typedef struct vorbis_enc_context
{
110 int log2_blocksize
[2];
111 AVTXContext
*mdct
[2];
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
123 struct FFBufQueue bufqueue
;
126 vorbis_enc_codebook
*codebooks
;
129 vorbis_enc_floor
*floors
;
132 vorbis_enc_residue
*residues
;
135 vorbis_enc_mapping
*mappings
;
138 vorbis_enc_mode
*modes
;
142 AVFloatDSPContext
*fdsp
;
143 } vorbis_enc_context
;
145 #define MAX_CHANNELS 2
146 #define MAX_CODEBOOK_DIM 8
148 #define MAX_FLOOR_CLASS_DIM 4
149 #define NUM_FLOOR_PARTITIONS 8
150 #define MAX_FLOOR_VALUES (MAX_FLOOR_CLASS_DIM*NUM_FLOOR_PARTITIONS+2)
152 #define RESIDUE_SIZE 1600
153 #define RESIDUE_PART_SIZE 32
154 #define NUM_RESIDUE_PARTITIONS (RESIDUE_SIZE/RESIDUE_PART_SIZE)
156 static inline int put_codeword(PutBitContext
*pb
, vorbis_enc_codebook
*cb
,
159 av_assert2(entry
>= 0);
160 av_assert2(entry
< cb
->nentries
);
161 av_assert2(cb
->lens
[entry
]);
162 if (put_bits_left(pb
) < cb
->lens
[entry
])
163 return AVERROR(EINVAL
);
164 put_bits(pb
, cb
->lens
[entry
], cb
->codewords
[entry
]);
168 static int cb_lookup_vals(int lookup
, int dimensions
, int entries
)
171 return ff_vorbis_nth_root(entries
, dimensions
);
172 else if (lookup
== 2)
173 return dimensions
*entries
;
177 static int ready_codebook(vorbis_enc_codebook
*cb
)
181 ff_vorbis_len2vlc(cb
->lens
, cb
->codewords
, cb
->nentries
);
184 cb
->pow2
= cb
->dimensions
= NULL
;
186 int vals
= cb_lookup_vals(cb
->lookup
, cb
->ndimensions
, cb
->nentries
);
187 cb
->dimensions
= av_malloc_array(cb
->nentries
, sizeof(float) * cb
->ndimensions
);
188 cb
->pow2
= av_calloc(cb
->nentries
, sizeof(*cb
->pow2
));
189 if (!cb
->dimensions
|| !cb
->pow2
)
190 return AVERROR(ENOMEM
);
191 for (i
= 0; i
< cb
->nentries
; i
++) {
195 for (j
= 0; j
< cb
->ndimensions
; j
++) {
198 off
= (i
/ div
) % vals
; // lookup type 1
200 off
= i
* cb
->ndimensions
+ j
; // lookup type 2
202 cb
->dimensions
[i
* cb
->ndimensions
+ j
] = last
+ cb
->min
+ cb
->quantlist
[off
] * cb
->delta
;
204 last
= cb
->dimensions
[i
* cb
->ndimensions
+ j
];
205 cb
->pow2
[i
] += cb
->dimensions
[i
* cb
->ndimensions
+ j
] * cb
->dimensions
[i
* cb
->ndimensions
+ j
];
214 static int ready_residue(vorbis_enc_residue
*rc
, vorbis_enc_context
*venc
)
217 av_assert0(rc
->type
== 2);
218 rc
->maxes
= av_calloc(rc
->classifications
, sizeof(*rc
->maxes
));
220 return AVERROR(ENOMEM
);
221 for (i
= 0; i
< rc
->classifications
; i
++) {
223 vorbis_enc_codebook
* cb
;
224 for (j
= 0; j
< 8; j
++)
225 if (rc
->books
[i
][j
] != -1)
229 cb
= &venc
->codebooks
[rc
->books
[i
][j
]];
230 assert(cb
->ndimensions
>= 2);
233 for (j
= 0; j
< cb
->nentries
; j
++) {
237 a
= fabs(cb
->dimensions
[j
* cb
->ndimensions
]);
238 if (a
> rc
->maxes
[i
][0])
240 a
= fabs(cb
->dimensions
[j
* cb
->ndimensions
+ 1]);
241 if (a
> rc
->maxes
[i
][1])
246 for (i
= 0; i
< rc
->classifications
; i
++) {
247 rc
->maxes
[i
][0] += 0.8;
248 rc
->maxes
[i
][1] += 0.8;
253 static av_cold
int dsp_init(AVCodecContext
*avctx
, vorbis_enc_context
*venc
)
258 venc
->fdsp
= avpriv_float_dsp_alloc(avctx
->flags
& AV_CODEC_FLAG_BITEXACT
);
260 return AVERROR(ENOMEM
);
263 venc
->win
[0] = ff_vorbis_vwin
[venc
->log2_blocksize
[0] - 6];
264 venc
->win
[1] = ff_vorbis_vwin
[venc
->log2_blocksize
[1] - 6];
266 if ((ret
= av_tx_init(&venc
->mdct
[0], &venc
->mdct_fn
[0], AV_TX_FLOAT_MDCT
,
267 0, 1 << (venc
->log2_blocksize
[0] - 1), &scale
, 0)) < 0)
269 if ((ret
= av_tx_init(&venc
->mdct
[1], &venc
->mdct_fn
[1], AV_TX_FLOAT_MDCT
,
270 0, 1 << (venc
->log2_blocksize
[1] - 1), &scale
, 0)) < 0)
276 static int create_vorbis_context(vorbis_enc_context
*venc
,
277 AVCodecContext
*avctx
)
279 vorbis_enc_floor
*fc
;
280 vorbis_enc_residue
*rc
;
281 vorbis_enc_mapping
*mc
;
282 const uint8_t *clens
, *quant
;
285 venc
->channels
= avctx
->ch_layout
.nb_channels
;
286 venc
->sample_rate
= avctx
->sample_rate
;
287 venc
->log2_blocksize
[0] = venc
->log2_blocksize
[1] = 11;
289 venc
->ncodebooks
= FF_ARRAY_ELEMS(cvectors
);
290 venc
->codebooks
= av_mallocz(sizeof(vorbis_enc_codebook
) * venc
->ncodebooks
);
291 if (!venc
->codebooks
)
292 return AVERROR(ENOMEM
);
294 // codebook 0..14 - floor1 book, values 0..255
295 // codebook 15 residue masterbook
296 // codebook 16..29 residue
298 quant
= quant_tables
;
299 for (book
= 0; book
< venc
->ncodebooks
; book
++) {
300 vorbis_enc_codebook
*cb
= &venc
->codebooks
[book
];
302 cb
->ndimensions
= cvectors
[book
].dim
;
303 cb
->nentries
= cvectors
[book
].real_len
;
304 cb
->min
= cvectors
[book
].min
;
305 cb
->delta
= cvectors
[book
].delta
;
306 cb
->lookup
= cvectors
[book
].lookup
;
309 cb
->lens
= av_malloc_array(cb
->nentries
, sizeof(uint8_t));
310 cb
->codewords
= av_malloc_array(cb
->nentries
, sizeof(uint32_t));
311 if (!cb
->lens
|| !cb
->codewords
)
312 return AVERROR(ENOMEM
);
313 memcpy(cb
->lens
, clens
, cvectors
[book
].len
);
314 memset(cb
->lens
+ cvectors
[book
].len
, 0, cb
->nentries
- cvectors
[book
].len
);
315 clens
+= cvectors
[book
].len
;
318 vals
= cb_lookup_vals(cb
->lookup
, cb
->ndimensions
, cb
->nentries
);
319 cb
->quantlist
= av_malloc_array(vals
, sizeof(int));
321 return AVERROR(ENOMEM
);
322 for (i
= 0; i
< vals
; i
++)
323 cb
->quantlist
[i
] = *quant
++;
325 cb
->quantlist
= NULL
;
327 if ((ret
= ready_codebook(cb
)) < 0)
332 venc
->floors
= av_mallocz(sizeof(vorbis_enc_floor
) * venc
->nfloors
);
334 return AVERROR(ENOMEM
);
337 fc
= &venc
->floors
[0];
338 fc
->partitions
= NUM_FLOOR_PARTITIONS
;
339 fc
->partition_to_class
= av_malloc(sizeof(int) * fc
->partitions
);
340 if (!fc
->partition_to_class
)
341 return AVERROR(ENOMEM
);
343 for (i
= 0; i
< fc
->partitions
; i
++) {
344 static const int a
[] = {0, 1, 2, 2, 3, 3, 4, 4};
345 fc
->partition_to_class
[i
] = a
[i
];
346 fc
->nclasses
= FFMAX(fc
->nclasses
, fc
->partition_to_class
[i
]);
349 fc
->classes
= av_calloc(fc
->nclasses
, sizeof(vorbis_enc_floor_class
));
351 return AVERROR(ENOMEM
);
352 for (i
= 0; i
< fc
->nclasses
; i
++) {
353 vorbis_enc_floor_class
* c
= &fc
->classes
[i
];
355 c
->dim
= floor_classes
[i
].dim
;
356 c
->subclass
= floor_classes
[i
].subclass
;
357 c
->masterbook
= floor_classes
[i
].masterbook
;
358 books
= (1 << c
->subclass
);
359 c
->books
= av_malloc_array(books
, sizeof(int));
361 return AVERROR(ENOMEM
);
362 for (j
= 0; j
< books
; j
++)
363 c
->books
[j
] = floor_classes
[i
].nbooks
[j
];
366 fc
->rangebits
= venc
->log2_blocksize
[1] - 1;
369 for (i
= 0; i
< fc
->partitions
; i
++)
370 fc
->values
+= fc
->classes
[fc
->partition_to_class
[i
]].dim
;
372 fc
->list
= av_malloc_array(fc
->values
, sizeof(vorbis_floor1_entry
));
374 return AVERROR(ENOMEM
);
376 fc
->list
[1].x
= 1 << fc
->rangebits
;
377 for (i
= 2; i
< fc
->values
; i
++) {
378 static const int a
[] = {
379 93, 23,372, 6, 46,186,750, 14, 33, 65,
380 130,260,556, 3, 10, 18, 28, 39, 55, 79,
381 111,158,220,312,464,650,850
383 fc
->list
[i
].x
= a
[i
- 2];
385 if (ff_vorbis_ready_floor1_list(avctx
, fc
->list
, fc
->values
))
389 venc
->residues
= av_mallocz(sizeof(vorbis_enc_residue
) * venc
->nresidues
);
391 return AVERROR(ENOMEM
);
394 rc
= &venc
->residues
[0];
398 rc
->partition_size
= 32;
399 rc
->classifications
= 10;
401 rc
->books
= av_malloc(sizeof(*rc
->books
) * rc
->classifications
);
403 return AVERROR(ENOMEM
);
405 static const int8_t a
[10][8] = {
406 { -1, -1, -1, -1, -1, -1, -1, -1, },
407 { -1, -1, 16, -1, -1, -1, -1, -1, },
408 { -1, -1, 17, -1, -1, -1, -1, -1, },
409 { -1, -1, 18, -1, -1, -1, -1, -1, },
410 { -1, -1, 19, -1, -1, -1, -1, -1, },
411 { -1, -1, 20, -1, -1, -1, -1, -1, },
412 { -1, -1, 21, -1, -1, -1, -1, -1, },
413 { 22, 23, -1, -1, -1, -1, -1, -1, },
414 { 24, 25, -1, -1, -1, -1, -1, -1, },
415 { 26, 27, 28, -1, -1, -1, -1, -1, },
417 memcpy(rc
->books
, a
, sizeof a
);
419 if ((ret
= ready_residue(rc
, venc
)) < 0)
423 venc
->mappings
= av_mallocz(sizeof(vorbis_enc_mapping
) * venc
->nmappings
);
425 return AVERROR(ENOMEM
);
428 mc
= &venc
->mappings
[0];
430 mc
->mux
= av_malloc(sizeof(int) * venc
->channels
);
432 return AVERROR(ENOMEM
);
433 for (i
= 0; i
< venc
->channels
; i
++)
435 mc
->floor
= av_malloc(sizeof(int) * mc
->submaps
);
436 mc
->residue
= av_malloc(sizeof(int) * mc
->submaps
);
437 if (!mc
->floor
|| !mc
->residue
)
438 return AVERROR(ENOMEM
);
439 for (i
= 0; i
< mc
->submaps
; i
++) {
443 mc
->coupling_steps
= venc
->channels
== 2 ? 1 : 0;
444 mc
->magnitude
= av_malloc(sizeof(int) * mc
->coupling_steps
);
445 mc
->angle
= av_malloc(sizeof(int) * mc
->coupling_steps
);
446 if (!mc
->magnitude
|| !mc
->angle
)
447 return AVERROR(ENOMEM
);
448 if (mc
->coupling_steps
) {
449 mc
->magnitude
[0] = 0;
454 venc
->modes
= av_malloc(sizeof(vorbis_enc_mode
) * venc
->nmodes
);
456 return AVERROR(ENOMEM
);
459 venc
->modes
[0].blockflag
= 0;
460 venc
->modes
[0].mapping
= 0;
462 venc
->modes
[1].blockflag
= 1;
463 venc
->modes
[1].mapping
= 0;
465 venc
->have_saved
= 0;
466 venc
->saved
= av_malloc_array(sizeof(float) * venc
->channels
, (1 << venc
->log2_blocksize
[1]) / 2);
467 venc
->samples
= av_malloc_array(sizeof(float) * venc
->channels
, (1 << venc
->log2_blocksize
[1]));
468 venc
->floor
= av_malloc_array(sizeof(float) * venc
->channels
, (1 << venc
->log2_blocksize
[1]) / 2);
469 venc
->coeffs
= av_malloc_array(sizeof(float) * venc
->channels
, (1 << venc
->log2_blocksize
[1]) / 2);
470 venc
->scratch
= av_malloc_array(sizeof(float) * venc
->channels
, (1 << venc
->log2_blocksize
[1]));
472 if (!venc
->saved
|| !venc
->samples
|| !venc
->floor
|| !venc
->coeffs
|| !venc
->scratch
)
473 return AVERROR(ENOMEM
);
475 if ((ret
= dsp_init(avctx
, venc
)) < 0)
481 static void put_float(PutBitContext
*pb
, float f
)
485 mant
= (int)ldexp(frexp(f
, &exp
), 20);
491 res
|= mant
| (exp
<< 21);
495 static void put_codebook_header(PutBitContext
*pb
, vorbis_enc_codebook
*cb
)
500 put_bits(pb
, 24, 0x564342); //magic
501 put_bits(pb
, 16, cb
->ndimensions
);
502 put_bits(pb
, 24, cb
->nentries
);
504 for (i
= 1; i
< cb
->nentries
; i
++)
505 if (cb
->lens
[i
] < cb
->lens
[i
-1])
507 if (i
== cb
->nentries
)
510 put_bits(pb
, 1, ordered
);
512 int len
= cb
->lens
[0];
513 put_bits(pb
, 5, len
- 1);
515 while (i
< cb
->nentries
) {
517 for (j
= 0; j
+i
< cb
->nentries
; j
++)
518 if (cb
->lens
[j
+i
] != len
)
520 put_bits(pb
, ilog(cb
->nentries
- i
), j
);
526 for (i
= 0; i
< cb
->nentries
; i
++)
529 if (i
!= cb
->nentries
)
531 put_bits(pb
, 1, sparse
);
533 for (i
= 0; i
< cb
->nentries
; i
++) {
535 put_bits(pb
, 1, !!cb
->lens
[i
]);
537 put_bits(pb
, 5, cb
->lens
[i
] - 1);
541 put_bits(pb
, 4, cb
->lookup
);
543 int tmp
= cb_lookup_vals(cb
->lookup
, cb
->ndimensions
, cb
->nentries
);
544 int bits
= ilog(cb
->quantlist
[0]);
546 for (i
= 1; i
< tmp
; i
++)
547 bits
= FFMAX(bits
, ilog(cb
->quantlist
[i
]));
549 put_float(pb
, cb
->min
);
550 put_float(pb
, cb
->delta
);
552 put_bits(pb
, 4, bits
- 1);
553 put_bits(pb
, 1, cb
->seq_p
);
555 for (i
= 0; i
< tmp
; i
++)
556 put_bits(pb
, bits
, cb
->quantlist
[i
]);
560 static void put_floor_header(PutBitContext
*pb
, vorbis_enc_floor
*fc
)
564 put_bits(pb
, 16, 1); // type, only floor1 is supported
566 put_bits(pb
, 5, fc
->partitions
);
568 for (i
= 0; i
< fc
->partitions
; i
++)
569 put_bits(pb
, 4, fc
->partition_to_class
[i
]);
571 for (i
= 0; i
< fc
->nclasses
; i
++) {
574 put_bits(pb
, 3, fc
->classes
[i
].dim
- 1);
575 put_bits(pb
, 2, fc
->classes
[i
].subclass
);
577 if (fc
->classes
[i
].subclass
)
578 put_bits(pb
, 8, fc
->classes
[i
].masterbook
);
580 books
= (1 << fc
->classes
[i
].subclass
);
582 for (j
= 0; j
< books
; j
++)
583 put_bits(pb
, 8, fc
->classes
[i
].books
[j
] + 1);
586 put_bits(pb
, 2, fc
->multiplier
- 1);
587 put_bits(pb
, 4, fc
->rangebits
);
589 for (i
= 2; i
< fc
->values
; i
++)
590 put_bits(pb
, fc
->rangebits
, fc
->list
[i
].x
);
593 static void put_residue_header(PutBitContext
*pb
, vorbis_enc_residue
*rc
)
597 put_bits(pb
, 16, rc
->type
);
599 put_bits(pb
, 24, rc
->begin
);
600 put_bits(pb
, 24, rc
->end
);
601 put_bits(pb
, 24, rc
->partition_size
- 1);
602 put_bits(pb
, 6, rc
->classifications
- 1);
603 put_bits(pb
, 8, rc
->classbook
);
605 for (i
= 0; i
< rc
->classifications
; i
++) {
607 for (j
= 0; j
< 8; j
++)
608 tmp
|= (rc
->books
[i
][j
] != -1) << j
;
610 put_bits(pb
, 3, tmp
& 7);
611 put_bits(pb
, 1, tmp
> 7);
614 put_bits(pb
, 5, tmp
>> 3);
617 for (i
= 0; i
< rc
->classifications
; i
++) {
619 for (j
= 0; j
< 8; j
++)
620 if (rc
->books
[i
][j
] != -1)
621 put_bits(pb
, 8, rc
->books
[i
][j
]);
625 static int put_main_header(vorbis_enc_context
*venc
, uint8_t **out
)
630 int buffer_len
= 50000;
631 uint8_t *buffer
= av_mallocz(buffer_len
), *p
= buffer
;
633 return AVERROR(ENOMEM
);
635 // identification header
636 init_put_bits(&pb
, p
, buffer_len
);
637 put_bits(&pb
, 8, 1); //magic
638 for (i
= 0; "vorbis"[i
]; i
++)
639 put_bits(&pb
, 8, "vorbis"[i
]);
640 put_bits32(&pb
, 0); // version
641 put_bits(&pb
, 8, venc
->channels
);
642 put_bits32(&pb
, venc
->sample_rate
);
643 put_bits32(&pb
, 0); // bitrate
644 put_bits32(&pb
, 0); // bitrate
645 put_bits32(&pb
, 0); // bitrate
646 put_bits(&pb
, 4, venc
->log2_blocksize
[0]);
647 put_bits(&pb
, 4, venc
->log2_blocksize
[1]);
648 put_bits(&pb
, 1, 1); // framing
651 hlens
[0] = put_bytes_output(&pb
);
652 buffer_len
-= hlens
[0];
656 init_put_bits(&pb
, p
, buffer_len
);
657 put_bits(&pb
, 8, 3); //magic
658 for (i
= 0; "vorbis"[i
]; i
++)
659 put_bits(&pb
, 8, "vorbis"[i
]);
660 put_bits32(&pb
, 0); // vendor length TODO
661 put_bits32(&pb
, 0); // amount of comments
662 put_bits(&pb
, 1, 1); // framing
665 hlens
[1] = put_bytes_output(&pb
);
666 buffer_len
-= hlens
[1];
670 init_put_bits(&pb
, p
, buffer_len
);
671 put_bits(&pb
, 8, 5); //magic
672 for (i
= 0; "vorbis"[i
]; i
++)
673 put_bits(&pb
, 8, "vorbis"[i
]);
676 put_bits(&pb
, 8, venc
->ncodebooks
- 1);
677 for (i
= 0; i
< venc
->ncodebooks
; i
++)
678 put_codebook_header(&pb
, &venc
->codebooks
[i
]);
680 // time domain, reserved, zero
682 put_bits(&pb
, 16, 0);
685 put_bits(&pb
, 6, venc
->nfloors
- 1);
686 for (i
= 0; i
< venc
->nfloors
; i
++)
687 put_floor_header(&pb
, &venc
->floors
[i
]);
690 put_bits(&pb
, 6, venc
->nresidues
- 1);
691 for (i
= 0; i
< venc
->nresidues
; i
++)
692 put_residue_header(&pb
, &venc
->residues
[i
]);
695 put_bits(&pb
, 6, venc
->nmappings
- 1);
696 for (i
= 0; i
< venc
->nmappings
; i
++) {
697 vorbis_enc_mapping
*mc
= &venc
->mappings
[i
];
699 put_bits(&pb
, 16, 0); // mapping type
701 put_bits(&pb
, 1, mc
->submaps
> 1);
703 put_bits(&pb
, 4, mc
->submaps
- 1);
705 put_bits(&pb
, 1, !!mc
->coupling_steps
);
706 if (mc
->coupling_steps
) {
707 put_bits(&pb
, 8, mc
->coupling_steps
- 1);
708 for (j
= 0; j
< mc
->coupling_steps
; j
++) {
709 put_bits(&pb
, ilog(venc
->channels
- 1), mc
->magnitude
[j
]);
710 put_bits(&pb
, ilog(venc
->channels
- 1), mc
->angle
[j
]);
714 put_bits(&pb
, 2, 0); // reserved
717 for (j
= 0; j
< venc
->channels
; j
++)
718 put_bits(&pb
, 4, mc
->mux
[j
]);
720 for (j
= 0; j
< mc
->submaps
; j
++) {
721 put_bits(&pb
, 8, 0); // reserved time configuration
722 put_bits(&pb
, 8, mc
->floor
[j
]);
723 put_bits(&pb
, 8, mc
->residue
[j
]);
728 put_bits(&pb
, 6, venc
->nmodes
- 1);
729 for (i
= 0; i
< venc
->nmodes
; i
++) {
730 put_bits(&pb
, 1, venc
->modes
[i
].blockflag
);
731 put_bits(&pb
, 16, 0); // reserved window type
732 put_bits(&pb
, 16, 0); // reserved transform type
733 put_bits(&pb
, 8, venc
->modes
[i
].mapping
);
736 put_bits(&pb
, 1, 1); // framing
739 hlens
[2] = put_bytes_output(&pb
);
741 len
= hlens
[0] + hlens
[1] + hlens
[2];
742 p
= *out
= av_mallocz(64 + len
+ len
/255);
744 return AVERROR(ENOMEM
);
747 p
+= av_xiphlacing(p
, hlens
[0]);
748 p
+= av_xiphlacing(p
, hlens
[1]);
750 for (i
= 0; i
< 3; i
++) {
751 memcpy(p
, buffer
+ buffer_len
, hlens
[i
]);
753 buffer_len
+= hlens
[i
];
760 static float get_floor_average(vorbis_enc_floor
* fc
, float *coeffs
, int i
)
762 int begin
= fc
->list
[fc
->list
[FFMAX(i
-1, 0)].sort
].x
;
763 int end
= fc
->list
[fc
->list
[FFMIN(i
+1, fc
->values
- 1)].sort
].x
;
767 for (j
= begin
; j
< end
; j
++)
768 average
+= fabs(coeffs
[j
]);
769 return average
/ (end
- begin
);
772 static void floor_fit(vorbis_enc_context
*venc
, vorbis_enc_floor
*fc
,
773 float *coeffs
, uint16_t *posts
, int samples
)
775 int range
= 255 / fc
->multiplier
+ 1;
777 float tot_average
= 0.0;
778 float averages
[MAX_FLOOR_VALUES
];
779 for (i
= 0; i
< fc
->values
; i
++) {
780 averages
[i
] = get_floor_average(fc
, coeffs
, i
);
781 tot_average
+= averages
[i
];
783 tot_average
/= fc
->values
;
784 tot_average
/= venc
->quality
;
786 for (i
= 0; i
< fc
->values
; i
++) {
787 int position
= fc
->list
[fc
->list
[i
].sort
].x
;
788 float average
= averages
[i
];
791 average
= sqrt(tot_average
* average
) * pow(1.25f
, position
*0.005f
); // MAGIC!
792 for (j
= 0; j
< range
- 1; j
++)
793 if (ff_vorbis_floor1_inverse_db_table
[j
* fc
->multiplier
] > average
)
795 posts
[fc
->list
[i
].sort
] = j
;
799 static int render_point(int x0
, int y0
, int x1
, int y1
, int x
)
801 return y0
+ (x
- x0
) * (y1
- y0
) / (x1
- x0
);
804 static int floor_encode(vorbis_enc_context
*venc
, vorbis_enc_floor
*fc
,
805 PutBitContext
*pb
, uint16_t *posts
,
806 float *floor
, int samples
)
808 int range
= 255 / fc
->multiplier
+ 1;
809 int coded
[MAX_FLOOR_VALUES
]; // first 2 values are unused
812 if (put_bits_left(pb
) < 1 + 2 * ilog(range
- 1))
813 return AVERROR(EINVAL
);
814 put_bits(pb
, 1, 1); // non zero
815 put_bits(pb
, ilog(range
- 1), posts
[0]);
816 put_bits(pb
, ilog(range
- 1), posts
[1]);
817 coded
[0] = coded
[1] = 1;
819 for (i
= 2; i
< fc
->values
; i
++) {
820 int predicted
= render_point(fc
->list
[fc
->list
[i
].low
].x
,
821 posts
[fc
->list
[i
].low
],
822 fc
->list
[fc
->list
[i
].high
].x
,
823 posts
[fc
->list
[i
].high
],
825 int highroom
= range
- predicted
;
826 int lowroom
= predicted
;
827 int room
= FFMIN(highroom
, lowroom
);
828 if (predicted
== posts
[i
]) {
829 coded
[i
] = 0; // must be used later as flag!
832 if (!coded
[fc
->list
[i
].low
])
833 coded
[fc
->list
[i
].low
] = -1;
834 if (!coded
[fc
->list
[i
].high
])
835 coded
[fc
->list
[i
].high
] = -1;
837 if (posts
[i
] > predicted
) {
838 if (posts
[i
] - predicted
> room
)
839 coded
[i
] = posts
[i
] - predicted
+ lowroom
;
841 coded
[i
] = (posts
[i
] - predicted
) << 1;
843 if (predicted
- posts
[i
] > room
)
844 coded
[i
] = predicted
- posts
[i
] + highroom
- 1;
846 coded
[i
] = ((predicted
- posts
[i
]) << 1) - 1;
851 for (i
= 0; i
< fc
->partitions
; i
++) {
852 vorbis_enc_floor_class
* c
= &fc
->classes
[fc
->partition_to_class
[i
]];
853 int k
, cval
= 0, csub
= 1<<c
->subclass
;
855 vorbis_enc_codebook
* book
= &venc
->codebooks
[c
->masterbook
];
857 for (k
= 0; k
< c
->dim
; k
++) {
859 for (l
= 0; l
< csub
; l
++) {
861 if (c
->books
[l
] != -1)
862 maxval
= venc
->codebooks
[c
->books
[l
]].nentries
;
863 // coded could be -1, but this still works, cause that is 0
864 if (coded
[counter
+ k
] < maxval
)
869 cshift
+= c
->subclass
;
871 if (put_codeword(pb
, book
, cval
))
872 return AVERROR(EINVAL
);
874 for (k
= 0; k
< c
->dim
; k
++) {
875 int book
= c
->books
[cval
& (csub
-1)];
876 int entry
= coded
[counter
++];
877 cval
>>= c
->subclass
;
882 if (put_codeword(pb
, &venc
->codebooks
[book
], entry
))
883 return AVERROR(EINVAL
);
887 ff_vorbis_floor1_render_list(fc
->list
, fc
->values
, posts
, coded
,
888 fc
->multiplier
, floor
, samples
);
893 static float *put_vector(vorbis_enc_codebook
*book
, PutBitContext
*pb
,
897 float distance
= FLT_MAX
;
898 assert(book
->dimensions
);
899 for (i
= 0; i
< book
->nentries
; i
++) {
900 float * vec
= book
->dimensions
+ i
* book
->ndimensions
, d
= book
->pow2
[i
];
904 for (j
= 0; j
< book
->ndimensions
; j
++)
905 d
-= vec
[j
] * num
[j
];
911 if (put_codeword(pb
, book
, entry
))
913 return &book
->dimensions
[entry
* book
->ndimensions
];
916 static int residue_encode(vorbis_enc_context
*venc
, vorbis_enc_residue
*rc
,
917 PutBitContext
*pb
, float *coeffs
, int samples
,
920 int pass
, i
, j
, p
, k
;
921 int psize
= rc
->partition_size
;
922 int partitions
= (rc
->end
- rc
->begin
) / psize
;
923 int channels
= (rc
->type
== 2) ? 1 : real_ch
;
924 int classes
[MAX_CHANNELS
][NUM_RESIDUE_PARTITIONS
];
925 int classwords
= venc
->codebooks
[rc
->classbook
].ndimensions
;
927 av_assert0(rc
->type
== 2);
928 av_assert0(real_ch
== 2);
929 for (p
= 0; p
< partitions
; p
++) {
930 float max1
= 0.0, max2
= 0.0;
931 int s
= rc
->begin
+ p
* psize
;
932 for (k
= s
; k
< s
+ psize
; k
+= 2) {
933 max1
= FFMAX(max1
, fabs(coeffs
[ k
/ real_ch
]));
934 max2
= FFMAX(max2
, fabs(coeffs
[samples
+ k
/ real_ch
]));
937 for (i
= 0; i
< rc
->classifications
- 1; i
++)
938 if (max1
< rc
->maxes
[i
][0] && max2
< rc
->maxes
[i
][1])
943 for (pass
= 0; pass
< 8; pass
++) {
945 while (p
< partitions
) {
947 for (j
= 0; j
< channels
; j
++) {
948 vorbis_enc_codebook
* book
= &venc
->codebooks
[rc
->classbook
];
950 for (i
= 0; i
< classwords
; i
++) {
951 entry
*= rc
->classifications
;
952 entry
+= classes
[j
][p
+ i
];
954 if (put_codeword(pb
, book
, entry
))
955 return AVERROR(EINVAL
);
957 for (i
= 0; i
< classwords
&& p
< partitions
; i
++, p
++) {
958 for (j
= 0; j
< channels
; j
++) {
959 int nbook
= rc
->books
[classes
[j
][p
]][pass
];
960 vorbis_enc_codebook
* book
= &venc
->codebooks
[nbook
];
961 float *buf
= coeffs
+ samples
*j
+ rc
->begin
+ p
*psize
;
965 assert(rc
->type
== 0 || rc
->type
== 2);
966 assert(!(psize
% book
->ndimensions
));
969 for (k
= 0; k
< psize
; k
+= book
->ndimensions
) {
971 float *a
= put_vector(book
, pb
, &buf
[k
]);
973 return AVERROR(EINVAL
);
974 for (l
= 0; l
< book
->ndimensions
; l
++)
978 int s
= rc
->begin
+ p
* psize
, a1
, b1
;
979 a1
= (s
% real_ch
) * samples
;
981 s
= real_ch
* samples
;
982 for (k
= 0; k
< psize
; k
+= book
->ndimensions
) {
983 int dim
, a2
= a1
, b2
= b1
;
984 float vec
[MAX_CODEBOOK_DIM
], *pv
= vec
;
985 for (dim
= book
->ndimensions
; dim
--; ) {
986 *pv
++ = coeffs
[a2
+ b2
];
987 if ((a2
+= samples
) == s
) {
992 pv
= put_vector(book
, pb
, vec
);
994 return AVERROR(EINVAL
);
995 for (dim
= book
->ndimensions
; dim
--; ) {
996 coeffs
[a1
+ b1
] -= *pv
++;
997 if ((a1
+= samples
) == s
) {
1011 static int apply_window_and_mdct(vorbis_enc_context
*venc
)
1014 const float * win
= venc
->win
[1];
1015 int window_len
= 1 << (venc
->log2_blocksize
[1] - 1);
1016 float n
= (float)(1 << venc
->log2_blocksize
[1]) / 4.0;
1017 AVFloatDSPContext
*fdsp
= venc
->fdsp
;
1019 for (channel
= 0; channel
< venc
->channels
; channel
++) {
1020 float *offset
= venc
->samples
+ channel
* window_len
* 2;
1022 fdsp
->vector_fmul(offset
, offset
, win
, window_len
);
1023 fdsp
->vector_fmul_scalar(offset
, offset
, 1/n
, window_len
);
1025 offset
+= window_len
;
1027 fdsp
->vector_fmul_reverse(offset
, offset
, win
, window_len
);
1028 fdsp
->vector_fmul_scalar(offset
, offset
, 1/n
, window_len
);
1030 venc
->mdct_fn
[1](venc
->mdct
[1], venc
->coeffs
+ channel
* window_len
,
1031 venc
->samples
+ channel
* window_len
* 2, sizeof(float));
1036 /* Used for padding the last encoded packet */
1037 static AVFrame
*spawn_empty_frame(AVCodecContext
*avctx
, int channels
)
1039 AVFrame
*f
= av_frame_alloc();
1045 f
->format
= avctx
->sample_fmt
;
1046 f
->nb_samples
= avctx
->frame_size
;
1047 f
->ch_layout
.order
= AV_CHANNEL_ORDER_UNSPEC
;
1048 f
->ch_layout
.nb_channels
= channels
;
1050 if (av_frame_get_buffer(f
, 4)) {
1055 for (ch
= 0; ch
< channels
; ch
++) {
1056 size_t bps
= av_get_bytes_per_sample(f
->format
);
1057 memset(f
->extended_data
[ch
], 0, bps
* f
->nb_samples
);
1062 /* Set up audio samples for psy analysis and window/mdct */
1063 static void move_audio(vorbis_enc_context
*venc
, int sf_size
)
1065 AVFrame
*cur
= NULL
;
1066 int frame_size
= 1 << (venc
->log2_blocksize
[1] - 1);
1067 int subframes
= frame_size
/ sf_size
;
1070 /* Copy samples from last frame into current frame */
1071 if (venc
->have_saved
)
1072 for (ch
= 0; ch
< venc
->channels
; ch
++)
1073 memcpy(venc
->samples
+ 2 * ch
* frame_size
,
1074 venc
->saved
+ ch
* frame_size
, sizeof(float) * frame_size
);
1076 for (ch
= 0; ch
< venc
->channels
; ch
++)
1077 memset(venc
->samples
+ 2 * ch
* frame_size
, 0, sizeof(float) * frame_size
);
1079 for (sf
= 0; sf
< subframes
; sf
++) {
1080 cur
= ff_bufqueue_get(&venc
->bufqueue
);
1082 for (ch
= 0; ch
< venc
->channels
; ch
++) {
1083 float *offset
= venc
->samples
+ 2 * ch
* frame_size
+ frame_size
;
1084 float *save
= venc
->saved
+ ch
* frame_size
;
1085 const float *input
= (float *) cur
->extended_data
[ch
];
1086 const size_t len
= cur
->nb_samples
* sizeof(float);
1088 memcpy(offset
+ sf
*sf_size
, input
, len
);
1089 memcpy(save
+ sf
*sf_size
, input
, len
); // Move samples for next frame
1091 av_frame_free(&cur
);
1093 venc
->have_saved
= 1;
1094 memcpy(venc
->scratch
, venc
->samples
, 2 * venc
->channels
* frame_size
);
1097 static int vorbis_encode_frame(AVCodecContext
*avctx
, AVPacket
*avpkt
,
1098 const AVFrame
*frame
, int *got_packet_ptr
)
1100 vorbis_enc_context
*venc
= avctx
->priv_data
;
1101 int i
, ret
, need_more
;
1102 int frame_size
= 1 << (venc
->log2_blocksize
[1] - 1);
1103 vorbis_enc_mode
*mode
;
1104 vorbis_enc_mapping
*mapping
;
1109 if ((ret
= ff_af_queue_add(&venc
->afq
, frame
)) < 0)
1111 clone
= av_frame_clone(frame
);
1113 return AVERROR(ENOMEM
);
1114 ff_bufqueue_add(avctx
, &venc
->bufqueue
, clone
);
1116 if (!venc
->afq
.remaining_samples
)
1119 need_more
= venc
->bufqueue
.available
* avctx
->frame_size
< frame_size
;
1120 need_more
= frame
&& need_more
;
1124 /* Pad the bufqueue with empty frames for encoding the last packet. */
1126 if (venc
->bufqueue
.available
* avctx
->frame_size
< frame_size
) {
1127 int frames_needed
= (frame_size
/avctx
->frame_size
) - venc
->bufqueue
.available
;
1130 for (i
= 0; i
< frames_needed
; i
++) {
1131 AVFrame
*empty
= spawn_empty_frame(avctx
, venc
->channels
);
1133 return AVERROR(ENOMEM
);
1135 ff_bufqueue_add(avctx
, &venc
->bufqueue
, empty
);
1140 move_audio(venc
, avctx
->frame_size
);
1142 if (!apply_window_and_mdct(venc
))
1145 if ((ret
= ff_alloc_packet(avctx
, avpkt
, 8192)) < 0)
1148 init_put_bits(&pb
, avpkt
->data
, avpkt
->size
);
1150 put_bits(&pb
, 1, 0); // magic bit
1152 put_bits(&pb
, ilog(venc
->nmodes
- 1), 1); // Mode for current frame
1154 mode
= &venc
->modes
[1];
1155 mapping
= &venc
->mappings
[mode
->mapping
];
1156 if (mode
->blockflag
) {
1157 put_bits(&pb
, 1, 1); // Previous windowflag
1158 put_bits(&pb
, 1, 1); // Next windowflag
1161 for (i
= 0; i
< venc
->channels
; i
++) {
1162 vorbis_enc_floor
*fc
= &venc
->floors
[mapping
->floor
[mapping
->mux
[i
]]];
1163 uint16_t posts
[MAX_FLOOR_VALUES
];
1164 floor_fit(venc
, fc
, &venc
->coeffs
[i
* frame_size
], posts
, frame_size
);
1165 if (floor_encode(venc
, fc
, &pb
, posts
, &venc
->floor
[i
* frame_size
], frame_size
)) {
1166 av_log(avctx
, AV_LOG_ERROR
, "output buffer is too small\n");
1167 return AVERROR(EINVAL
);
1171 for (i
= 0; i
< venc
->channels
* frame_size
; i
++)
1172 venc
->coeffs
[i
] /= venc
->floor
[i
];
1174 for (i
= 0; i
< mapping
->coupling_steps
; i
++) {
1175 float *mag
= venc
->coeffs
+ mapping
->magnitude
[i
] * frame_size
;
1176 float *ang
= venc
->coeffs
+ mapping
->angle
[i
] * frame_size
;
1178 for (j
= 0; j
< frame_size
; j
++) {
1188 if (residue_encode(venc
, &venc
->residues
[mapping
->residue
[mapping
->mux
[0]]],
1189 &pb
, venc
->coeffs
, frame_size
, venc
->channels
)) {
1190 av_log(avctx
, AV_LOG_ERROR
, "output buffer is too small\n");
1191 return AVERROR(EINVAL
);
1194 flush_put_bits(&pb
);
1195 avpkt
->size
= put_bytes_output(&pb
);
1197 ff_af_queue_remove(&venc
->afq
, frame_size
, &avpkt
->pts
, &avpkt
->duration
);
1199 if (frame_size
> avpkt
->duration
) {
1200 uint8_t *side
= av_packet_new_side_data(avpkt
, AV_PKT_DATA_SKIP_SAMPLES
, 10);
1202 return AVERROR(ENOMEM
);
1203 AV_WL32(&side
[4], frame_size
- avpkt
->duration
);
1206 *got_packet_ptr
= 1;
1211 static av_cold
int vorbis_encode_close(AVCodecContext
*avctx
)
1213 vorbis_enc_context
*venc
= avctx
->priv_data
;
1216 if (venc
->codebooks
)
1217 for (i
= 0; i
< venc
->ncodebooks
; i
++) {
1218 av_freep(&venc
->codebooks
[i
].lens
);
1219 av_freep(&venc
->codebooks
[i
].codewords
);
1220 av_freep(&venc
->codebooks
[i
].quantlist
);
1221 av_freep(&venc
->codebooks
[i
].dimensions
);
1222 av_freep(&venc
->codebooks
[i
].pow2
);
1224 av_freep(&venc
->codebooks
);
1227 for (i
= 0; i
< venc
->nfloors
; i
++) {
1229 if (venc
->floors
[i
].classes
)
1230 for (j
= 0; j
< venc
->floors
[i
].nclasses
; j
++)
1231 av_freep(&venc
->floors
[i
].classes
[j
].books
);
1232 av_freep(&venc
->floors
[i
].classes
);
1233 av_freep(&venc
->floors
[i
].partition_to_class
);
1234 av_freep(&venc
->floors
[i
].list
);
1236 av_freep(&venc
->floors
);
1239 for (i
= 0; i
< venc
->nresidues
; i
++) {
1240 av_freep(&venc
->residues
[i
].books
);
1241 av_freep(&venc
->residues
[i
].maxes
);
1243 av_freep(&venc
->residues
);
1246 for (i
= 0; i
< venc
->nmappings
; i
++) {
1247 av_freep(&venc
->mappings
[i
].mux
);
1248 av_freep(&venc
->mappings
[i
].floor
);
1249 av_freep(&venc
->mappings
[i
].residue
);
1250 av_freep(&venc
->mappings
[i
].magnitude
);
1251 av_freep(&venc
->mappings
[i
].angle
);
1253 av_freep(&venc
->mappings
);
1255 av_freep(&venc
->modes
);
1257 av_freep(&venc
->saved
);
1258 av_freep(&venc
->samples
);
1259 av_freep(&venc
->floor
);
1260 av_freep(&venc
->coeffs
);
1261 av_freep(&venc
->scratch
);
1262 av_freep(&venc
->fdsp
);
1264 av_tx_uninit(&venc
->mdct
[0]);
1265 av_tx_uninit(&venc
->mdct
[1]);
1266 ff_af_queue_close(&venc
->afq
);
1267 ff_bufqueue_discard_all(&venc
->bufqueue
);
1272 static av_cold
int vorbis_encode_init(AVCodecContext
*avctx
)
1274 vorbis_enc_context
*venc
= avctx
->priv_data
;
1277 if (avctx
->ch_layout
.nb_channels
!= 2) {
1278 av_log(avctx
, AV_LOG_ERROR
, "Current FFmpeg Vorbis encoder only supports 2 channels.\n");
1282 if ((ret
= create_vorbis_context(venc
, avctx
)) < 0)
1285 avctx
->bit_rate
= 0;
1286 if (avctx
->flags
& AV_CODEC_FLAG_QSCALE
)
1287 venc
->quality
= avctx
->global_quality
/ (float)FF_QP2LAMBDA
;
1290 venc
->quality
*= venc
->quality
;
1292 if ((ret
= put_main_header(venc
, (uint8_t**)&avctx
->extradata
)) < 0)
1294 avctx
->extradata_size
= ret
;
1296 avctx
->frame_size
= 64;
1297 avctx
->initial_padding
= 1 << (venc
->log2_blocksize
[1] - 1);
1299 ff_af_queue_init(avctx
, &venc
->afq
);
1304 const FFCodec ff_vorbis_encoder
= {
1306 CODEC_LONG_NAME("Vorbis"),
1307 .p
.type
= AVMEDIA_TYPE_AUDIO
,
1308 .p
.id
= AV_CODEC_ID_VORBIS
,
1309 .p
.capabilities
= AV_CODEC_CAP_DR1
| AV_CODEC_CAP_DELAY
|
1310 AV_CODEC_CAP_EXPERIMENTAL
,
1311 .priv_data_size
= sizeof(vorbis_enc_context
),
1312 .init
= vorbis_encode_init
,
1313 FF_CODEC_ENCODE_CB(vorbis_encode_frame
),
1314 .close
= vorbis_encode_close
,
1315 .p
.sample_fmts
= (const enum AVSampleFormat
[]){ AV_SAMPLE_FMT_FLTP
,
1316 AV_SAMPLE_FMT_NONE
},
1317 .caps_internal
= FF_CODEC_CAP_INIT_CLEANUP
,