Go to the documentation of this file.
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++) {
169 float c_change = 0.0f;
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;
274 for (
i = 0;
i <
s->inflection_points_count;
i++) {
275 if (
s->inflection_points[
i] >= step_offset) {
281 for (
i = start_offset;
i <
FFMIN(radius,
s->inflection_points_count - start_offset);
i++) {
282 if (
s->inflection_points[
i] < (step_offset + radius)) {
283 neighbouring_points++;
288 f->transient = neighbouring_points > 0;
302 f->skip_band_floor =
f->end_band;
303 f->intensity_stereo =
f->end_band;
315 float rate, frame_bits = 0;
322 float max_score = 1.0f;
327 float tonal_contrib = 0.0f;
328 for (
f = 0;
f < (1 <<
s->p.framesize);
f++) {
330 for (ch = 0; ch <
s->avctx->channels; ch++) {
332 tonal_contrib += start[
f]->
tone[ch][
i];
335 tonal += tonal_contrib;
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;
387 if (
s->avctx->channels < 2)
394 f->dual_stereo = td2 < td1;
395 s->dual_stereo_used += td2 < td1;
401 float dist, best_dist = FLT_MAX;
405 if (
s->avctx->channels < 2)
408 for (
i =
f->end_band;
i >= end_band;
i--) {
409 f->intensity_stereo =
i;
411 if (best_dist > dist) {
417 f->intensity_stereo = best_band;
418 s->avg_is_band = (
s->avg_is_band +
f->intensity_stereo)/2.0
f;
424 float score[2] = { 0 };
426 for (cway = 0; cway < 2; cway++) {
428 int base =
f->transient ? 120 : 960;
430 for (
i = 0;
i < 2;
i++) {
436 float iscore0 = 0.0f;
437 float iscore1 = 0.0f;
438 for (j = 0; j < (1 <<
f->size); j++) {
439 for (k = 0; k <
s->avctx->channels; k++) {
445 score[cway] +=
config[cway][
i] ? iscore1 : iscore0;
449 f->tf_select = score[0] < score[1];
457 int start_transient_flag =
f->transient;
468 if (
f->transient != start_transient_flag) {
470 s->redo_analysis = 1;
474 s->redo_analysis = 0;
486 for (
i = 0;
i < steps_out;
i++)
489 for (
i = 0;
i <
s->max_steps;
i++)
492 for (
i = 0;
i <
s->max_steps;
i++) {
493 const int i_new =
i - steps_out;
494 s->steps[i_new < 0 ?
s->max_steps + i_new : i_new] =
tmp[
i];
497 for (
i = steps_out;
i <
s->buffered_steps;
i++)
498 s->steps[
i]->index -= steps_out;
500 ideal_fbits =
s->avctx->bit_rate/(
s->avctx->sample_rate/
frame_size);
502 for (
i = 0;
i <
s->p.frames;
i++) {
503 s->avg_is_band +=
f[
i].intensity_stereo;
504 s->lambda *= ideal_fbits /
f[
i].framebits;
507 s->avg_is_band /= (
s->p.frames + 1);
510 s->steps_to_process = 0;
511 s->buffered_steps -= steps_out;
512 s->total_packets_out +=
s->p.frames;
513 s->inflection_points_count = 0;
521 s->redo_analysis = 0;
525 s->bufqueue = bufqueue;
526 s->max_steps =
ceilf(
s->options->max_delay_ms/2.5f);
529 s->inflection_points_count = 0;
531 s->inflection_points =
av_mallocz(
sizeof(*
s->inflection_points)*
s->max_steps);
532 if (!
s->inflection_points) {
543 for (ch = 0; ch <
s->avctx->channels; ch++) {
550 for (
i = 0;
i <
s->max_steps;
i++) {
582 for (
i = 0;
i <
s->max_steps;
i++)
605 for (
i = 0;
i <
s->max_steps;
i++)
609 av_log(
s->avctx,
AV_LOG_INFO,
"Dual Stereo used: %0.2f%%\n", ((
float)
s->dual_stereo_used/
s->total_packets_out)*100.0f);
float stereo[CELT_MAX_BANDS]
int ff_opus_psy_process(OpusPsyContext *s, OpusPacketInfo *p)
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
const uint8_t ff_celt_freq_bands[]
void ff_opus_psy_celt_frame_init(OpusPsyContext *s, CeltFrame *f, int index)
static void celt_search_for_dual_stereo(OpusPsyContext *s, CeltFrame *f)
This structure describes decoded (raw) audio or video data.
enum OpusBandwidth bandwidth
#define OPUS_RC_CHECKPOINT_SPAWN(rc)
av_cold int ff_mdct15_init(MDCT15Context **ps, int inverse, int N, double scale)
@ OPUS_BANDWIDTH_FULLBAND
float coeffs[OPUS_MAX_CHANNELS][OPUS_BLOCK_SIZE(CELT_BLOCK_960)]
static __device__ float ceilf(float a)
static int celt_search_for_tf(OpusPsyContext *s, OpusPsyStep **start, CeltFrame *f)
The official guide to swscale for confused that consecutive non overlapping rectangles of slice_bottom special converter These generally are unscaled converters of common like for each output line the vertical scaler pulls lines from a ring buffer When the ring buffer does not contain the wanted then it is pulled from the input slice through the input converter and horizontal scaler The result is also stored in the ring buffer to serve future vertical scaler requests When no more output can be generated because lines from a future slice would be then all remaining lines in the current slice are horizontally scaled and put in the ring buffer[This is done for luma and chroma, each with possibly different numbers of lines per picture.] Input to YUV Converter When the input to the main path is not planar bits per component YUV or bit it is converted to planar bit YUV Two sets of converters exist for this the other leaves the full chroma resolution
float change_amp[OPUS_MAX_CHANNELS][CELT_MAX_BANDS]
int flags
AV_CODEC_FLAG_*.
av_cold int ff_opus_psy_end(OpusPsyContext *s)
void ff_opus_psy_postencode_update(OpusPsyContext *s, CeltFrame *f, OpusRangeCoder *rc)
static int bessel_init(FFBesselFilter *s, float n, float f0, float fs, int highpass)
static void psy_output_groups(OpusPsyContext *s)
#define OPUS_BLOCK_SIZE(x)
const uint8_t ff_celt_band_end[]
int ff_opus_psy_celt_frame_process(OpusPsyContext *s, CeltFrame *f, int index)
int alloc_boost[CELT_MAX_BANDS]
static int64_t fsize(FILE *f)
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
void ff_celt_bitalloc(CeltFrame *f, OpusRangeCoder *rc, int encode)
static int bands_dist(OpusPsyContext *s, CeltFrame *f, float *total_dist)
static void generate_window_func(float *lut, int N, int win_func, float *overlap)
const uint8_t ff_celt_freq_range[]
static void celt_gauge_psy_weight(OpusPsyContext *s, OpusPsyStep **start, CeltFrame *f_out)
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
static int weight(int i, int blen, int offset)
const OptionDef options[]
float * bands[OPUS_MAX_CHANNELS][CELT_MAX_BANDS]
void ff_opus_rc_enc_init(OpusRangeCoder *rc)
float tone[OPUS_MAX_CHANNELS][CELT_MAX_BANDS]
#define OPUS_MAX_PACKET_SIZE
const int8_t ff_celt_tf_select[4][2][2][2]
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
#define AV_LOG_INFO
Standard information.
static float pvq_band_cost(CeltPVQ *pvq, CeltFrame *f, OpusRangeCoder *rc, int band, float *bits, float lambda)
int nb_samples
number of audio samples (per channel) described by this frame
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)
Structure holding the queue.
uint8_t ** extended_data
pointers to the data planes/channels.
void ff_opus_psy_signal_eof(OpusPsyContext *s)
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
av_cold void ff_mdct15_uninit(MDCT15Context **ps)
float energy[OPUS_MAX_CHANNELS][CELT_MAX_BANDS]
static av_always_inline uint32_t opus_rc_tell_frac(const OpusRangeCoder *rc)
main external API structure.
static float bessel_filter(FFBesselFilter *s, float x)
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
static void search_for_change_points(OpusPsyContext *s, float tgt_change, int offset_s, int offset_e, int resolution, int level)
av_cold int ff_opus_psy_init(OpusPsyContext *s, AVCodecContext *avctx, struct FFBufQueue *bufqueue, OpusEncOptions *options)
av_cold AVFloatDSPContext * avpriv_float_dsp_alloc(int bit_exact)
Allocate a float DSP context.
static void step_collect_psy_metrics(OpusPsyContext *s, int index)
static void celt_search_for_intensity(OpusPsyContext *s, CeltFrame *f)
static int flush_silent_frames(OpusPsyContext *s)
#define OPUS_RC_CHECKPOINT_BITS(rc)
#define OPUS_RC_CHECKPOINT_ROLLBACK(rc)
#define OPUS_SAMPLES_TO_BLOCK_SIZE(x)