43#define BITSTREAM_WRITER_LE
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)
173 return dimensions *entries;
189 if (!
cb->dimensions || !
cb->pow2)
191 for (
i = 0;
i <
cb->nentries;
i++) {
195 for (j = 0; j <
cb->ndimensions; j++) {
198 off = (
i / div) % vals;
200 off =
i *
cb->ndimensions + j;
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];
224 for (j = 0; j < 8; j++)
225 if (rc->
books[
i][j] != -1)
230 assert(
cb->ndimensions >= 2);
233 for (j = 0; j <
cb->nentries; j++) {
237 a =
fabs(
cb->dimensions[j *
cb->ndimensions]);
240 a =
fabs(
cb->dimensions[j *
cb->ndimensions + 1]);
282 const uint8_t *clens, *
quant;
299 for (book = 0; book < venc->
ncodebooks; book++) {
311 if (!
cb->lens || !
cb->codewords)
322 for (
i = 0;
i < vals;
i++)
339 fc->partition_to_class =
av_malloc(
sizeof(
int) *
fc->partitions);
340 if (!
fc->partition_to_class)
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]);
352 for (
i = 0;
i <
fc->nclasses;
i++) {
358 books = (1 <<
c->subclass);
362 for (j = 0; j < books; j++)
369 for (
i = 0;
i <
fc->partitions;
i++)
370 fc->values +=
fc->classes[
fc->partition_to_class[
i]].dim;
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];
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);
437 if (!
mc->floor || !
mc->residue)
439 for (
i = 0;
i <
mc->submaps;
i++) {
443 mc->coupling_steps = venc->
channels == 2 ? 1 : 0;
446 if (!
mc->magnitude || !
mc->angle)
448 if (
mc->coupling_steps) {
449 mc->magnitude[0] = 0;
475 if ((ret =
dsp_init(avctx, venc)) < 0)
485 mant = (int)ldexp(frexp(
f, &
exp), 20);
491 res |= mant | (
exp << 21);
504 for (
i = 1;
i <
cb->nentries;
i++)
505 if (
cb->lens[
i] <
cb->lens[
i-1])
507 if (
i ==
cb->nentries)
512 int len =
cb->lens[0];
517 for (j = 0; j+
i <
cb->nentries; j++)
526 for (
i = 0;
i <
cb->nentries;
i++)
529 if (
i !=
cb->nentries)
533 for (
i = 0;
i <
cb->nentries;
i++) {
568 for (
i = 0;
i <
fc->partitions;
i++)
571 for (
i = 0;
i <
fc->nclasses;
i++) {
577 if (
fc->classes[
i].subclass)
580 books = (1 <<
fc->classes[
i].subclass);
582 for (j = 0; j < books; j++)
589 for (
i = 2;
i <
fc->values;
i++)
607 for (j = 0; j < 8; j++)
619 for (j = 0; j < 8; j++)
620 if (rc->
books[
i][j] != -1)
630 int buffer_len = 50000;
638 for (
i = 0;
"vorbis"[
i];
i++)
652 buffer_len -= hlens[0];
658 for (
i = 0;
"vorbis"[
i];
i++)
666 buffer_len -= hlens[1];
672 for (
i = 0;
"vorbis"[
i];
i++)
706 if (
mc->coupling_steps) {
708 for (j = 0; j <
mc->coupling_steps; j++) {
717 for (j = 0; j < venc->
channels; j++)
720 for (j = 0; j <
mc->submaps; j++) {
741 len = hlens[0] + hlens[1] + hlens[2];
752 for (
i = 0;
i < 3;
i++) {
753 memcpy(p,
buffer + buffer_len, hlens[
i]);
755 buffer_len += hlens[
i];
764 int begin =
fc->list[
fc->list[
FFMAX(
i-1, 0)].sort].x;
765 int end =
fc->list[
fc->list[
FFMIN(
i+1,
fc->values - 1)].sort].x;
769 for (j = begin; j < end; j++)
770 average +=
fabs(coeffs[j]);
771 return average / (end - begin);
775 float *coeffs, uint16_t *posts,
int samples)
777 int range = 255 /
fc->multiplier + 1;
779 float tot_average = 0.0;
781 for (
i = 0;
i <
fc->values;
i++) {
783 tot_average += averages[
i];
785 tot_average /=
fc->values;
788 for (
i = 0;
i <
fc->values;
i++) {
789 int position =
fc->list[
fc->list[
i].sort].x;
790 float average = averages[
i];
793 average = sqrt(tot_average * average) * pow(1.25f, position*0.005f);
794 for (j = 0; j <
range - 1; j++)
797 posts[
fc->list[
i].sort] = j;
803 return y0 + (x - x0) * (y1 - y0) / (x1 - x0);
808 float *
floor,
int samples)
810 int range = 255 /
fc->multiplier + 1;
819 coded[0] = coded[1] = 1;
821 for (
i = 2;
i <
fc->values;
i++) {
823 posts[
fc->list[
i].low],
824 fc->list[
fc->list[
i].high].x,
825 posts[
fc->list[
i].high],
827 int highroom =
range - predicted;
828 int lowroom = predicted;
829 int room =
FFMIN(highroom, lowroom);
830 if (predicted == posts[
i]) {
834 if (!coded[
fc->list[
i].low ])
835 coded[
fc->list[
i].low ] = -1;
836 if (!coded[
fc->list[
i].high])
837 coded[
fc->list[
i].high] = -1;
839 if (posts[
i] > predicted) {
840 if (posts[
i] - predicted > room)
841 coded[
i] = posts[
i] - predicted + lowroom;
843 coded[
i] = (posts[
i] - predicted) << 1;
845 if (predicted - posts[
i] > room)
846 coded[
i] = predicted - posts[
i] + highroom - 1;
848 coded[
i] = ((predicted - posts[
i]) << 1) - 1;
853 for (
i = 0;
i <
fc->partitions;
i++) {
855 int k, cval = 0, csub = 1<<
c->subclass;
859 for (k = 0; k <
c->dim; k++) {
861 for (l = 0; l < csub; l++) {
863 if (
c->books[l] != -1)
866 if (coded[counter + k] < maxval)
871 cshift +=
c->subclass;
876 for (k = 0; k <
c->dim; k++) {
877 int book =
c->books[cval & (csub-1)];
878 int entry = coded[counter++];
879 cval >>=
c->subclass;
890 fc->multiplier,
floor, samples);
907 d -= vec[j] * num[j];
922 int pass,
i, j, p, k;
924 int partitions = (rc->
end - rc->
begin) / psize;
931 for (p = 0; p < partitions; p++) {
932 float max1 = 0.0, max2 = 0.0;
933 int s = rc->
begin + p * psize;
934 for (k =
s; k <
s + psize; k += 2) {
935 max1 =
FFMAX(max1,
fabs(coeffs[ k / real_ch]));
936 max2 =
FFMAX(max2,
fabs(coeffs[samples + k / real_ch]));
940 if (max1 < rc->maxes[
i][0] && max2 < rc->maxes[
i][1])
945 for (pass = 0; pass < 8; pass++) {
947 while (p < partitions) {
952 for (
i = 0;
i < classwords;
i++) {
954 entry += classes[j][p +
i];
959 for (
i = 0;
i < classwords && p < partitions;
i++, p++) {
961 int nbook = rc->
books[classes[j][p]][pass];
963 float *buf = coeffs + samples*j + rc->
begin + p*psize;
967 assert(rc->
type == 0 || rc->
type == 2);
981 a1 = (
s % real_ch) * samples;
983 s = real_ch * samples;
988 *pv++ = coeffs[
a2 +
b2];
989 if ((
a2 += samples) ==
s) {
998 coeffs[
a1 +
b1] -= *pv++;
999 if ((
a1 += samples) ==
s) {
1016 const float *
win = venc->
win[1];
1057 for (ch = 0; ch <
channels; ch++) {
1059 memset(
f->extended_data[ch], 0,
bps *
f->nb_samples);
1074 for (ch = 0; ch < venc->
channels; ch++)
1078 for (ch = 0; ch < venc->
channels; ch++)
1081 for (sf = 0; sf < subframes; sf++) {
1084 for (ch = 0; ch < venc->
channels; ch++) {
1090 memcpy(
offset + sf*sf_size, input,
len);
1091 memcpy(save + sf*sf_size, input,
len);
1103 int i, ret, need_more;
1122 need_more =
frame && need_more;
1132 for (
i = 0;
i < frames_needed;
i++) {
1158 if (
mode->blockflag) {
1210 *got_packet_ptr = 1;
1282 av_log(avctx,
AV_LOG_ERROR,
"Current FFmpeg Vorbis encoder only supports 2 channels.\n");
static float win(SuperEqualizerContext *s, float n, int N)
const FFCodec ff_vorbis_encoder
av_cold void ff_af_queue_close(AudioFrameQueue *afq)
Close AudioFrameQueue.
av_cold void ff_af_queue_init(AVCodecContext *avctx, AudioFrameQueue *afq)
Initialize AudioFrameQueue.
int ff_af_queue_remove(AudioFrameQueue *afq, int nb_samples, AVPacket *pkt)
Remove frame(s) from the queue.
int ff_af_queue_add(AudioFrameQueue *afq, const AVFrame *f)
Add a frame to the queue.
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Libavcodec external API header.
static void ff_bufqueue_add(void *log, struct FFBufQueue *queue, AVFrame *buf)
Add a buffer to the queue.
static void ff_bufqueue_discard_all(struct FFBufQueue *queue)
Unref and remove all buffers from the queue.
static AVFrame * ff_bufqueue_get(struct FFBufQueue *queue)
Get the first buffer from the queue and remove it.
#define i(width, name, range_min, range_max)
#define FF_CODEC_ENCODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
#define CODEC_SAMPLEFMTS(...)
static __device__ float fabs(float a)
static __device__ float floor(float a)
static const uint16_t fc[]
unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
Encode extradata length to a buffer.
int(* init)(AVBSFContext *ctx)
channel
Use these values when setting the channel map with ebur128_set_channel().
int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and allocate data.
static const uint8_t bits[8]
static const uint8_t frame_size[4]
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
#define AV_CODEC_FLAG_QSCALE
Use fixed qscale.
#define AV_CODEC_CAP_EXPERIMENTAL
Codec is experimental and is thus avoided in favor of non experimental encoders.
@ AV_PKT_DATA_SKIP_SAMPLES
Recommends skipping the specified number of samples.
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, size_t size)
Allocate new information of a packet.
@ AV_CHANNEL_ORDER_UNSPEC
Only the channel count is specified, without any further information about the channel order.
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
@ AV_SAMPLE_FMT_FLTP
float, planar
static void scale(int *out, const int *in, const int w, const int h, const int shift)
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
av_cold AVFloatDSPContext * avpriv_float_dsp_alloc(int bit_exact)
Allocate a float DSP context.
void * av_calloc(size_t nmemb, size_t size)
Memory handling functions.
static float distance(float x, float y, int band)
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
static int put_bits_left(PutBitContext *s)
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
static int put_bytes_output(const PutBitContext *s)
static av_unused void put_bits32(PutBitContext *s, uint32_t value)
Write exactly 32 bits into a bitstream.
#define FF_ARRAY_ELEMS(a)
int nb_channels
Number of channels in this layout.
main external API structure.
AVChannelLayout ch_layout
Audio channel layout.
enum AVSampleFormat sample_fmt
audio sample format
int global_quality
Global quality for codecs which cannot change it per frame.
int64_t bit_rate
the average bitrate
int initial_padding
Audio only.
int sample_rate
samples per second
int flags
AV_CODEC_FLAG_*.
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
int frame_size
Number of samples per channel in an audio frame.
void(* vector_fmul_reverse)(float *dst, const float *src0, const float *src1, int len)
Calculate the entry wise product of two vectors of floats, and store the result in a vector of floats...
void(* vector_fmul_scalar)(float *dst, const float *src, float mul, int len)
Multiply a vector of floats by a scalar float.
void(* vector_fmul)(float *dst, const float *src0, const float *src1, int len)
Calculate the entry wise product of two vectors of floats and store the result in a vector of floats.
This structure describes decoded (raw) audio or video data.
int nb_samples
number of audio samples (per channel) described by this frame
uint8_t ** extended_data
pointers to the data planes/channels.
This structure stores compressed data.
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Structure holding the queue.
unsigned short available
number of available buffers
vorbis_enc_codebook * codebooks
vorbis_enc_residue * residues
vorbis_enc_mapping * mappings
struct FFBufQueue bufqueue
vorbis_enc_floor * floors
vorbis_enc_floor_class * classes
vorbis_floor1_entry * list
#define av_malloc_array(a, b)
av_cold void av_tx_uninit(AVTXContext **ctx)
Frees a context and sets *ctx to NULL, does nothing when *ctx == NULL.
av_cold int av_tx_init(AVTXContext **ctx, av_tx_fn *tx, enum AVTXType type, int inv, int len, const void *scale, uint64_t flags)
Initialize a transform context with the given configuration (i)MDCTs with an odd length are currently...
@ AV_TX_FLOAT_MDCT
Standard MDCT with a sample data type of float, double or int32_t, respectively.
void(* av_tx_fn)(AVTXContext *s, void *out, void *in, ptrdiff_t stride)
Function pointer to a function to perform the transform.
static double cb(void *priv, double x, double y)
static double b1(void *priv, double x, double y)
static double b2(void *priv, double x, double y)
static double a2(void *priv, double x, double y)
static double a1(void *priv, double x, double y)
static const uint8_t quant[64]
unsigned int ff_vorbis_nth_root(unsigned int x, unsigned int n)
void ff_vorbis_floor1_render_list(vorbis_floor1_entry *list, int values, uint16_t *y_list, int *flag, int multiplier, float *out, int samples)
int ff_vorbis_len2vlc(uint8_t *bits, uint32_t *codes, unsigned num)
int ff_vorbis_ready_floor1_list(void *logctx, vorbis_floor1_entry *list, int values)
const float ff_vorbis_floor1_inverse_db_table[256]
const float *const ff_vorbis_vwin[8]
static const uint8_t quant_tables[]
static const struct @015006164144220275366232250062243134312011102000 cvectors[]
static const uint8_t codebooks[]
static const struct @151266026250113322224256156040007311307363203375 floor_classes[]
static void put_float(PutBitContext *pb, float f)
static int put_codeword(PutBitContext *pb, vorbis_enc_codebook *cb, int entry)
static av_cold int dsp_init(AVCodecContext *avctx, vorbis_enc_context *venc)
static void move_audio(vorbis_enc_context *venc, int sf_size)
static void floor_fit(vorbis_enc_context *venc, vorbis_enc_floor *fc, float *coeffs, uint16_t *posts, int samples)
static float * put_vector(vorbis_enc_codebook *book, PutBitContext *pb, float *num)
static void put_codebook_header(PutBitContext *pb, vorbis_enc_codebook *cb)
static int create_vorbis_context(vorbis_enc_context *venc, AVCodecContext *avctx)
#define NUM_FLOOR_PARTITIONS
static void put_residue_header(PutBitContext *pb, vorbis_enc_residue *rc)
static void put_floor_header(PutBitContext *pb, vorbis_enc_floor *fc)
static av_cold int vorbis_encode_close(AVCodecContext *avctx)
#define NUM_RESIDUE_PARTITIONS
static int render_point(int x0, int y0, int x1, int y1, int x)
static int put_main_header(vorbis_enc_context *venc, uint8_t **out)
static int ready_residue(vorbis_enc_residue *rc, vorbis_enc_context *venc)
static int ready_codebook(vorbis_enc_codebook *cb)
static int apply_window_and_mdct(vorbis_enc_context *venc)
static int residue_encode(vorbis_enc_context *venc, vorbis_enc_residue *rc, PutBitContext *pb, float *coeffs, int samples, int real_ch)
static float get_floor_average(vorbis_enc_floor *fc, float *coeffs, int i)
static av_cold int vorbis_encode_init(AVCodecContext *avctx)
static AVFrame * spawn_empty_frame(AVCodecContext *avctx, int channels)
static int vorbis_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
static int floor_encode(vorbis_enc_context *venc, vorbis_enc_floor *fc, PutBitContext *pb, uint16_t *posts, float *floor, int samples)
static int cb_lookup_vals(int lookup, int dimensions, int entries)