29 float *
bits,
float lambda)
32 uint32_t
cm[2] = { (1 <<
f->blocks) - 1, (1 <<
f->blocks) - 1 };
34 float buf[176 * 2], lowband_scratch[176], norm1[176], norm2[176];
35 float dist, cost, err_x = 0.0f, err_y = 0.0f;
38 float *
Y = (
f->channels == 2) ? &buf[176] :
NULL;
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) {
48 int curr_balance =
f->remaining /
FFMIN(3,
f->coded_bands - band);
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;
86 for (ch = 0; ch <
s->avctx->channels; ch++) {
87 const int lap_size = (1 <<
s->bsize_analysis);
93 for (
i = 0;
i < lap_size;
i++) {
94 const int offset =
i*120 + lap_size;
99 s->dsp->vector_fmul(
s->scratch,
s->scratch,
s->window[
s->bsize_analysis],
102 s->mdct[
s->bsize_analysis]->mdct(
s->mdct[
s->bsize_analysis], st->
coeffs[ch],
s->scratch, 1);
108 for (ch = 0; ch <
s->avctx->channels; ch++) {
110 float avg_c_s, energy = 0.0f, dist_dev = 0.0f;
112 const float *coeffs = st->
bands[ch][
i];
113 for (j = 0; j < range; j++)
114 energy += coeffs[j]*coeffs[j];
116 st->
energy[ch][
i] += sqrtf(energy);
117 silence |= !!st->
energy[ch][
i];
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);
131 if (
s->avctx->channels > 1) {
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);
143 for (ch = 0; ch <
s->avctx->channels; ch++) {
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++) {
173 c_change +=
s->steps[
i]->total_change;
174 if (c_change > tgt_change)
180 s->inflection_points[
s->inflection_points_count++] =
i;
186 int fsize, silent_frames;
188 for (silent_frames = 0; silent_frames <
s->buffered_steps; silent_frames++)
189 if (!
s->steps[silent_frames]->silence)
191 if (--silent_frames < 0)
195 if ((1 <<
fsize) > silent_frames)
208 int max_delay_samples = (
s->options->max_delay_ms*
s->avctx->sample_rate)/1000;
226 float total_energy_change = 0.0f;
228 if (
s->buffered_steps <
s->max_steps && !
s->eof) {
229 const int awin = (1 <<
s->bsize_analysis);
230 if (++
s->steps_to_process >= awin) {
232 s->steps_to_process = 0;
234 if ((++
s->buffered_steps) <
s->max_steps)
238 for (
i = 0;
i <
s->buffered_steps;
i++)
239 total_energy_change +=
s->steps[
i]->total_change;
242 s->buffered_steps, 1, 0);
256 int i, neighbouring_points = 0, start_offset = 0;
257 int radius = (1 <<
s->p.framesize), step_offset = radius*
index;
262 f->channels =
s->avctx->channels;
263 f->size =
s->p.framesize;
265 for (
i = 0;
i < (1 <<
f->size);
i++)
266 silence &=
s->steps[
index*(1 <<
f->size) +
i]->silence;
268 f->silence = silence;
271 f->intensity_stereo =
f->end_band;
275 for (
i = 0;
i <
s->inflection_points_count;
i++) {
276 if (
s->inflection_points[
i] >= step_offset) {
282 for (
i = start_offset;
i <
FFMIN(radius,
s->inflection_points_count - start_offset);
i++) {
283 if (
s->inflection_points[
i] < (step_offset + radius)) {
284 neighbouring_points++;
289 f->transient = neighbouring_points > 0;
303 f->skip_band_floor =
f->end_band;
304 f->intensity_stereo =
f->end_band;
316 float rate, frame_bits = 0;
323 float max_score = 1.0f;
328 float tonal_contrib = 0.0f;
329 for (
f = 0;
f < (1 <<
s->p.framesize);
f++) {
331 for (ch = 0; ch <
s->avctx->channels; ch++) {
333 tonal_contrib += start[
f]->
tone[ch][
i];
336 tonal += tonal_contrib;
343 if (band_score[
i] > max_score)
344 max_score = band_score[
i];
349 frame_bits += band_score[
i]*8.0f;
355 rate = ((float)
s->avctx->bit_rate) + frame_bits*
frame_size*16;
388 if (
s->avctx->channels < 2)
395 f->dual_stereo = td2 < td1;
396 s->dual_stereo_used += td2 < td1;
402 float dist, best_dist = FLT_MAX;
406 if (
s->avctx->channels < 2)
409 for (
i =
f->end_band;
i >= end_band;
i--) {
410 f->intensity_stereo =
i;
412 if (best_dist > dist) {
418 f->intensity_stereo = best_band;
419 s->avg_is_band = (
s->avg_is_band +
f->intensity_stereo)/2.0f;
425 float score[2] = { 0 };
427 for (cway = 0; cway < 2; cway++) {
429 int base =
f->transient ? 120 : 960;
431 for (
i = 0;
i < 2;
i++) {
437 float iscore0 = 0.0f;
438 float iscore1 = 0.0f;
439 for (j = 0; j < (1 <<
f->size); j++) {
440 for (k = 0; k <
s->avctx->channels; k++) {
445 config[cway][
i] =
FFABS(iscore0 - 1.0f) <
FFABS(iscore1 - 1.0f);
446 score[cway] += config[cway][
i] ? iscore1 : iscore0;
450 f->tf_select = score[0] < score[1];
458 int start_transient_flag =
f->transient;
469 if (
f->transient != start_transient_flag) {
471 s->redo_analysis = 1;
475 s->redo_analysis = 0;
487 for (
i = 0;
i < steps_out;
i++)
490 for (
i = 0;
i <
s->max_steps;
i++)
493 for (
i = 0;
i <
s->max_steps;
i++) {
494 const int i_new =
i - steps_out;
495 s->steps[i_new < 0 ?
s->max_steps + i_new : i_new] =
tmp[
i];
498 for (
i = steps_out;
i <
s->buffered_steps;
i++)
499 s->steps[
i]->index -= steps_out;
501 ideal_fbits =
s->avctx->bit_rate/(
s->avctx->sample_rate/
frame_size);
503 for (
i = 0;
i <
s->p.frames;
i++) {
504 s->avg_is_band +=
f[
i].intensity_stereo;
505 s->lambda *= ideal_fbits /
f[
i].framebits;
508 s->avg_is_band /= (
s->p.frames + 1);
511 s->steps_to_process = 0;
512 s->buffered_steps -= steps_out;
513 s->total_packets_out +=
s->p.frames;
514 s->inflection_points_count = 0;
522 s->redo_analysis = 0;
526 s->bufqueue = bufqueue;
527 s->max_steps =
ceilf(
s->options->max_delay_ms/2.5f);
530 s->inflection_points_count = 0;
532 s->inflection_points =
av_mallocz(
sizeof(*
s->inflection_points)*
s->max_steps);
533 if (!
s->inflection_points) {
544 for (ch = 0; ch <
s->avctx->channels; ch++) {
551 for (
i = 0;
i <
s->max_steps;
i++) {
583 for (
i = 0;
i <
s->max_steps;
i++)
606 for (
i = 0;
i <
s->max_steps;
i++)
610 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 FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
static __device__ float ceilf(float a)
const OptionDef options[]
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
#define AV_LOG_INFO
Standard information.
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
static int weight(int i, int blen, int offset)
av_cold AVFloatDSPContext * avpriv_float_dsp_alloc(int bit_exact)
Allocate a float DSP context.
av_cold void ff_mdct15_uninit(MDCT15Context **ps)
av_cold int ff_mdct15_init(MDCT15Context **ps, int inverse, int N, double scale)
void ff_celt_bitalloc(CeltFrame *f, OpusRangeCoder *rc, int encode)
@ OPUS_BANDWIDTH_FULLBAND
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)
#define OPUS_MAX_PACKET_SIZE
#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)
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)
void ff_opus_psy_postencode_update(OpusPsyContext *s, CeltFrame *f, OpusRangeCoder *rc)
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)
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]
main external API structure.
int flags
AV_CODEC_FLAG_*.
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.
enum OpusBandwidth bandwidth
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]
static const uint8_t offset[127][2]
static void generate_window_func(float *lut, int N, int win_func, float *overlap)