29 float *bits,
float lambda)
34 float buf[176 * 2], lowband_scratch[176], norm1[176], norm2[176];
35 float dist, cost, err_x = 0.0f, err_y = 0.0f;
42 memcpy(X, X_orig, band_size*
sizeof(
float));
44 memcpy(Y, Y_orig, band_size*
sizeof(
float));
47 if (band <= f->coded_bands - 1) {
53 pvq->
quant_band(pvq, f, rc, band, X,
NULL, band_size, b / 2, f->
blocks,
NULL,
54 f->
size, norm1, 0, 1.0f, lowband_scratch, cm[0]);
56 pvq->
quant_band(pvq, f, rc, band, Y,
NULL, band_size, b / 2, f->
blocks,
NULL,
57 f->
size, norm2, 0, 1.0f, lowband_scratch, cm[1]);
59 pvq->
quant_band(pvq, f, rc, band, X, Y, band_size, b, f->
blocks,
NULL, f->
size,
60 norm1, 0, 1.0f, lowband_scratch, cm[0] | cm[1]);
63 for (i = 0; i < band_size; i++) {
64 err_x += (X[i] - X_orig[i])*(X[i] - X_orig[i]);
66 err_y += (Y[i] - Y_orig[i])*(Y[i] - Y_orig[i]);
69 dist = sqrtf(err_x) + sqrtf(err_y);
75 return lambda*dist*cost;
81 int silence = 0,
ch, i, j;
88 for (i = 1; i <=
FFMIN(lap_size, index); i++) {
93 for (i = 0; i < lap_size; i++) {
94 const int offset = i*120 + lap_size;
110 float avg_c_s, energy = 0.0f, dist_dev = 0.0f;
113 for (j = 0; j < range; j++)
114 energy += coeffs[j]*coeffs[j];
118 avg_c_s = energy / range;
120 for (j = 0; j < range; j++) {
121 const float c_s = coeffs[j]*coeffs[j];
122 dist_dev += (avg_c_s - c_s)*(avg_c_s - c_s);
125 st->
tone[
ch][i] += sqrtf(dist_dev);
133 float incompat = 0.0f;
134 const float *coeffs1 = st->
bands[0][i];
135 const float *coeffs2 = st->
bands[1][i];
137 for (j = 0; j < range; j++)
138 incompat += (coeffs1[j] - coeffs2[j])*(coeffs1[j] - coeffs2[j]);
139 st->
stereo[i] = sqrtf(incompat);
165 int offset_s,
int offset_e,
int resolution,
169 float c_change = 0.0f;
170 if ((offset_e - offset_s) <= resolution)
172 for (i = offset_s; i < offset_e; i++) {
174 if (c_change > tgt_change)
186 int fsize, silent_frames;
188 for (silent_frames = 0; silent_frames < s->
buffered_steps; silent_frames++)
191 if (--silent_frames < 0)
195 if ((1 << fsize) > silent_frames)
197 s->
p.
frames =
FFMIN(silent_frames / (1 << fsize), 48 >> fsize);
226 float total_energy_change = 0.0f;
256 int i, neighbouring_points = 0, start_offset = 0;
257 int radius = (1 << s->
p.
framesize), step_offset = radius*index;
265 for (i = 0; i < (1 << f->
size); i++)
283 neighbouring_points++;
307 memset(f->
alloc_boost, 0,
sizeof(
int)*CELT_MAX_BANDS);
315 float rate, frame_bits = 0;
322 float max_score = 1.0f;
327 float tonal_contrib = 0.0f;
329 weight = start[f]->
stereo[i];
332 tonal_contrib += start[f]->
tone[
ch][i];
335 tonal += tonal_contrib;
339 tonal /= (float)CELT_MAX_BANDS;
342 if (band_score[i] > max_score)
343 max_score = band_score[i];
348 frame_bits += band_score[i]*8.0f;
354 rate = ((float)s->
avctx->
bit_rate) + frame_bits*frame_size*16;
401 float dist, best_dist = FLT_MAX;
408 for (i = f->
end_band; i >= end_band; i--) {
411 if (best_dist > dist) {
424 float score[2] = { 0 };
426 for (cway = 0; cway < 2; cway++) {
430 for (i = 0; i < 2; i++) {
432 mag[i] = c < 0 ? base >>
FFABS(c) : base <<
FFABS(c);
436 float iscore0 = 0.0f;
437 float iscore1 = 0.0f;
438 for (j = 0; j < (1 << f->
size); j++) {
440 iscore0 += start[j]->
tone[k][i]*start[j]->
change_amp[k][i]/mag[0];
441 iscore1 += start[j]->
tone[k][i]*start[j]->
change_amp[k][i]/mag[1];
444 config[cway][i] =
FFABS(iscore0 - 1.0f) <
FFABS(iscore1 - 1.0f);
445 score[cway] += config[cway][i] ? iscore1 : iscore0;
468 if (f->
transient != start_transient_flag) {
482 int steps_out = s->
p.
frames*(frame_size/120);
486 for (i = 0; i < steps_out; i++)
490 tmp[i] = s->
steps[i];
493 const int i_new = i - steps_out;
502 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
#define OPUS_RC_CHECKPOINT_SPAWN(rc)
This structure describes decoded (raw) audio or video data.
float * window[CELT_BLOCK_NB]
void ff_opus_rc_enc_init(OpusRangeCoder *rc)
float coeffs[CELT_MAX_FRAME_SIZE]
int64_t bit_rate
the average bitrate
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[]
uint8_t pi<< 24) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_U8,(uint64_t)((*(const uint8_t *) pi - 0x80U))<< 56) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16,(*(const int16_t *) pi >>8)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S16,(uint64_t)(*(const int16_t *) pi)<< 48) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32,(*(const int32_t *) pi >>24)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S32,(uint64_t)(*(const int32_t *) pi)<< 32) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S64,(*(const int64_t *) pi >>56)+0x80) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S64, *(const int64_t *) pi *(1.0f/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S64, *(const int64_t *) pi *(1.0/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_FLT, llrintf(*(const float *) pi *(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_DBL, llrint(*(const double *) pi *(INT64_C(1)<< 63))) #define FMT_PAIR_FUNC(out, in) static conv_func_type *const fmt_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_S32, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_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), };static void cpy1(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, len);} static void cpy2(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, 2 *len);} static void cpy4(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, 4 *len);} static void cpy8(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, 8 *len);} AudioConvert *swri_audio_convert_alloc(enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, const int *ch_map, int flags) { 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) return NULL;ctx=av_mallocz(sizeof(*ctx));if(!ctx) return NULL;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)){ case 1:ctx->simd_f=cpy1;break;case 2:ctx->simd_f=cpy2;break;case 4:ctx->simd_f=cpy4;break;case 8:ctx->simd_f=cpy8;break;} } if(HAVE_X86ASM &&HAVE_MMX) 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);return ctx;} void swri_audio_convert_free(AudioConvert **ctx) { av_freep(ctx);} int swri_audio_convert(AudioConvert *ctx, AudioData *out, AudioData *in, int len) { int ch;int off=0;const int os=(out->planar ? 1 :out->ch_count) *out->bps;unsigned misaligned=0;av_assert0(ctx->channels==out->ch_count);if(ctx->in_simd_align_mask) { int planes=in->planar ? in->ch_count :1;unsigned m=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) { int planes=out->planar ? out->ch_count :1;unsigned m=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){ int planes=out->planar ? out->ch_count :1;for(ch=0;ch< planes;ch++){ ctx->simd_f(out-> ch ch
static void generate_window_func(float *lut, int N, int win_func, float *overlap)
#define OPUS_MAX_PACKET_SIZE
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]
void(* mdct)(struct MDCT15Context *s, float *dst, const float *src, ptrdiff_t stride)
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...
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)
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.
#define OPUS_BLOCK_SIZE(x)
static float pvq_band_cost(CeltPVQ *pvq, CeltFrame *f, OpusRangeCoder *rc, int band, float *bits, float lambda)
#define OPUS_RC_CHECKPOINT_ROLLBACK(rc)
int flags
AV_CODEC_FLAG_*.
av_cold int ff_opus_psy_init(OpusPsyContext *s, AVCodecContext *avctx, struct FFBufQueue *bufqueue, OpusEncOptions *options)
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
int tf_change[CELT_MAX_BANDS]
int pulses[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]
#define OPUS_RC_CHECKPOINT_BITS(rc)
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[]
#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)
void ff_celt_bitalloc(CeltFrame *f, OpusRangeCoder *rc, int encode)
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)
const OptionDef options[]
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.
int nb_samples
number of audio samples (per channel) described by this frame
OpusBandExcitation ex[OPUS_MAX_CHANNELS][CELT_MAX_BANDS]
static av_always_inline uint32_t opus_rc_tell_frac(const OpusRangeCoder *rc)
static AVFrame * ff_bufqueue_peek(struct FFBufQueue *queue, unsigned index)
Get a buffer from the queue without altering it.