Go to the documentation of this file.
67 #define OFFSET(x) offsetof(AudioPsyClipContext, x)
68 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_RUNTIME_PARAM
85 for (
int i = 0;
i <
size;
i++) {
95 const int (*points)[2],
int num_points,
int sample_rate)
99 s->margin_curve[0] = points[0][1];
101 for (
int i = 0;
i < num_points - 1;
i++) {
102 while (j < s->fft_size / 2 + 1 && j *
sample_rate /
s->fft_size < points[
i + 1][0]) {
105 s->margin_curve[j] = points[
i][1] + (binHz - points[
i][0]) * (points[
i + 1][1] - points[
i][1]) / (points[
i + 1][0] - points[
i][0]);
110 while (j < s->fft_size / 2 + 1) {
111 s->margin_curve[j] = points[num_points - 1][1];
116 for (j = 0; j <
s->fft_size / 2 + 1; j++)
117 s->margin_curve[j] =
powf(10.
f,
s->margin_curve[j] / 20.f);
135 while (bin < s->num_psy_bins) {
137 int base_idx = table_index *
s->num_psy_bins;
138 int start_bin = bin * 3 / 4;
139 int end_bin =
FFMIN(
s->num_psy_bins, ((bin + 1) * 4 + 2) / 3);
142 for (
int j = start_bin; j < end_bin; j++) {
144 float rel_idx_log =
FFABS(logf((j + 0.5
f) / (bin + 0.5
f)));
155 s->spread_table[base_idx +
s->num_psy_bins / 2 + j - bin] =
value;
158 for (
int j = start_bin; j < end_bin; j++) {
159 s->spread_table[base_idx +
s->num_psy_bins / 2 + j - bin] /= sum;
162 s->spread_table_range[table_index][0] = start_bin - bin;
163 s->spread_table_range[table_index][1] = end_bin - bin;
168 if ((bin & (bin - 1)) == 0) {
177 for (
int i = bin;
i < next_bin;
i++)
178 s->spread_table_index[
i] = table_index;
189 static const int points[][2] = { {0,14}, {125,14}, {250,16}, {500,18}, {1000,20}, {2000,20}, {4000,20}, {8000,15}, {16000,5}, {20000,-10} };
190 static const int num_points = 10;
194 s->fft_size =
inlink->sample_rate > 100000 ? 1024 :
inlink->sample_rate > 50000 ? 512 : 256;
195 s->overlap =
s->fft_size / 4;
199 if (
inlink->sample_rate <= 50000) {
200 s->num_psy_bins =
s->fft_size / 2;
201 }
else if (
inlink->sample_rate <= 100000) {
202 s->num_psy_bins =
s->fft_size / 4;
204 s->num_psy_bins =
s->fft_size / 8;
208 s->inv_window =
av_calloc(
s->fft_size,
sizeof(*
s->inv_window));
209 if (!
s->window || !
s->inv_window)
219 if (!
s->in_buffer || !
s->in_frame ||
220 !
s->out_dist_frame || !
s->windowed_frame ||
221 !
s->clipping_delta || !
s->spectrum_buf || !
s->mask_curve)
226 s->margin_curve =
av_calloc(
s->fft_size / 2 + 1,
sizeof(*
s->margin_curve));
227 if (!
s->margin_curve)
230 s->spread_table_rows =
av_log2(
s->num_psy_bins) * 2;
231 s->spread_table =
av_calloc(
s->spread_table_rows *
s->num_psy_bins,
sizeof(*
s->spread_table));
232 if (!
s->spread_table)
235 s->spread_table_range =
av_calloc(
s->spread_table_rows * 2,
sizeof(*
s->spread_table_range));
236 if (!
s->spread_table_range)
239 s->spread_table_index =
av_calloc(
s->num_psy_bins,
sizeof(*
s->spread_table_index));
240 if (!
s->spread_table_index)
247 s->channels =
inlink->channels;
250 s->itx_ctx =
av_calloc(
s->channels,
sizeof(*
s->itx_ctx));
251 if (!
s->tx_ctx || !
s->itx_ctx)
254 for (
int ch = 0; ch <
s->channels; ch++) {
268 const float *in_frame,
float *out_frame,
const int add_to_out_frame)
270 const float *
window =
s->window;
272 for (
int i = 0;
i <
s->fft_size;
i++) {
273 if (add_to_out_frame) {
282 const float *spectrum,
float *mask_curve)
284 for (
int i = 0;
i <
s->fft_size / 2 + 1;
i++)
287 for (
int i = 0;
i <
s->num_psy_bins;
i++) {
288 int base_idx, start_bin, end_bin, table_idx;
293 magnitude =
FFABS(spectrum[0]);
294 }
else if (
i ==
s->fft_size / 2) {
295 magnitude =
FFABS(spectrum[1]);
300 magnitude = hypotf(spectrum[2 *
i], spectrum[2 *
i + 1]) * 2;
303 table_idx =
s->spread_table_index[
i];
304 range[0] =
s->spread_table_range[table_idx][0];
305 range[1] =
s->spread_table_range[table_idx][1];
306 base_idx = table_idx *
s->num_psy_bins;
307 start_bin =
FFMAX(0,
i + range[0]);
308 end_bin =
FFMIN(
s->num_psy_bins,
i + range[1]);
310 for (
int j = start_bin; j < end_bin; j++)
311 mask_curve[j] +=
s->spread_table[base_idx +
s->num_psy_bins / 2 + j -
i] * magnitude;
315 for (
int i =
s->num_psy_bins; i < s->fft_size / 2 + 1;
i++) {
317 if (
i ==
s->fft_size / 2) {
318 magnitude =
FFABS(spectrum[1]);
323 magnitude = hypotf(spectrum[2 *
i], spectrum[2 *
i + 1]) * 2;
326 mask_curve[
i] = magnitude;
329 for (
int i = 0;
i <
s->fft_size / 2 + 1;
i++)
330 mask_curve[
i] = mask_curve[
i] /
s->margin_curve[
i];
334 const float *windowed_frame,
float *clipping_delta,
float delta_boost)
336 const float *
window =
s->window;
338 for (
int i = 0;
i <
s->fft_size;
i++) {
340 const float effective_value = windowed_frame[
i] + clipping_delta[
i];
342 if (effective_value >
limit) {
343 clipping_delta[
i] += (
limit - effective_value) * delta_boost;
344 }
else if (effective_value < -
limit) {
345 clipping_delta[
i] += (-
limit - effective_value) * delta_boost;
351 float *clip_spectrum,
const float *mask_curve)
354 float relative_distortion_level =
FFABS(clip_spectrum[0]) / mask_curve[0];
356 if (relative_distortion_level > 1.
f)
357 clip_spectrum[0] /= relative_distortion_level;
360 for (
int i = 1;
i <
s->fft_size / 2;
i++) {
361 float real = clip_spectrum[
i * 2];
362 float imag = clip_spectrum[
i * 2 + 1];
366 relative_distortion_level = hypotf(real, imag) * 2 / mask_curve[
i];
367 if (relative_distortion_level > 1.0) {
368 clip_spectrum[
i * 2] /= relative_distortion_level;
369 clip_spectrum[
i * 2 + 1] /= relative_distortion_level;
373 relative_distortion_level =
FFABS(clip_spectrum[1]) / mask_curve[
s->fft_size / 2];
374 if (relative_distortion_level > 1.
f)
375 clip_spectrum[1] /= relative_distortion_level;
380 for (
int i =
size - 1;
i >= 0;
i--)
383 for (
int i =
size - 1;
i >= 0;
i--)
397 const float *in_samples,
float *out_samples,
int diff_only,
398 float *in_frame,
float *out_dist_frame,
399 float *windowed_frame,
float *clipping_delta,
400 float *spectrum_buf,
float *mask_curve)
403 const float clip_level_inv = 1.f /
s->clip_level;
404 const float level_out =
s->level_out;
409 for (
int i = 0;
i <
s->fft_size -
s->overlap;
i++) {
410 in_frame[
i] = in_frame[
i +
s->overlap];
411 out_dist_frame[
i] = out_dist_frame[
i +
s->overlap];
414 for (
int i = 0;
i <
s->overlap;
i++) {
415 in_frame[
i +
s->fft_size -
s->overlap] = in_samples[
i];
416 out_dist_frame[
i +
s->fft_size -
s->overlap] = 0.f;
420 r2c(windowed_frame,
s->fft_size);
421 s->tx_fn(
s->tx_ctx[ch], spectrum_buf, windowed_frame,
sizeof(
float));
422 c2r(windowed_frame,
s->fft_size);
428 for (
int i = 0;
i <
s->fft_size;
i++)
429 orig_peak =
FFMAX(orig_peak,
FFABS(windowed_frame[
i] *
s->inv_window[
i]));
430 orig_peak *= clip_level_inv;
434 for (
int i = 0;
i <
s->fft_size * 2;
i++)
435 clipping_delta[
i] = 0.
f;
438 for (
int i = 0;
i <
s->iterations;
i++) {
439 float mask_curve_shift = 1.122f;
441 float delta_boost = 1.f;
442 if (
i >=
s->iterations -
s->iterations / 3) {
450 r2c(clipping_delta,
s->fft_size);
451 s->tx_fn(
s->tx_ctx[ch], spectrum_buf, clipping_delta,
sizeof(
float));
455 s->itx_fn(
s->itx_ctx[ch], clipping_delta, spectrum_buf,
sizeof(
float));
456 c2r(clipping_delta,
s->fft_size);
458 for (
int i = 0;
i <
s->fft_size;
i++)
459 clipping_delta[
i] /=
s->fft_size;
462 for (
int i = 0;
i <
s->fft_size;
i++)
463 peak =
FFMAX(peak,
FFABS((windowed_frame[
i] + clipping_delta[
i]) *
s->inv_window[
i]));
464 peak *= clip_level_inv;
467 if (orig_peak > 1.
f && peak > 1.
f) {
468 float diff_achieved = orig_peak - peak;
469 if (
i + 1 <
s->iterations -
s->iterations / 3 && diff_achieved > 0) {
470 float diff_needed = orig_peak - 1.f;
471 float diff_ratio = diff_needed / diff_achieved;
476 diff_ratio =
FFMIN(diff_ratio, peak);
477 mask_curve_shift =
FFMAX(mask_curve_shift, diff_ratio);
481 mask_curve_shift =
FFMAX(mask_curve_shift, peak);
485 mask_curve_shift = 1.f + (mask_curve_shift - 1.f) *
s->adaptive;
489 for (
int i = 0;
i <
s->fft_size / 2 + 1;
i++)
490 mask_curve[
i] *= mask_curve_shift;
496 for (
int i = 0;
i <
s->overlap;
i++) {
498 if (!
ctx->is_disabled) {
499 out_samples[
i] = out_dist_frame[
i] / 1.5f;
501 out_samples[
i] += in_frame[
i];
503 out_samples[
i] *= clip_level_inv;
504 out_samples[
i] *= level_out;
506 out_samples[
i] = in_frame[
i];
515 float *in_buffer = (
float *)
s->in_buffer->extended_data[ch];
516 float *dst = (
float *)
out->extended_data[ch];
518 for (
int n = 0; n <
s->overlap; n++)
519 in_buffer[n] =
src[n] *
s->level_in;
521 feed(
ctx, ch, in_buffer, dst,
s->diff_only,
522 (
float *)(
s->in_frame->extended_data[ch]),
523 (
float *)(
s->out_dist_frame->extended_data[ch]),
524 (
float *)(
s->windowed_frame->extended_data[ch]),
525 (
float *)(
s->clipping_delta->extended_data[ch]),
526 (
float *)(
s->spectrum_buf->extended_data[ch]),
527 (
float *)(
s->mask_curve->extended_data[ch]));
536 const int start = (
out->channels * jobnr) / nb_jobs;
537 const int end = (
out->channels * (jobnr+1)) / nb_jobs;
539 for (
int ch = start; ch < end; ch++)
621 for (
int ch = 0; ch <
s->channels; ch++) {
651 .priv_class = &apsyclip_class,
const AVFilter ff_af_apsyclip
static void generate_hann_window(float *window, float *inv_window, int size)
AVFILTER_DEFINE_CLASS(apsyclip)
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
@ AV_SAMPLE_FMT_FLTP
float, planar
they must not be accessed directly The fifo field contains the frames that are queued in the input for processing by the filter The status_in and status_out fields contains the queued status(EOF or error) of the link
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
static void set_margin_curve(AudioPsyClipContext *s, const int(*points)[2], int num_points, int sample_rate)
static int psy_channel(AVFilterContext *ctx, AVFrame *in, AVFrame *out, int ch)
static void limit_clip_spectrum(AudioPsyClipContext *s, float *clip_spectrum, const float *mask_curve)
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
#define FILTER_SINGLE_SAMPLEFMT(sample_fmt_)
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
This structure describes decoded (raw) audio or video data.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
static const AVOption apsyclip_options[]
static int activate(AVFilterContext *ctx)
const char * name
Filter name.
A link between two filters.
#define FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink)
Forward the status on an output link to an input link.
int channels
Number of channels.
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...
static void calculate_mask_curve(AudioPsyClipContext *s, const float *spectrum, float *mask_curve)
static SDL_Window * window
static void apply_window(AudioPsyClipContext *s, const float *in_frame, float *out_frame, const int add_to_out_frame)
static av_always_inline float scale(float x, float s)
A filter pad used for either input or output.
void(* av_tx_fn)(AVTXContext *s, void *out, void *in, ptrdiff_t stride)
Function pointer to a function to perform the transform.
static void ff_outlink_set_status(AVFilterLink *link, int status, int64_t pts)
Set the status field of a link from the source filter.
static const AVFilterPad inputs[]
void ff_inlink_request_frame(AVFilterLink *link)
Mark that a frame is wanted on the link.
static void c2r(float *buffer, int size)
@ AV_TX_FLOAT_FFT
Standard complex to complex FFT with sample data type AVComplexFloat.
#define FILTER_INPUTS(array)
static void feed(AVFilterContext *ctx, int ch, const float *in_samples, float *out_samples, int diff_only, float *in_frame, float *out_dist_frame, float *windowed_frame, float *clipping_delta, float *spectrum_buf, float *mask_curve)
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Describe the class of an AVClass context structure.
int ff_inlink_consume_samples(AVFilterLink *link, unsigned min, unsigned max, AVFrame **rframe)
Take samples from the link's FIFO and update the link's stats.
int(* spread_table_range)[2]
static av_cold void uninit(AVFilterContext *ctx)
static void r2c(float *buffer, int size)
int ff_inlink_acknowledge_status(AVFilterLink *link, int *rstatus, int64_t *rpts)
Test and acknowledge the change of status on the link.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
static void clip_to_window(AudioPsyClipContext *s, const float *windowed_frame, float *clipping_delta, float delta_boost)
int ff_filter_process_command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
Generic processing of user supplied commands that are set in the same way as the filter options.
static const AVFilterPad outputs[]
av_cold void av_tx_uninit(AVTXContext **ctx)
Frees a context and sets ctx to NULL, does nothing when ctx == NULL.
int nb_samples
number of audio samples (per channel) described by this frame
#define i(width, name, range_min, range_max)
uint8_t ** extended_data
pointers to the data planes/channels.
#define increment(name, min, max)
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
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 default value
const char * name
Pad name.
int ff_inlink_queued_samples(AVFilterLink *link)
void * av_calloc(size_t nmemb, size_t size)
static double limit(double x)
static int psy_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
the frame and frame reference mechanism is intended to as much as expensive copies of that data while still allowing the filters to produce correct results The data is stored in buffers represented by AVFrame structures Several references can point to the same frame buffer
static void generate_spread_table(AudioPsyClipContext *s)
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
#define FILTER_OUTPUTS(array)
#define AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL
Same as AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, except that the filter will have its filter_frame() c...
the definition of that something depends on the semantic of the filter The callback must examine the status of the filter s links and proceed accordingly The status of output links is stored in the status_in and status_out fields and tested by the ff_outlink_frame_wanted() function. If this function returns true
static av_always_inline int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
static int config_input(AVFilterLink *inlink)
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
void ff_filter_set_ready(AVFilterContext *filter, unsigned priority)
Mark a filter ready and schedule it for activation.