Go to the documentation of this file.
35 #define FF_BUFQUEUE_SIZE (1024)
43 #define MAX_ITEMS 882000
44 #define MIN_PEAK (1. / 32768.)
90 const uint8_t *srcp,
int nb_samples);
95 #define OFFSET(x) offsetof(SpeechNormalizerContext, x)
96 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
128 if (pi[start].
type == 0)
132 while (start != end) {
136 if (pi[start].
type == 0)
139 sum += pi[start].
size;
149 int min_pi_nb_samples;
151 min_pi_nb_samples =
get_pi_samples(
s->cc[0].pi,
s->cc[0].pi_start,
s->cc[0].pi_end,
s->cc[0].pi_size);
152 for (
int ch = 1; ch <
inlink->ch_layout.nb_channels && min_pi_nb_samples > 0; ch++) {
158 return min_pi_nb_samples;
163 if (cc->
pi_size >= nb_samples) {
171 double pi_rms_sum,
int pi_size)
174 const double compression = 1. /
s->max_compression;
175 const int type =
s->invert ? pi_max_peak <=
s->threshold_value : pi_max_peak >=
s->threshold_value;
176 double expansion =
FFMIN(
s->max_expansion,
s->peak_value / pi_max_peak);
178 if (
s->rms_value > DBL_EPSILON)
179 expansion =
FFMIN(expansion,
s->rms_value / sqrt(pi_rms_sum / pi_size));
221 while (
size <= max_size) {
236 #define ANALYZE_CHANNEL(name, ptype, zero, min_peak) \
237 static void analyze_channel_## name (AVFilterContext *ctx, ChannelContext *cc, \
238 const uint8_t *srcp, int nb_samples) \
240 SpeechNormalizerContext *s = ctx->priv; \
241 const ptype *src = (const ptype *)srcp; \
242 const int max_period = s->max_period; \
243 PeriodItem *pi = (PeriodItem *)&cc->pi; \
244 int pi_end = cc->pi_end; \
248 cc->state = src[0] >= zero; \
250 while (n < nb_samples) { \
251 ptype new_max_peak; \
255 if ((cc->state != (src[n] >= zero)) || \
256 (pi[pi_end].size > max_period)) { \
257 ptype max_peak = pi[pi_end].max_peak; \
258 ptype rms_sum = pi[pi_end].rms_sum; \
259 int state = cc->state; \
261 cc->state = src[n] >= zero; \
262 av_assert1(pi[pi_end].size > 0); \
263 if (max_peak >= min_peak || \
264 pi[pi_end].size > max_period) { \
265 pi[pi_end].type = 1; \
267 if (pi_end >= MAX_ITEMS) \
269 if (cc->state != state) { \
270 pi[pi_end].max_peak = DBL_MIN; \
271 pi[pi_end].rms_sum = 0.0; \
273 pi[pi_end].max_peak = max_peak; \
274 pi[pi_end].rms_sum = rms_sum; \
276 pi[pi_end].type = 0; \
277 pi[pi_end].size = 0; \
278 av_assert1(pi_end != cc->pi_start); \
282 new_max_peak = pi[pi_end].max_peak; \
283 new_rms_sum = pi[pi_end].rms_sum; \
284 new_size = pi[pi_end].size; \
286 while (src[n] >= zero) { \
287 new_max_peak = FFMAX(new_max_peak, src[n]); \
288 new_rms_sum += src[n] * src[n]; \
291 if (n >= nb_samples) \
295 while (src[n] < zero) { \
296 new_max_peak = FFMAX(new_max_peak, -src[n]); \
297 new_rms_sum += src[n] * src[n]; \
300 if (n >= nb_samples) \
305 pi[pi_end].max_peak = new_max_peak; \
306 pi[pi_end].rms_sum = new_rms_sum; \
307 pi[pi_end].size = new_size; \
309 cc->pi_end = pi_end; \
315 #define FILTER_CHANNELS(name, ptype) \
316 static void filter_channels_## name (AVFilterContext *ctx, \
317 AVFrame *in, AVFrame *out, int nb_samples) \
319 SpeechNormalizerContext *s = ctx->priv; \
320 AVFilterLink *inlink = ctx->inputs[0]; \
322 for (int ch = 0; ch < inlink->ch_layout.nb_channels; ch++) { \
323 ChannelContext *cc = &s->cc[ch]; \
324 const ptype *src = (const ptype *)in->extended_data[ch]; \
325 ptype *dst = (ptype *)out->extended_data[ch]; \
326 enum AVChannel channel = av_channel_layout_channel_from_index(&inlink->ch_layout, ch); \
327 const int bypass = av_channel_layout_index_from_channel(&s->ch_layout, channel) < 0; \
330 while (n < nb_samples) { \
334 next_pi(ctx, cc, bypass); \
335 size = FFMIN(nb_samples - n, cc->pi_size); \
336 av_assert1(size > 0); \
337 gain = cc->gain_state; \
338 consume_pi(cc, size); \
339 for (int i = n; !ctx->is_disabled && i < n + size; i++) \
340 dst[i] = src[i] * gain; \
359 #define FILTER_LINK_CHANNELS(name, ptype, tlerp) \
360 static void filter_link_channels_## name (AVFilterContext *ctx, \
361 AVFrame *in, AVFrame *out, \
364 SpeechNormalizerContext *s = ctx->priv; \
365 AVFilterLink *inlink = ctx->inputs[0]; \
368 while (n < nb_samples) { \
369 int min_size = nb_samples - n; \
370 ptype gain = s->max_expansion; \
372 for (int ch = 0; ch < inlink->ch_layout.nb_channels; ch++) { \
373 ChannelContext *cc = &s->cc[ch]; \
375 enum AVChannel channel = av_channel_layout_channel_from_index(&inlink->ch_layout, ch); \
376 cc->bypass = av_channel_layout_index_from_channel(&s->ch_layout, channel) < 0; \
378 next_pi(ctx, cc, cc->bypass); \
379 min_size = FFMIN(min_size, cc->pi_size); \
382 av_assert1(min_size > 0); \
383 for (int ch = 0; ch < inlink->ch_layout.nb_channels; ch++) { \
384 ChannelContext *cc = &s->cc[ch]; \
388 gain = FFMIN(gain, min_gain(ctx, cc, min_size)); \
391 for (int ch = 0; ch < inlink->ch_layout.nb_channels; ch++) { \
392 ChannelContext *cc = &s->cc[ch]; \
393 const ptype *src = (const ptype *)in->extended_data[ch]; \
394 ptype *dst = (ptype *)out->extended_data[ch]; \
396 consume_pi(cc, min_size); \
400 for (int i = n; !ctx->is_disabled && i < n + min_size; i++) { \
401 ptype g = tlerp(s->prev_gain, gain, (i - n) / (ptype)min_size); \
402 dst[i] = src[i] * g; \
406 s->prev_gain = gain; \
421 while (
s->queue.available > 0) {
422 int min_pi_nb_samples;
430 if (min_pi_nb_samples < in->nb_samples && !
s->eof)
446 s->filter_channels[
s->link](
ctx, in,
out, in->nb_samples);
467 for (
int ch = 0; ch <
inlink->ch_layout.nb_channels; ch++) {
488 if (strcmp(
s->ch_layout_str,
"all"))
504 s->queue.available == 0) {
509 if (
s->queue.available > 0) {
529 s->max_period =
inlink->sample_rate / 10;
536 for (
int ch = 0; ch <
inlink->ch_layout.nb_channels; ch++) {
545 s->analyze_channel = analyze_channel_flt;
546 s->filter_channels[0] = filter_channels_flt;
547 s->filter_channels[1] = filter_link_channels_flt;
550 s->analyze_channel = analyze_channel_dbl;
551 s->filter_channels[0] = filter_channels_dbl;
552 s->filter_channels[1] = filter_link_channels_dbl;
562 char *res,
int res_len,
int flags)
595 .
name =
"speechnorm",
598 .priv_class = &speechnorm_class,
static const AVFilterPad inputs[]
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
static int get_pi_samples(PeriodItem *pi, int start, int end, int remain)
static int mix(int c0, int c1)
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 consume_pi(ChannelContext *cc, int nb_samples)
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
#define AVERROR_EOF
End of file.
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
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.
AVChannelLayout ch_layout
void(* filter_channels[2])(AVFilterContext *ctx, AVFrame *in, AVFrame *out, int nb_samples)
const char * name
Filter name.
static double next_gain(AVFilterContext *ctx, double pi_max_peak, int bypass, double state, double pi_rms_sum, int pi_size)
A link between two filters.
#define FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink)
Forward the status on an output link to an input link.
static AVFrame * ff_bufqueue_get(struct FFBufQueue *queue)
Get the first buffer from the queue and remove it.
int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe)
Take a frame from the link's FIFO and update the link's stats.
static double dlerp(double min, double max, double mix)
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 type
AVFILTER_DEFINE_CLASS(speechnorm)
A filter pad used for either input or output.
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 int available_samples(AVFilterContext *ctx)
#define av_assert0(cond)
assert() equivalent, that is always enabled.
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
#define FILTER_INPUTS(array)
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 link
static double min_gain(AVFilterContext *ctx, ChannelContext *cc, int max_size)
Describe the class of an AVClass context structure.
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
static void ff_bufqueue_discard_all(struct FFBufQueue *queue)
Unref and remove all buffers from the queue.
const AVFilterPad ff_audio_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_AUDIO.
int ff_inlink_acknowledge_status(AVFilterLink *link, int *rstatus, int64_t *rpts)
Test and acknowledge the change of status on the link.
size_t ff_inlink_queued_frames(AVFilterLink *link)
Get the number of frames available on the link.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
An AVChannelLayout holds information about the channel layout of audio data.
static AVRational av_make_q(int num, int den)
Create an AVRational.
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
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.
FF_FILTER_FORWARD_WANTED(outlink, inlink)
static void ff_bufqueue_add(void *log, struct FFBufQueue *queue, AVFrame *buf)
Add a buffer to the queue.
#define FILTER_CHANNELS(name, ptype)
int sample_rate
samples per second
int av_channel_layout_from_string(AVChannelLayout *channel_layout, const char *str)
Initialize a channel layout from a given string description.
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.
Structure holding the queue.
static void invert(float *h, int n)
uint8_t ** extended_data
pointers to the data planes/channels.
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
const char * name
Pad name.
int ff_inlink_queued_samples(AVFilterLink *link)
#define ANALYZE_CHANNEL(name, ptype, zero, min_peak)
void * av_calloc(size_t nmemb, size_t size)
static int activate(AVFilterContext *ctx)
static av_cold void uninit(AVFilterContext *ctx)
static int config_input(AVFilterLink *inlink)
static void next_pi(AVFilterContext *ctx, ChannelContext *cc, int bypass)
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
@ AV_SAMPLE_FMT_DBLP
double, planar
AVRational time_base
Define the time base used by the PTS of the frames/samples which will pass through this link.
int av_channel_layout_copy(AVChannelLayout *dst, const AVChannelLayout *src)
Make a copy of a channel layout.
static int filter_frame(AVFilterContext *ctx)
#define FILTER_LINK_CHANNELS(name, ptype, tlerp)
static float flerp(float min, float max, float mix)
#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...
#define flags(name, subs,...)
static const AVOption speechnorm_options[]
void(* analyze_channel)(AVFilterContext *ctx, ChannelContext *cc, const uint8_t *srcp, int nb_samples)
const AVFilter ff_af_speechnorm
#define FILTER_SAMPLEFMTS(...)
void ff_filter_set_ready(AVFilterContext *filter, unsigned priority)
Mark a filter ready and schedule it for activation.