32 float *
bits,
float lambda)
35 uint32_t
cm[2] = { (1 <<
f->blocks) - 1, (1 <<
f->blocks) - 1 };
37 float buf[176 * 2], lowband_scratch[176], norm1[176], norm2[176];
38 float dist, cost, err_x = 0.0f, err_y = 0.0f;
41 float *
Y = (
f->channels == 2) ? &buf[176] :
NULL;
45 memcpy(
X, X_orig, band_size*
sizeof(
float));
47 memcpy(
Y, Y_orig, band_size*
sizeof(
float));
50 if (band <= f->coded_bands - 1) {
51 int curr_balance =
f->remaining /
FFMIN(3,
f->coded_bands - band);
56 pvq->
quant_band(pvq,
f, rc, band,
X,
NULL, band_size,
b / 2,
f->blocks,
NULL,
57 f->size, norm1, 0, 1.0f, lowband_scratch,
cm[0]);
59 pvq->
quant_band(pvq,
f, rc, band,
Y,
NULL, band_size,
b / 2,
f->blocks,
NULL,
60 f->size, norm2, 0, 1.0f, lowband_scratch,
cm[1]);
62 pvq->
quant_band(pvq,
f, rc, band,
X,
Y, band_size,
b,
f->blocks,
NULL,
f->size,
63 norm1, 0, 1.0f, lowband_scratch,
cm[0] |
cm[1]);
66 for (
i = 0;
i < band_size;
i++) {
67 err_x += (
X[
i] - X_orig[
i])*(
X[
i] - X_orig[
i]);
69 err_y += (
Y[
i] - Y_orig[
i])*(
Y[
i] - Y_orig[
i]);
78 return lambda*dist*cost;
84 int silence = 0, ch,
i, j;
94 const int step_samples =
s->avctx->frame_size;
95 const int steps_per_half = half_samples / step_samples;
100 for (ch = 0; ch <
s->avctx->ch_layout.nb_channels; ch++) {
101 memset(
s->scratch, 0,
sizeof(
float) * (half_samples << 1));
104 const int offset = (steps_per_half -
i) * step_samples;
108 for (
i = 0;
i < steps_per_half;
i++) {
109 if (
index + 1 +
i >=
s->bufqueue->available)
111 const int offset = (steps_per_half +
i) * step_samples;
116 s->dsp->vector_fmul(
s->scratch,
s->scratch,
s->window[
s->bsize_analysis],
119 s->mdct_fn[
s->bsize_analysis](
s->mdct[
s->bsize_analysis], st->
coeffs[ch],
120 s->scratch,
sizeof(
float));
126 for (ch = 0; ch <
s->avctx->ch_layout.nb_channels; ch++) {
128 float avg_c_s, energy = 0.0f, dist_dev = 0.0f;
130 const float *coeffs = st->
bands[ch][
i];
131 for (j = 0; j <
range; j++)
132 energy += coeffs[j]*coeffs[j];
135 silence |= !!st->
energy[ch][
i];
136 avg_c_s = energy /
range;
138 for (j = 0; j <
range; j++) {
139 const float c_s = coeffs[j]*coeffs[j];
140 dist_dev += (avg_c_s - c_s)*(avg_c_s - c_s);
149 if (
s->avctx->ch_layout.nb_channels > 1) {
151 float incompat = 0.0f;
152 const float *coeffs1 = st->
bands[0][
i];
153 const float *coeffs2 = st->
bands[1][
i];
155 for (j = 0; j <
range; j++)
156 incompat += (coeffs1[j] - coeffs2[j])*(coeffs1[j] - coeffs2[j]);
161 for (ch = 0; ch <
s->avctx->ch_layout.nb_channels; ch++) {
183 int offset_s,
int offset_e,
int resolution,
187 float c_change = 0.0f;
188 if ((offset_e - offset_s) <= resolution)
190 for (
i = offset_s;
i < offset_e;
i++) {
191 c_change +=
s->steps[
i]->total_change;
192 if (c_change > tgt_change)
198 s->inflection_points[
s->inflection_points_count++] =
i;
210 const int step_samples =
s->avctx->frame_size;
211 int fsize, silent_frames;
213 for (silent_frames = 0; silent_frames <
s->buffered_steps; silent_frames++)
214 if (!
s->steps[silent_frames]->silence)
216 if (--silent_frames < 0)
221 const int steps_per_packet = packet_samples / step_samples;
223 if (steps_per_packet < 1 || silent_frames < steps_per_packet)
227 s->p.frames =
FFMIN(silent_frames / steps_per_packet,
239 int max_delay_samples = (
s->options->max_delay_ms*
s->avctx->sample_rate)/1000;
257 float total_energy_change = 0.0f;
259 if (
s->buffered_steps <
s->max_steps && !
s->eof) {
264 if (++
s->steps_to_process >= awin) {
266 s->steps_to_process = 0;
268 if ((++
s->buffered_steps) <
s->max_steps)
272 for (
i = 0;
i <
s->buffered_steps;
i++)
273 total_energy_change +=
s->steps[
i]->total_change;
276 s->buffered_steps, 1, 0);
280 p->frames =
s->p.frames;
281 p->framesize =
s->p.framesize;
283 p->bandwidth =
s->p.bandwidth;
290 int i, neighbouring_points = 0, start_offset = 0;
292 int step_offset = steps_per_frame*
index;
297 f->channels =
s->avctx->ch_layout.nb_channels;
298 f->size =
s->p.framesize;
300 for (
i = 0;
i < steps_per_frame;
i++)
301 silence &=
s->steps[
index * steps_per_frame +
i]->silence;
309 if (
s->eof && step_offset >=
s->buffered_steps)
312 f->silence = silence;
315 f->intensity_stereo =
f->end_band;
319 for (
i = 0;
i <
s->inflection_points_count;
i++) {
320 if (
s->inflection_points[
i] >= step_offset) {
326 for (
i = start_offset;
i <
FFMIN(steps_per_frame,
s->inflection_points_count - start_offset);
i++) {
327 if (
s->inflection_points[
i] < (step_offset + steps_per_frame)) {
328 neighbouring_points++;
333 f->transient = neighbouring_points > 0;
347 f->skip_band_floor =
f->end_band;
348 f->intensity_stereo =
f->end_band;
360 int steps_per_frame =
frame_size /
s->avctx->frame_size;
361 float rate, frame_bits = 0;
368 float max_score = 1.0f;
373 float tonal_contrib = 0.0f;
374 for (
f = 0;
f < steps_per_frame;
f++) {
376 for (ch = 0; ch <
s->avctx->ch_layout.nb_channels; ch++) {
378 tonal_contrib += start[
f]->
tone[ch][
i];
381 tonal += tonal_contrib;
388 if (band_score[
i] > max_score)
389 max_score = band_score[
i];
393 f_out->
alloc_boost[
i] = (int)((band_score[
i]/max_score)*3.0f);
433 if (
s->avctx->ch_layout.nb_channels < 2)
440 f->dual_stereo = td2 < td1;
441 s->dual_stereo_used += td2 < td1;
447 float dist, best_dist = FLT_MAX;
451 if (
s->avctx->ch_layout.nb_channels < 2)
454 for (
i =
f->end_band;
i >= end_band;
i--) {
455 f->intensity_stereo =
i;
457 if (best_dist > dist) {
463 f->intensity_stereo = best_band;
464 s->avg_is_band = (
s->avg_is_band +
f->intensity_stereo)/2.0f;
471 float score[2] = { 0 };
473 for (cway = 0; cway < 2; cway++) {
475 int base =
f->transient ? 120 : 960;
477 for (
i = 0;
i < 2;
i++) {
483 float iscore0 = 0.0f;
484 float iscore1 = 0.0f;
485 for (j = 0; j < steps_per_frame; j++) {
486 for (k = 0; k <
s->avctx->ch_layout.nb_channels; k++) {
491 config[cway][
i] =
FFABS(iscore0 - 1.0f) <
FFABS(iscore1 - 1.0f);
492 score[cway] += config[cway][
i] ? iscore1 : iscore0;
496 f->tf_select = score[0] < score[1];
504 int start_transient_flag =
f->transient;
516 if (
f->transient != start_transient_flag) {
527 int steps_out =
s->p.frames*(
frame_size/
s->avctx->frame_size);
531 for (
i = 0;
i < steps_out;
i++)
534 for (
i = 0;
i <
s->max_steps;
i++)
537 for (
i = 0;
i <
s->max_steps;
i++) {
538 const int i_new =
i - steps_out;
539 s->steps[i_new < 0 ?
s->max_steps + i_new : i_new] =
tmp[
i];
542 for (
i = steps_out;
i <
s->buffered_steps;
i++)
543 s->steps[
i]->index -= steps_out;
545 ideal_fbits =
s->avctx->bit_rate/(
s->avctx->sample_rate/
frame_size);
547 for (
i = 0;
i <
s->p.frames;
i++) {
548 s->avg_is_band +=
f[
i].intensity_stereo;
549 if (
f[
i].framebits > 0)
550 s->lambda *= ideal_fbits /
f[
i].framebits;
553 s->avg_is_band /= (
s->p.frames + 1);
555 s->steps_to_process = 0;
556 s->buffered_steps -= steps_out;
557 s->total_packets_out +=
s->p.frames;
558 s->inflection_points_count = 0;
569 s->bufqueue = bufqueue;
575 s->inflection_points_count = 0;
577 s->inflection_points =
av_mallocz(
sizeof(*
s->inflection_points)*
s->max_steps);
578 if (!
s->inflection_points) {
589 for (ch = 0; ch <
s->avctx->ch_layout.nb_channels; ch++) {
596 for (
i = 0;
i <
s->max_steps;
i++) {
615 0, 15 << (
i + 3), &
scale, 0);
631 for (
i = 0;
i <
s->max_steps;
i++)
654 for (
i = 0;
i <
s->max_steps;
i++)
658 av_log(
s->avctx,
AV_LOG_INFO,
"Dual Stereo used: %0.2f%%\n", ((
float)
s->dual_stereo_used/
s->total_packets_out)*100.0f);
static int64_t fsize(FILE *f)
static AVFrame * ff_bufqueue_peek(struct FFBufQueue *queue, unsigned index)
Get a buffer from the queue without altering it.
#define i(width, name, range_min, range_max)
void ff_celt_bitalloc(CeltFrame *f, OpusRangeCoder *rc, int encode)
#define CELT_SHORT_BLOCKSIZE
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
static __device__ float sqrtf(float a)
static __device__ float ceilf(float a)
#define OPUS_SAMPLES_TO_BLOCK_SIZE(x)
#define OPUS_BLOCK_SIZE(x)
static int bands_dist(OpusPsyContext *s, CeltFrame *f, float *total_dist)
static void step_collect_psy_metrics(OpusPsyContext *s, int index)
void ff_opus_psy_postencode_update(OpusPsyContext *s, CeltFrame *f)
static int celt_search_for_tf(OpusPsyContext *s, OpusPsyStep **start, CeltFrame *f)
static void celt_gauge_psy_weight(OpusPsyContext *s, OpusPsyStep **start, CeltFrame *f_out)
av_cold int ff_opus_psy_end(OpusPsyContext *s)
int ff_opus_psy_celt_frame_process(OpusPsyContext *s, CeltFrame *f, int index)
int ff_opus_psy_process(OpusPsyContext *s, OpusPacketInfo *p)
av_cold int ff_opus_psy_init(OpusPsyContext *s, AVCodecContext *avctx, struct FFBufQueue *bufqueue, OpusEncOptions *options)
static void celt_search_for_intensity(OpusPsyContext *s, CeltFrame *f)
static float pvq_band_cost(CeltPVQ *pvq, CeltFrame *f, OpusRangeCoder *rc, int band, float *bits, float lambda)
static void celt_search_for_dual_stereo(OpusPsyContext *s, CeltFrame *f)
void ff_opus_psy_signal_eof(OpusPsyContext *s)
void ff_opus_psy_celt_frame_init(OpusPsyContext *s, CeltFrame *f, int index)
static int flush_silent_frames(OpusPsyContext *s)
static void psy_output_groups(OpusPsyContext *s)
static void search_for_change_points(OpusPsyContext *s, float tgt_change, int offset_s, int offset_e, int resolution, int level)
static float bessel_filter(FFBesselFilter *s, float x)
static int bessel_init(FFBesselFilter *s, float n, float f0, float fs, int highpass)
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_LOG_INFO
Standard information.
static void scale(int *out, const int *in, const int w, const int h, const int shift)
av_cold AVFloatDSPContext * avpriv_float_dsp_alloc(int bit_exact)
Allocate a float DSP context.
Memory handling functions.
@ OPUS_BANDWIDTH_FULLBAND
#define OPUS_MAX_FRAME_SIZE
void ff_opus_rc_enc_init(OpusRangeCoder *rc)
#define OPUS_RC_CHECKPOINT_ROLLBACK(rc)
#define OPUS_RC_CHECKPOINT_SPAWN(rc)
#define OPUS_RC_CHECKPOINT_BITS(rc)
static av_always_inline uint32_t opus_rc_tell_frac(const OpusRangeCoder *rc)
const h264_weight_func weight
main external API structure.
int sample_rate
samples per second
int flags
AV_CODEC_FLAG_*.
int frame_size
Number of samples per channel in an audio frame.
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.
int alloc_boost[CELT_MAX_BANDS]
Structure holding the queue.
float coeffs[OPUS_MAX_CHANNELS][OPUS_BLOCK_SIZE(CELT_BLOCK_960)]
float tone[OPUS_MAX_CHANNELS][CELT_MAX_BANDS]
float stereo[CELT_MAX_BANDS]
float change_amp[OPUS_MAX_CHANNELS][CELT_MAX_BANDS]
float * bands[OPUS_MAX_CHANNELS][CELT_MAX_BANDS]
float energy[OPUS_MAX_CHANNELS][CELT_MAX_BANDS]
const uint8_t ff_celt_freq_range[]
const uint8_t ff_celt_band_end[]
const uint8_t ff_celt_freq_bands[]
const int8_t ff_celt_tf_select[4][2][2][2]
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.
static void generate_window_func(float *lut, int N, int win_func, float *overlap)