35 #define BITSTREAM_WRITER_LE
102 int log2_blocksize[2];
130 #define MAX_CHANNELS 2
131 #define MAX_CODEBOOK_DIM 8
133 #define MAX_FLOOR_CLASS_DIM 4
134 #define NUM_FLOOR_PARTITIONS 8
135 #define MAX_FLOOR_VALUES (MAX_FLOOR_CLASS_DIM*NUM_FLOOR_PARTITIONS+2)
137 #define RESIDUE_SIZE 1600
138 #define RESIDUE_PART_SIZE 32
139 #define NUM_RESIDUE_PARTITIONS (RESIDUE_SIZE/RESIDUE_PART_SIZE)
145 assert(entry < cb->nentries);
146 assert(cb->
lens[entry]);
157 else if (lookup == 2)
158 return dimensions *entries;
176 for (i = 0; i < cb->
nentries; i++) {
183 off = (i / div) % vals;
202 assert(rc->
type == 2);
209 for (j = 0; j < 8; j++)
210 if (rc->
books[i][j] != -1)
218 for (j = 0; j < cb->
nentries; j++) {
223 if (a > rc->
maxes[i][0])
226 if (a > rc->
maxes[i][1])
232 rc->
maxes[i][0] += 0.8;
233 rc->
maxes[i][1] += 0.8;
258 for (book = 0; book < venc->
ncodebooks; book++) {
280 for (i = 0; i < vals; i++)
302 static const int a[] = {0, 1, 2, 2, 3, 3, 4, 4};
310 for (i = 0; i < fc->
nclasses; i++) {
320 for (j = 0; j < books; j++)
335 for (i = 2; i < fc->
values; i++) {
336 static const int a[] = {
337 93, 23,372, 6, 46,186,750, 14, 33, 65,
338 130,260,556, 3, 10, 18, 28, 39, 55, 79,
339 111,158,220,312,464,650,850
341 fc->
list[i].
x = a[i - 2];
363 static const int8_t
a[10][8] = {
364 { -1, -1, -1, -1, -1, -1, -1, -1, },
365 { -1, -1, 16, -1, -1, -1, -1, -1, },
366 { -1, -1, 17, -1, -1, -1, -1, -1, },
367 { -1, -1, 18, -1, -1, -1, -1, -1, },
368 { -1, -1, 19, -1, -1, -1, -1, -1, },
369 { -1, -1, 20, -1, -1, -1, -1, -1, },
370 { -1, -1, 21, -1, -1, -1, -1, -1, },
371 { 22, 23, -1, -1, -1, -1, -1, -1, },
372 { 24, 25, -1, -1, -1, -1, -1, -1, },
373 { 26, 27, 28, -1, -1, -1, -1, -1, },
375 memcpy(rc->
books, a,
sizeof a);
391 for (i = 0; i < venc->
channels; i++)
397 for (i = 0; i < mc->
submaps; i++) {
443 mant = (int)ldexp(frexp(f, &exp), 20);
449 res |= mant | (exp << 21);
473 while (i < cb->nentries) {
475 for (j = 0; j+i < cb->
nentries; j++)
476 if (cb->
lens[j+i] != len)
491 for (i = 0; i < cb->
nentries; i++) {
504 for (i = 1; i < tmp; i++)
513 for (i = 0; i < tmp; i++)
529 for (i = 0; i < fc->
nclasses; i++) {
540 for (j = 0; j < books; j++)
547 for (i = 2; i < fc->
values; i++)
565 for (j = 0; j < 8; j++)
566 tmp |= (rc->
books[i][j] != -1) << j;
577 for (j = 0; j < 8; j++)
578 if (rc->
books[i][j] != -1)
588 int buffer_len =
sizeof buffer;
594 for (i = 0;
"vorbis"[i]; i++)
608 buffer_len -= hlens[0];
614 for (i = 0;
"vorbis"[i]; i++)
622 buffer_len -= hlens[1];
628 for (i = 0;
"vorbis"[i]; i++)
642 for (i = 0; i < venc->
nfloors; i++)
673 for (j = 0; j < venc->
channels; j++)
676 for (j = 0; j < mc->
submaps; j++) {
685 for (i = 0; i < venc->
nmodes; i++) {
697 len = hlens[0] + hlens[1] + hlens[2];
706 for (i = 0; i < 3; i++) {
707 memcpy(p, buffer + buffer_len, hlens[i]);
709 buffer_len += hlens[i];
722 for (j = begin; j < end; j++)
723 average += fabs(coeffs[j]);
724 return average / (end - begin);
732 float tot_average = 0.;
734 for (i = 0; i < fc->
values; i++) {
736 tot_average += averages[i];
738 tot_average /= fc->
values;
741 for (i = 0; i < fc->
values; i++) {
743 float average = averages[i];
746 average = sqrt(tot_average * average) * pow(1.25f, position*0.005f);
747 for (j = 0; j < range - 1; j++)
756 return y0 + (x - x0) * (y1 - y0) / (x1 - x0);
772 coded[0] = coded[1] = 1;
774 for (i = 2; i < fc->
values; i++) {
780 int highroom = range - predicted;
781 int lowroom = predicted;
782 int room =
FFMIN(highroom, lowroom);
783 if (predicted == posts[i]) {
792 if (posts[i] > predicted) {
793 if (posts[i] - predicted > room)
794 coded[i] = posts[i] - predicted + lowroom;
796 coded[i] = (posts[i] - predicted) << 1;
798 if (predicted - posts[i] > room)
799 coded[i] = predicted - posts[i] + highroom - 1;
801 coded[i] = ((predicted - posts[i]) << 1) - 1;
808 int k, cval = 0, csub = 1<<c->
subclass;
812 for (k = 0; k < c->
dim; k++) {
814 for (l = 0; l < csub; l++) {
816 if (c->
books[l] != -1)
819 if (coded[counter + k] < maxval)
829 for (k = 0; k < c->
dim; k++) {
830 int book = c->
books[cval & (csub-1)];
831 int entry = coded[counter++];
854 for (i = 0; i < book->
nentries; i++) {
860 d -= vec[j] * num[j];
875 int pass, i, j, p, k;
877 int partitions = (rc->
end - rc->
begin) / psize;
878 int channels = (rc->
type == 2) ? 1 : real_ch;
882 assert(rc->
type == 2);
883 assert(real_ch == 2);
884 for (p = 0; p < partitions; p++) {
885 float max1 = 0., max2 = 0.;
886 int s = rc->
begin + p * psize;
887 for (k = s; k < s + psize; k += 2) {
888 max1 =
FFMAX(max1, fabs(coeffs[ k / real_ch]));
889 max2 =
FFMAX(max2, fabs(coeffs[samples + k / real_ch]));
893 if (max1 < rc->maxes[i][0] && max2 < rc->maxes[i][1])
898 for (pass = 0; pass < 8; pass++) {
900 while (p < partitions) {
902 for (j = 0; j < channels; j++) {
905 for (i = 0; i < classwords; i++) {
907 entry += classes[j][p + i];
912 for (i = 0; i < classwords && p < partitions; i++, p++) {
913 for (j = 0; j < channels; j++) {
914 int nbook = rc->
books[classes[j][p]][
pass];
916 float *buf = coeffs + samples*j + rc->
begin + p*psize;
920 assert(rc->
type == 0 || rc->
type == 2);
933 int s = rc->
begin + p * psize,
a1, b1;
934 a1 = (s % real_ch) * samples;
941 *pv++ = coeffs[a2 + b2];
942 if ((a2 += samples) == s) {
951 coeffs[a1 + b1] -= *pv++;
952 if ((a1 += samples) == s) {
970 const float * win = venc->
win[0];
979 for (channel = 0; channel < venc->
channels; channel++)
980 memcpy(venc->
samples + channel * window_len * 2,
981 venc->
saved + channel * window_len,
sizeof(
float) * window_len);
983 for (channel = 0; channel < venc->
channels; channel++)
984 memset(venc->
samples + channel * window_len * 2, 0,
985 sizeof(
float) * window_len);
989 for (channel = 0; channel < venc->
channels; channel++) {
990 float * offset = venc->
samples + channel*window_len*2 + window_len;
992 offset[i] = audio[channel][i] / n * win[window_len - i - 1];
995 for (channel = 0; channel < venc->
channels; channel++)
996 memset(venc->
samples + channel * window_len * 2 + window_len,
997 0,
sizeof(
float) * window_len);
1000 for (channel = 0; channel < venc->
channels; channel++)
1002 venc->
samples + channel * window_len * 2);
1005 for (channel = 0; channel < venc->
channels; channel++) {
1006 float *offset = venc->
saved + channel * window_len;
1008 offset[i] = audio[channel][i] / n * win[i];
1019 const AVFrame *frame,
int *got_packet_ptr)
1049 mode = &venc->
modes[0];
1050 mapping = &venc->
mappings[mode->mapping];
1051 if (mode->blockflag) {
1056 for (i = 0; i < venc->
channels; i++) {
1069 for (i = 0; i < mapping->coupling_steps; i++) {
1070 float *mag = venc->
coeffs + mapping->magnitude[i] *
samples;
1073 for (j = 0; j <
samples; j++) {
1101 *got_packet_ptr = 1;
1122 for (i = 0; i < venc->
nfloors; i++) {
1160 #if FF_API_OLD_ENCODE_AUDIO
1174 av_log(avccontext,
AV_LOG_ERROR,
"Current Libav Vorbis encoder only supports 2 channels.\n");
1194 #if FF_API_OLD_ENCODE_AUDIO
static int ready_residue(vorbis_enc_residue *rc, vorbis_enc_context *venc)
static void av_unused put_bits32(PutBitContext *s, uint32_t value)
Write exactly 32 bits into a bitstream.
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
static const int16_t coeffs[28]
unsigned int ff_vorbis_nth_root(unsigned int x, unsigned int n)
This structure describes decoded (raw) audio or video data.
static int vorbis_encode_frame(AVCodecContext *avccontext, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
void(* mdct_calc)(struct FFTContext *s, FFTSample *output, const FFTSample *input)
static int ready_codebook(vorbis_enc_codebook *cb)
static av_cold int vorbis_encode_init(AVCodecContext *avccontext)
AVFrame * coded_frame
the picture in the bitstream
static int render_point(int x0, int y0, int x1, int y1, int x)
const float ff_vorbis_floor1_inverse_db_table[256]
static int floor_encode(vorbis_enc_context *venc, vorbis_enc_floor *fc, PutBitContext *pb, uint16_t *posts, float *floor, int samples)
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
static void put_codebook_header(PutBitContext *pb, vorbis_enc_codebook *cb)
vorbis_floor1_entry * list
vorbis_enc_codebook * codebooks
vorbis_enc_residue * residues
#define NUM_FLOOR_PARTITIONS
int64_t pts
presentation timestamp in time_base units (time when frame should be shown to user) If AV_NOPTS_VALUE...
#define CODEC_FLAG_QSCALE
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
static int apply_window_and_mdct(vorbis_enc_context *venc, float **audio, int samples)
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
static int init(AVCodecParserContext *s)
vorbis_enc_mapping * mappings
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
void av_log(void *avcl, int level, const char *fmt,...)
const char * name
Name of the codec implementation.
static void put_bits(PutBitContext *s, int n, unsigned int value)
Write up to 31 bits into a bitstream.
static void floor_fit(vorbis_enc_context *venc, vorbis_enc_floor *fc, float *coeffs, uint16_t *posts, int samples)
static int put_bits_count(PutBitContext *s)
static float distance(float x, float y, int band)
AVFrame * avcodec_alloc_frame(void)
Allocate an AVFrame and set its fields to default values.
int bit_rate
the average bitrate
static int cb_lookup_vals(int lookup, int dimensions, int entries)
static const struct @60 floor_classes[]
int ff_vorbis_len2vlc(uint8_t *bits, uint32_t *codes, unsigned num)
#define CODEC_CAP_EXPERIMENTAL
int ff_alloc_packet(AVPacket *avpkt, int size)
Check AVPacket size and/or allocate data.
static void put_floor_header(PutBitContext *pb, vorbis_enc_floor *fc)
static float * put_vector(vorbis_enc_codebook *book, PutBitContext *pb, float *num)
static const struct @59 cvectors[]
vorbis_enc_floor_class * classes
int frame_size
Number of samples per channel in an audio frame.
int sample_rate
samples per second
static int create_vorbis_context(vorbis_enc_context *venc, AVCodecContext *avccontext)
main external API structure.
static void close(AVCodecParserContext *s)
static int put_codeword(PutBitContext *pb, vorbis_enc_codebook *cb, int entry)
unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
Encode extradata length to a buffer.
#define FF_ARRAY_ELEMS(a)
vorbis_enc_floor * floors
const float *const ff_vorbis_vwin[8]
static int residue_encode(vorbis_enc_context *venc, vorbis_enc_residue *rc, PutBitContext *pb, float *coeffs, int samples, int real_ch)
int global_quality
Global quality for codecs which cannot change it per frame.
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
static void put_residue_header(PutBitContext *pb, vorbis_enc_residue *rc)
static void put_float(PutBitContext *pb, float f)
static float get_floor_average(vorbis_enc_floor *fc, float *coeffs, int i)
AVSampleFormat
Audio Sample Formats.
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
AVCodec ff_vorbis_encoder
int channels
number of audio channels
void ff_vorbis_floor1_render_list(vorbis_floor1_entry *list, int values, uint16_t *y_list, int *flag, int multiplier, float *out, int samples)
static av_cold int vorbis_encode_close(AVCodecContext *avccontext)
static int put_main_header(vorbis_enc_context *venc, uint8_t **out)
static av_always_inline int64_t ff_samples_to_time_base(AVCodecContext *avctx, int64_t samples)
Rescale from sample rate to AVCodecContext.time_base.
#define NUM_RESIDUE_PARTITIONS
uint8_t ** extended_data
pointers to the data planes/channels.
int ff_vorbis_ready_floor1_list(AVCodecContext *avccontext, vorbis_floor1_entry *list, int values)
This structure stores compressed data.
int nb_samples
number of audio samples (per channel) described by this frame
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
if(!(ptr_align%ac->ptr_align)&&samples_align >=aligned_len)