Go to the documentation of this file.
34 #define FF_BUFQUEUE_SIZE (1024)
42 #define MAX_ITEMS 882000
43 #define MIN_PEAK (1. / 32768.)
86 const uint8_t *srcp,
int nb_samples);
91 #define OFFSET(x) offsetof(SpeechNormalizerContext, x)
92 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
122 if (pi[start].
type == 0)
126 while (start != end) {
130 if (pi[start].
type == 0)
133 sum += pi[start].
size;
143 int min_pi_nb_samples;
145 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);
146 for (
int ch = 1; ch <
inlink->ch_layout.nb_channels && min_pi_nb_samples > 0; ch++) {
152 return min_pi_nb_samples;
157 if (cc->
pi_size >= nb_samples) {
167 const double expansion =
FFMIN(
s->max_expansion,
s->peak_value / pi_max_peak);
168 const double compression = 1. /
s->max_compression;
169 const int type =
s->invert ? pi_max_peak <=
s->threshold_value : pi_max_peak >=
s->threshold_value;
209 while (
size <= max_size) {
223 #define ANALYZE_CHANNEL(name, ptype, zero, min_peak) \
224 static void analyze_channel_## name (AVFilterContext *ctx, ChannelContext *cc, \
225 const uint8_t *srcp, int nb_samples) \
227 SpeechNormalizerContext *s = ctx->priv; \
228 const ptype *src = (const ptype *)srcp; \
229 const int max_period = s->max_period; \
230 PeriodItem *pi = (PeriodItem *)&cc->pi; \
231 int pi_end = cc->pi_end; \
235 cc->state = src[0] >= zero; \
237 while (n < nb_samples) { \
238 ptype new_max_peak; \
241 if ((cc->state != (src[n] >= zero)) || \
242 (pi[pi_end].size > max_period)) { \
243 ptype max_peak = pi[pi_end].max_peak; \
244 int state = cc->state; \
246 cc->state = src[n] >= zero; \
247 av_assert1(pi[pi_end].size > 0); \
248 if (max_peak >= min_peak || \
249 pi[pi_end].size > max_period) { \
250 pi[pi_end].type = 1; \
252 if (pi_end >= MAX_ITEMS) \
254 if (cc->state != state) \
255 pi[pi_end].max_peak = DBL_MIN; \
257 pi[pi_end].max_peak = max_peak; \
258 pi[pi_end].type = 0; \
259 pi[pi_end].size = 0; \
260 av_assert1(pi_end != cc->pi_start); \
264 new_max_peak = pi[pi_end].max_peak; \
265 new_size = pi[pi_end].size; \
267 while (src[n] >= zero) { \
268 new_max_peak = FFMAX(new_max_peak, src[n]); \
271 if (n >= nb_samples) \
275 while (src[n] < zero) { \
276 new_max_peak = FFMAX(new_max_peak, -src[n]); \
279 if (n >= nb_samples) \
284 pi[pi_end].max_peak = new_max_peak; \
285 pi[pi_end].size = new_size; \
287 cc->pi_end = pi_end; \
293 #define FILTER_CHANNELS(name, ptype) \
294 static void filter_channels_## name (AVFilterContext *ctx, \
295 AVFrame *in, AVFrame *out, int nb_samples) \
297 SpeechNormalizerContext *s = ctx->priv; \
298 AVFilterLink *inlink = ctx->inputs[0]; \
300 for (int ch = 0; ch < inlink->ch_layout.nb_channels; ch++) { \
301 ChannelContext *cc = &s->cc[ch]; \
302 const ptype *src = (const ptype *)in->extended_data[ch]; \
303 ptype *dst = (ptype *)out->extended_data[ch]; \
304 enum AVChannel channel = av_channel_layout_channel_from_index(&inlink->ch_layout, ch); \
305 const int bypass = av_channel_layout_index_from_channel(&s->ch_layout, channel) < 0; \
308 while (n < nb_samples) { \
312 next_pi(ctx, cc, bypass); \
313 size = FFMIN(nb_samples - n, cc->pi_size); \
314 av_assert1(size > 0); \
315 gain = cc->gain_state; \
316 consume_pi(cc, size); \
317 for (int i = n; !ctx->is_disabled && i < n + size; i++) \
318 dst[i] = src[i] * gain; \
337 #define FILTER_LINK_CHANNELS(name, ptype, tlerp) \
338 static void filter_link_channels_## name (AVFilterContext *ctx, \
339 AVFrame *in, AVFrame *out, \
342 SpeechNormalizerContext *s = ctx->priv; \
343 AVFilterLink *inlink = ctx->inputs[0]; \
346 while (n < nb_samples) { \
347 int min_size = nb_samples - n; \
349 ptype gain = s->max_expansion; \
351 for (int ch = 0; ch < inlink->ch_layout.nb_channels; ch++) { \
352 ChannelContext *cc = &s->cc[ch]; \
354 enum AVChannel channel = av_channel_layout_channel_from_index(&inlink->ch_layout, ch); \
355 cc->bypass = av_channel_layout_index_from_channel(&s->ch_layout, channel) < 0; \
357 next_pi(ctx, cc, cc->bypass); \
358 min_size = FFMIN(min_size, cc->pi_size); \
359 max_size = FFMAX(max_size, cc->pi_size); \
362 av_assert1(min_size > 0); \
363 for (int ch = 0; ch < inlink->ch_layout.nb_channels; ch++) { \
364 ChannelContext *cc = &s->cc[ch]; \
368 gain = FFMIN(gain, min_gain(ctx, cc, max_size)); \
371 for (int ch = 0; ch < inlink->ch_layout.nb_channels; ch++) { \
372 ChannelContext *cc = &s->cc[ch]; \
373 const ptype *src = (const ptype *)in->extended_data[ch]; \
374 ptype *dst = (ptype *)out->extended_data[ch]; \
376 consume_pi(cc, min_size); \
380 for (int i = n; !ctx->is_disabled && i < n + min_size; i++) { \
381 ptype g = tlerp(s->prev_gain, gain, (i - n) / (ptype)min_size); \
382 dst[i] = src[i] * g; \
386 s->prev_gain = gain; \
401 while (
s->queue.available > 0) {
402 int min_pi_nb_samples;
410 if (min_pi_nb_samples < in->nb_samples && !
s->eof)
426 s->filter_channels[
s->link](
ctx, in,
out, in->nb_samples);
447 for (
int ch = 0; ch <
inlink->ch_layout.nb_channels; ch++) {
468 if (strcmp(
s->ch_layout_str,
"all"))
484 s->queue.available == 0) {
489 if (
s->queue.available > 0) {
509 s->max_period =
inlink->sample_rate / 10;
516 for (
int ch = 0; ch <
inlink->ch_layout.nb_channels; ch++) {
525 s->analyze_channel = analyze_channel_flt;
526 s->filter_channels[0] = filter_channels_flt;
527 s->filter_channels[1] = filter_link_channels_flt;
530 s->analyze_channel = analyze_channel_dbl;
531 s->filter_channels[0] = filter_channels_dbl;
532 s->filter_channels[1] = filter_link_channels_dbl;
542 char *res,
int res_len,
int flags)
582 .
name =
"speechnorm",
585 .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)
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
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.
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 av_channel_layout_copy(AVChannelLayout *dst, const AVChannelLayout *src)
Make a copy of a channel layout.
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.
static const AVFilterPad outputs[]
#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.
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.
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
#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)
static double next_gain(AVFilterContext *ctx, double pi_max_peak, int bypass, double state)
int sample_rate
samples per second
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)
int av_channel_layout_from_string(AVChannelLayout *channel_layout, const char *str)
Initialize a channel layout from a given string description.
static void next_pi(AVFilterContext *ctx, ChannelContext *cc, int bypass)
@ 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.
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.