31 int silence = 0,
ch, i, j;
38 for (i = 1; i <=
FFMIN(lap_size, index); i++) {
43 for (i = 0; i < lap_size; i++) {
44 const int offset = i*120 + lap_size;
60 float avg_c_s, energy = 0.0f, dist_dev = 0.0f;
63 for (j = 0; j < range; j++)
64 energy += coeffs[j]*coeffs[j];
68 avg_c_s = energy / range;
70 for (j = 0; j < range; j++) {
71 const float c_s = coeffs[j]*coeffs[j];
72 dist_dev = (avg_c_s - c_s)*(avg_c_s - c_s);
75 st->
tone[
ch][i] += sqrtf(dist_dev);
83 float incompat = 0.0f;
84 const float *coeffs1 = st->
bands[0][i];
85 const float *coeffs2 = st->
bands[1][i];
87 for (j = 0; j < range; j++)
88 incompat += (coeffs1[j] - coeffs2[j])*(coeffs1[j] - coeffs2[j]);
89 st->
stereo[i] = sqrtf(incompat);
115 int offset_s,
int offset_e,
int resolution,
119 float c_change = 0.0f;
120 if ((offset_e - offset_s) <= resolution)
122 for (i = offset_s; i < offset_e; i++) {
124 if (c_change > tgt_change)
136 int fsize, silent_frames;
138 for (silent_frames = 0; silent_frames < s->
buffered_steps; silent_frames++)
141 if (--silent_frames < 0)
145 if ((1 << fsize) > silent_frames)
147 s->
p.
frames =
FFMIN(silent_frames / (1 << fsize), 48 >> fsize);
176 float total_energy_change = 0.0f;
206 int i, neighbouring_points = 0, start_offset = 0;
207 int radius = (1 << s->
p.
framesize), step_offset = radius*index;
215 for (i = 0; i < (1 << f->
size); i++)
233 neighbouring_points++;
257 memset(f->
alloc_boost, 0,
sizeof(
int)*CELT_MAX_BANDS);
265 float rate, frame_bits = 0;
272 float max_score = 1.0f;
277 float tonal_contrib = 0.0f;
279 weight = start[f]->
stereo[i];
282 tonal_contrib += start[f]->
tone[
ch][i];
285 tonal += tonal_contrib;
289 tonal /= (float)CELT_MAX_BANDS;
292 if (band_score[i] > max_score)
293 max_score = band_score[i];
298 frame_bits += band_score[i]*8.0f;
304 rate = ((float)s->
avctx->
bit_rate) + frame_bits*frame_size*16;
347 float dist, best_dist = FLT_MAX;
352 for (i = f->
end_band; i >= end_band; i--) {
355 if (best_dist > dist) {
368 float score[2] = { 0 };
370 for (cway = 0; cway < 2; cway++) {
375 for (i = 0; i < 2; i++) {
377 mag[i] = c < 0 ? base >>
FFABS(c) : base <<
FFABS(c);
381 float iscore0 = 0.0f;
382 float iscore1 = 0.0f;
383 for (j = 0; j < (1 << f->
size); j++) {
385 iscore0 += start[j]->
tone[k][i]*start[j]->
change_amp[k][i]/mag[0];
386 iscore1 += start[j]->
tone[k][i]*start[j]->
change_amp[k][i]/mag[1];
389 config[cway][i] =
FFABS(iscore0 - 1.0f) <
FFABS(iscore1 - 1.0f);
390 score[cway] += config[cway][i] ? iscore1 : iscore0;
413 if (f->
transient != start_transient_flag) {
427 int steps_out = s->
p.
frames*(frame_size/120);
431 for (i = 0; i < steps_out; i++)
435 tmp[i] = s->
steps[i];
438 const int i_new = i - steps_out;
447 for (i = 0; i < s->
p.
frames; i++) {
MDCT15Context * mdct[CELT_BLOCK_NB]
void ff_opus_psy_celt_frame_init(OpusPsyContext *s, CeltFrame *f, int index)
static int flush_silent_frames(OpusPsyContext *s)
#define OPUS_SAMPLES_TO_BLOCK_SIZE(x)
int64_t total_packets_out
enum OpusBandwidth bandwidth
struct FFBufQueue * bufqueue
This structure describes decoded (raw) audio or video data.
float * window[CELT_BLOCK_NB]
void ff_opus_rc_enc_init(OpusRangeCoder *rc)
int64_t bit_rate
the average bitrate
static FFServerConfig config
OpusPsyStep * steps[FF_BUFQUEUE_SIZE+1]
FFBesselFilter bfilter_hi[OPUS_MAX_CHANNELS][CELT_MAX_BANDS]
FFBesselFilter bfilter_lo[OPUS_MAX_CHANNELS][CELT_MAX_BANDS]
const uint8_t ff_celt_freq_bands[]
static void generate_window_func(float *lut, int N, int win_func, float *overlap)
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
#define OPUS_MAX_PACKET_SIZE
void ff_celt_enc_bitalloc(OpusRangeCoder *rc, CeltFrame *f)
Structure holding the queue.
const uint8_t ff_celt_band_end[]
float coeffs[OPUS_MAX_CHANNELS][OPUS_BLOCK_SIZE(CELT_BLOCK_960)]
static float bessel_filter(FFBesselFilter *s, float x)
float stereo[CELT_MAX_BANDS]
uint8_t pi<< 24) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_U8,(uint64_t)((*(constuint8_t *) pi-0x80U))<< 56) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S16,(uint64_t)(*(constint16_t *) pi)<< 48) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S32,(uint64_t)(*(constint32_t *) pi)<< 32) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S64,(*(constint64_t *) pi >>56)+0x80) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S64,*(constint64_t *) pi *(1.0f/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S64,*(constint64_t *) pi *(1.0/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_FLT, llrintf(*(constfloat *) pi *(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_DBL, llrint(*(constdouble *) pi *(INT64_C(1)<< 63)))#defineFMT_PAIR_FUNC(out, in) staticconv_func_type *constfmt_pair_to_conv_functions[AV_SAMPLE_FMT_NB *AV_SAMPLE_FMT_NB]={FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S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V_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S64),};staticvoidcpy1(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, len);}staticvoidcpy2(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 2 *len);}staticvoidcpy4(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 4 *len);}staticvoidcpy8(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 8 *len);}AudioConvert *swri_audio_convert_alloc(enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, constint *ch_map, intflags){AudioConvert *ctx;conv_func_type *f=fmt_pair_to_conv_functions[av_get_packed_sample_fmt(out_fmt)+AV_SAMPLE_FMT_NB *av_get_packed_sample_fmt(in_fmt)];if(!f) returnNULL;ctx=av_mallocz(sizeof(*ctx));if(!ctx) returnNULL;if(channels==1){in_fmt=av_get_planar_sample_fmt(in_fmt);out_fmt=av_get_planar_sample_fmt(out_fmt);}ctx->channels=channels;ctx->conv_f=f;ctx->ch_map=ch_map;if(in_fmt==AV_SAMPLE_FMT_U8||in_fmt==AV_SAMPLE_FMT_U8P) memset(ctx->silence, 0x80, sizeof(ctx->silence));if(out_fmt==in_fmt &&!ch_map){switch(av_get_bytes_per_sample(in_fmt)){case1:ctx->simd_f=cpy1;break;case2:ctx->simd_f=cpy2;break;case4:ctx->simd_f=cpy4;break;case8:ctx->simd_f=cpy8;break;}}if(HAVE_X86ASM &&1) swri_audio_convert_init_x86(ctx, out_fmt, in_fmt, channels);if(ARCH_ARM) swri_audio_convert_init_arm(ctx, out_fmt, in_fmt, channels);if(ARCH_AARCH64) swri_audio_convert_init_aarch64(ctx, out_fmt, in_fmt, channels);returnctx;}voidswri_audio_convert_free(AudioConvert **ctx){av_freep(ctx);}intswri_audio_convert(AudioConvert *ctx, AudioData *out, AudioData *in, intlen){intch;intoff=0;constintos=(out->planar?1:out->ch_count)*out->bps;unsignedmisaligned=0;av_assert0(ctx->channels==out->ch_count);if(ctx->in_simd_align_mask){intplanes=in->planar?in->ch_count:1;unsignedm=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) in->ch[ch];misaligned|=m &ctx->in_simd_align_mask;}if(ctx->out_simd_align_mask){intplanes=out->planar?out->ch_count:1;unsignedm=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) out->ch[ch];misaligned|=m &ctx->out_simd_align_mask;}if(ctx->simd_f &&!ctx->ch_map &&!misaligned){off=len &~15;av_assert1(off >=0);av_assert1(off<=len);av_assert2(ctx->channels==SWR_CH_MAX||!in->ch[ctx->channels]);if(off >0){if(out->planar==in->planar){intplanes=out->planar?out->ch_count:1;for(ch=0;ch< planes;ch++){ctx->simd_f(out-> ch ch
int ff_opus_psy_process(OpusPsyContext *s, OpusPacketInfo *p)
av_cold int ff_mdct15_init(MDCT15Context **ps, int inverse, int N, double scale)
int ff_opus_psy_celt_frame_process(OpusPsyContext *s, CeltFrame *f, int index)
const OptionDef options[]
int alloc_boost[CELT_MAX_BANDS]
static int bands_dist(OpusPsyContext *s, CeltFrame *f, float *total_dist)
av_cold AVFloatDSPContext * avpriv_float_dsp_alloc(int bit_exact)
Allocate a float DSP context.
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...
#define OPUS_BLOCK_SIZE(x)
int flags
AV_CODEC_FLAG_*.
av_cold int ff_opus_psy_init(OpusPsyContext *s, AVCodecContext *avctx, struct FFBufQueue *bufqueue, OpusEncOptions *options)
int tf_change[CELT_MAX_BANDS]
static const uint8_t offset[127][2]
static void celt_search_for_dual_stereo(OpusPsyContext *s, CeltFrame *f)
float * bands[OPUS_MAX_CHANNELS][CELT_MAX_BANDS]
const int8_t ff_celt_tf_select[4][2][2][2]
static void celt_gauge_psy_weight(OpusPsyContext *s, OpusPsyStep **start, CeltFrame *f_out)
static int celt_search_for_tf(OpusPsyContext *s, OpusPsyStep **start, CeltFrame *f)
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
float tone[OPUS_MAX_CHANNELS][CELT_MAX_BANDS]
float change_amp[OPUS_MAX_CHANNELS][CELT_MAX_BANDS]
static void psy_output_groups(OpusPsyContext *s)
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
int inflection_points_count
const uint8_t ff_celt_freq_range[]
float(* band_cost)(struct CeltPVQ *pvq, CeltFrame *f, OpusRangeCoder *rc, int band, float *bits, float lambda)
#define AV_LOG_INFO
Standard information.
int sample_rate
samples per second
main external API structure.
static int bessel_init(FFBesselFilter *s, float n, float f0, float fs, int highpass)
void ff_opus_psy_signal_eof(OpusPsyContext *s)
static void celt_search_for_intensity(OpusPsyContext *s, CeltFrame *f)
av_cold int ff_opus_psy_end(OpusPsyContext *s)
void ff_opus_psy_postencode_update(OpusPsyContext *s, CeltFrame *f, OpusRangeCoder *rc)
static int weight(int i, int blen, int offset)
av_cold void ff_mdct15_uninit(MDCT15Context **ps)
static const int16_t coeffs[]
static void step_collect_psy_metrics(OpusPsyContext *s, int index)
int channels
number of audio channels
float energy[OPUS_MAX_CHANNELS][CELT_MAX_BANDS]
static int64_t fsize(FILE *f)
static void search_for_change_points(OpusPsyContext *s, float tgt_change, int offset_s, int offset_e, int resolution, int level)
uint8_t ** extended_data
pointers to the data planes/channels.
void(* mdct)(struct MDCT15Context *s, float *dst, const float *src, ptrdiff_t stride)
int nb_samples
number of audio samples (per channel) described by this frame
OpusBandExcitation ex[OPUS_MAX_CHANNELS][CELT_MAX_BANDS]
static AVFrame * ff_bufqueue_peek(struct FFBufQueue *queue, unsigned index)
Get a buffer from the queue without altering it.