36 #define BITSTREAM_WRITER_LE
131 #define MAX_CHANNELS 2
132 #define MAX_CODEBOOK_DIM 8
134 #define MAX_FLOOR_CLASS_DIM 4
135 #define NUM_FLOOR_PARTITIONS 8
136 #define MAX_FLOOR_VALUES (MAX_FLOOR_CLASS_DIM*NUM_FLOOR_PARTITIONS+2)
138 #define RESIDUE_SIZE 1600
139 #define RESIDUE_PART_SIZE 32
140 #define NUM_RESIDUE_PARTITIONS (RESIDUE_SIZE/RESIDUE_PART_SIZE)
158 else if (lookup == 2)
159 return dimensions *entries;
177 for (i = 0; i < cb->
nentries; i++) {
184 off = (i / div) % vals;
210 for (j = 0; j < 8; j++)
211 if (rc->
books[i][j] != -1)
219 for (j = 0; j < cb->
nentries; j++) {
224 if (a > rc->
maxes[i][0])
227 if (a > rc->
maxes[i][1])
233 rc->
maxes[i][0] += 0.8;
234 rc->
maxes[i][1] += 0.8;
259 for (book = 0; book < venc->
ncodebooks; book++) {
281 for (i = 0; i < vals; i++)
303 static const int a[] = {0, 1, 2, 2, 3, 3, 4, 4};
311 for (i = 0; i < fc->
nclasses; i++) {
321 for (j = 0; j < books; j++)
336 for (i = 2; i < fc->
values; i++) {
337 static const int a[] = {
338 93, 23,372, 6, 46,186,750, 14, 33, 65,
339 130,260,556, 3, 10, 18, 28, 39, 55, 79,
340 111,158,220,312,464,650,850
342 fc->
list[i].
x = a[i - 2];
364 static const int8_t
a[10][8] = {
365 { -1, -1, -1, -1, -1, -1, -1, -1, },
366 { -1, -1, 16, -1, -1, -1, -1, -1, },
367 { -1, -1, 17, -1, -1, -1, -1, -1, },
368 { -1, -1, 18, -1, -1, -1, -1, -1, },
369 { -1, -1, 19, -1, -1, -1, -1, -1, },
370 { -1, -1, 20, -1, -1, -1, -1, -1, },
371 { -1, -1, 21, -1, -1, -1, -1, -1, },
372 { 22, 23, -1, -1, -1, -1, -1, -1, },
373 { 24, 25, -1, -1, -1, -1, -1, -1, },
374 { 26, 27, 28, -1, -1, -1, -1, -1, },
376 memcpy(rc->
books, a,
sizeof a);
392 for (i = 0; i < venc->
channels; i++)
398 for (i = 0; i < mc->
submaps; i++) {
444 mant = (int)ldexp(frexp(f, &exp), 20);
450 res |= mant | (exp << 21);
474 while (i < cb->nentries) {
476 for (j = 0; j+i < cb->
nentries; j++)
477 if (cb->
lens[j+i] != len)
492 for (i = 0; i < cb->
nentries; i++) {
505 for (i = 1; i < tmp; i++)
514 for (i = 0; i < tmp; i++)
530 for (i = 0; i < fc->
nclasses; i++) {
541 for (j = 0; j < books; j++)
548 for (i = 2; i < fc->
values; i++)
566 for (j = 0; j < 8; j++)
567 tmp |= (rc->
books[i][j] != -1) << j;
578 for (j = 0; j < 8; j++)
579 if (rc->
books[i][j] != -1)
589 int buffer_len = 50000;
597 for (i = 0;
"vorbis"[i]; i++)
611 buffer_len -= hlens[0];
617 for (i = 0;
"vorbis"[i]; i++)
625 buffer_len -= hlens[1];
631 for (i = 0;
"vorbis"[i]; i++)
645 for (i = 0; i < venc->
nfloors; i++)
676 for (j = 0; j < venc->
channels; j++)
679 for (j = 0; j < mc->
submaps; j++) {
688 for (i = 0; i < venc->
nmodes; i++) {
700 len = hlens[0] + hlens[1] + hlens[2];
709 for (i = 0; i < 3; i++) {
710 memcpy(p, buffer + buffer_len, hlens[i]);
712 buffer_len += hlens[i];
726 for (j = begin; j <
end; j++)
727 average += fabs(coeffs[j]);
728 return average / (end - begin);
732 float *coeffs, uint16_t *posts,
int samples)
736 float tot_average = 0.0;
738 for (i = 0; i < fc->
values; i++) {
740 tot_average += averages[i];
742 tot_average /= fc->
values;
745 for (i = 0; i < fc->
values; i++) {
747 float average = averages[i];
750 average = sqrt(tot_average * average) * pow(1.25f, position*0.005f);
751 for (j = 0; j < range - 1; j++)
760 return y0 + (x - x0) * (y1 - y0) / (x1 - x0);
765 float *floor,
int samples)
776 coded[0] = coded[1] = 1;
778 for (i = 2; i < fc->
values; i++) {
784 int highroom = range - predicted;
785 int lowroom = predicted;
786 int room =
FFMIN(highroom, lowroom);
787 if (predicted == posts[i]) {
796 if (posts[i] > predicted) {
797 if (posts[i] - predicted > room)
798 coded[i] = posts[i] - predicted + lowroom;
800 coded[i] = (posts[i] - predicted) << 1;
802 if (predicted - posts[i] > room)
803 coded[i] = predicted - posts[i] + highroom - 1;
805 coded[i] = ((predicted - posts[i]) << 1) - 1;
812 int k, cval = 0, csub = 1<<c->
subclass;
816 for (k = 0; k < c->
dim; k++) {
818 for (l = 0; l < csub; l++) {
820 if (c->
books[l] != -1)
823 if (coded[counter + k] < maxval)
833 for (k = 0; k < c->
dim; k++) {
834 int book = c->
books[cval & (csub-1)];
835 int entry = coded[counter++];
858 for (i = 0; i < book->
nentries; i++) {
864 d -= vec[j] * num[j];
879 int pass, i, j, p, k;
881 int partitions = (rc->
end - rc->
begin) / psize;
882 int channels = (rc->
type == 2) ? 1 : real_ch;
888 for (p = 0; p < partitions; p++) {
889 float max1 = 0.0, max2 = 0.0;
890 int s = rc->
begin + p * psize;
891 for (k = s; k < s + psize; k += 2) {
892 max1 =
FFMAX(max1, fabs(coeffs[ k / real_ch]));
893 max2 =
FFMAX(max2, fabs(coeffs[samples + k / real_ch]));
897 if (max1 < rc->maxes[i][0] && max2 < rc->maxes[i][1])
902 for (pass = 0; pass < 8; pass++) {
904 while (p < partitions) {
906 for (j = 0; j < channels; j++) {
909 for (i = 0; i < classwords; i++) {
911 entry += classes[j][p + i];
916 for (i = 0; i < classwords && p < partitions; i++, p++) {
917 for (j = 0; j < channels; j++) {
918 int nbook = rc->
books[classes[j][p]][
pass];
920 float *
buf = coeffs + samples*j + rc->
begin + p*psize;
924 assert(rc->
type == 0 || rc->
type == 2);
937 int s = rc->
begin + p * psize,
a1, b1;
938 a1 = (s % real_ch) * samples;
940 s = real_ch * samples;
945 *pv++ = coeffs[a2 + b2];
946 if ((a2 += samples) ==
s) {
955 coeffs[a1 + b1] -= *pv++;
956 if ((a1 += samples) == s) {
971 float **audio,
int samples)
974 const float * win = venc->
win[0];
983 for (channel = 0; channel < venc->
channels; channel++)
984 memcpy(venc->
samples + channel * window_len * 2,
985 venc->
saved + channel * window_len,
sizeof(
float) * window_len);
987 for (channel = 0; channel < venc->
channels; channel++)
988 memset(venc->
samples + channel * window_len * 2, 0,
989 sizeof(
float) * window_len);
993 for (channel = 0; channel < venc->
channels; channel++) {
994 float *
offset = venc->
samples + channel*window_len*2 + window_len;
995 for (i = 0; i < samples; i++)
996 offset[i] = audio[channel][i] / n * win[window_len - i - 1];
999 for (channel = 0; channel < venc->
channels; channel++)
1000 memset(venc->
samples + channel * window_len * 2 + window_len,
1001 0,
sizeof(
float) * window_len);
1004 for (channel = 0; channel < venc->
channels; channel++)
1006 venc->
samples + channel * window_len * 2);
1009 for (channel = 0; channel < venc->
channels; channel++) {
1010 float *
offset = venc->
saved + channel * window_len;
1011 for (i = 0; i < samples; i++)
1012 offset[i] = audio[channel][i] / n * win[i];
1052 if (
mode->blockflag) {
1057 for (i = 0; i < venc->
channels; i++) {
1067 for (i = 0; i < venc->
channels * samples; i++)
1070 for (i = 0; i < mapping->coupling_steps; i++) {
1071 float *mag = venc->
coeffs + mapping->magnitude[i] * samples;
1072 float *ang = venc->
coeffs + mapping->angle[i] * samples;
1074 for (j = 0; j < samples; j++) {
1103 *got_packet_ptr = 1;
1124 for (i = 0; i < venc->
nfloors; i++) {
1173 av_log(avctx,
AV_LOG_ERROR,
"Current FFmpeg Vorbis encoder only supports 2 channels.\n");
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.
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and/or allocate data.
unsigned int ff_vorbis_nth_root(unsigned int x, unsigned int n)
This structure describes decoded (raw) audio or video data.
void(* mdct_calc)(struct FFTContext *s, FFTSample *output, const FFTSample *input)
static int ready_codebook(vorbis_enc_codebook *cb)
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
static av_cold int init(AVCodecContext *avctx)
static int render_point(int x0, int y0, int x1, int y1, int x)
const float ff_vorbis_floor1_inverse_db_table[256]
static const struct @91 floor_classes[]
static int floor_encode(vorbis_enc_context *venc, vorbis_enc_floor *fc, PutBitContext *pb, uint16_t *posts, float *floor, int samples)
#define FF_ARRAY_ELEMS(a)
static void put_codebook_header(PutBitContext *pb, vorbis_enc_codebook *cb)
vorbis_floor1_entry * list
vorbis_enc_codebook * codebooks
#define av_assert0(cond)
assert() equivalent, that is always enabled.
static double cb(void *priv, double x, double y)
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
static av_cold int vorbis_encode_close(AVCodecContext *avctx)
vorbis_enc_residue * residues
static av_cold int end(AVCodecContext *avctx)
#define NUM_FLOOR_PARTITIONS
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
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 av_cold int vorbis_encode_init(AVCodecContext *avctx)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
#define CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
vorbis_enc_mapping * mappings
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
static const struct @90 cvectors[]
#define CODEC_FLAG_QSCALE
Use fixed qscale.
const char * name
Name of the codec implementation.
static const uint8_t offset[127][2]
Libavcodec external API header.
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)
static const uint16_t fc[]
int bit_rate
the average bitrate
static int cb_lookup_vals(int lookup, int dimensions, int entries)
int ff_vorbis_len2vlc(uint8_t *bits, uint32_t *codes, unsigned num)
static void put_floor_header(PutBitContext *pb, vorbis_enc_floor *fc)
static float * put_vector(vorbis_enc_codebook *book, PutBitContext *pb, float *num)
vorbis_enc_floor_class * classes
int frame_size
Number of samples per channel in an audio frame.
AVSampleFormat
Audio sample formats.
static int create_vorbis_context(vorbis_enc_context *venc, AVCodecContext *avctx)
int sample_rate
samples per second
main external API structure.
static int put_codeword(PutBitContext *pb, vorbis_enc_codebook *cb, int entry)
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
Encode extradata length to a buffer.
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)
#define CODEC_CAP_EXPERIMENTAL
Codec is experimental and is thus avoided in favor of non experimental encoders.
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
static int vorbis_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
AVCodec ff_vorbis_encoder
int channels
number of audio channels
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
void ff_vorbis_floor1_render_list(vorbis_floor1_entry *list, int values, uint16_t *y_list, int *flag, int multiplier, float *out, int samples)
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> out
static void * av_mallocz_array(size_t nmemb, size_t size)
static enum AVSampleFormat sample_fmts[]
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 av_malloc_array(a, b)
#define NUM_RESIDUE_PARTITIONS
uint8_t ** extended_data
pointers to the data planes/channels.
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...
#define AV_NOPTS_VALUE
Undefined timestamp value.
int ff_vorbis_ready_floor1_list(AVCodecContext *avctx, vorbis_floor1_entry *list, int values)