Go to the documentation of this file.
34 #define FF_BUFQUEUE_SIZE (1024)
42 #define MAX_ITEMS 882000
43 #define MIN_PEAK (1. / 32768.)
85 const uint8_t *srcp,
int nb_samples);
90 #define OFFSET(x) offsetof(SpeechNormalizerContext, x)
91 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
121 if (pi[start].
type == 0)
125 while (start != end) {
129 if (pi[start].
type == 0)
132 sum += pi[start].
size;
142 int min_pi_nb_samples;
144 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);
145 for (
int ch = 1; ch <
inlink->channels && min_pi_nb_samples > 0; ch++) {
151 return min_pi_nb_samples;
156 if (cc->
pi_size >= nb_samples) {
166 const double expansion =
FFMIN(
s->max_expansion,
s->peak_value / pi_max_peak);
167 const double compression = 1. /
s->max_compression;
168 const int type =
s->invert ? pi_max_peak <=
s->threshold_value : pi_max_peak >=
s->threshold_value;
208 while (
size <= max_size) {
222 #define ANALYZE_CHANNEL(name, ptype, zero, min_peak) \
223 static void analyze_channel_## name (AVFilterContext *ctx, ChannelContext *cc, \
224 const uint8_t *srcp, int nb_samples) \
226 SpeechNormalizerContext *s = ctx->priv; \
227 const ptype *src = (const ptype *)srcp; \
231 cc->state = src[0] >= zero; \
233 while (n < nb_samples) { \
234 if ((cc->state != (src[n] >= zero)) || \
235 (cc->pi[cc->pi_end].size > s->max_period)) { \
236 ptype max_peak = cc->pi[cc->pi_end].max_peak; \
237 int state = cc->state; \
238 cc->state = src[n] >= zero; \
239 av_assert0(cc->pi[cc->pi_end].size > 0); \
240 if (max_peak >= min_peak || \
241 cc->pi[cc->pi_end].size > s->max_period) { \
242 cc->pi[cc->pi_end].type = 1; \
244 if (cc->pi_end >= MAX_ITEMS) \
246 if (cc->state != state) \
247 cc->pi[cc->pi_end].max_peak = DBL_MIN; \
249 cc->pi[cc->pi_end].max_peak = max_peak; \
250 cc->pi[cc->pi_end].type = 0; \
251 cc->pi[cc->pi_end].size = 0; \
252 av_assert0(cc->pi_end != cc->pi_start); \
257 while (src[n] >= zero) { \
258 cc->pi[cc->pi_end].max_peak = FFMAX(cc->pi[cc->pi_end].max_peak, src[n]); \
259 cc->pi[cc->pi_end].size++; \
261 if (n >= nb_samples) \
265 while (src[n] < zero) { \
266 cc->pi[cc->pi_end].max_peak = FFMAX(cc->pi[cc->pi_end].max_peak, -src[n]); \
267 cc->pi[cc->pi_end].size++; \
269 if (n >= nb_samples) \
279 #define FILTER_CHANNELS(name, ptype) \
280 static void filter_channels_## name (AVFilterContext *ctx, \
281 AVFrame *in, AVFrame *out, int nb_samples) \
283 SpeechNormalizerContext *s = ctx->priv; \
284 AVFilterLink *inlink = ctx->inputs[0]; \
286 for (int ch = 0; ch < inlink->channels; ch++) { \
287 ChannelContext *cc = &s->cc[ch]; \
288 const ptype *src = (const ptype *)in->extended_data[ch]; \
289 ptype *dst = (ptype *)out->extended_data[ch]; \
290 const int bypass = !(av_channel_layout_extract_channel(inlink->channel_layout, ch) & s->channels); \
293 while (n < nb_samples) { \
297 next_pi(ctx, cc, bypass); \
298 size = FFMIN(nb_samples - n, cc->pi_size); \
299 av_assert0(size > 0); \
300 gain = cc->gain_state; \
301 consume_pi(cc, size); \
302 for (int i = n; !ctx->is_disabled && i < n + size; i++) \
303 dst[i] = src[i] * gain; \
322 #define FILTER_LINK_CHANNELS(name, ptype, tlerp) \
323 static void filter_link_channels_## name (AVFilterContext *ctx, \
324 AVFrame *in, AVFrame *out, \
327 SpeechNormalizerContext *s = ctx->priv; \
328 AVFilterLink *inlink = ctx->inputs[0]; \
331 while (n < nb_samples) { \
332 int min_size = nb_samples - n; \
334 ptype gain = s->max_expansion; \
336 for (int ch = 0; ch < inlink->channels; ch++) { \
337 ChannelContext *cc = &s->cc[ch]; \
339 cc->bypass = !(av_channel_layout_extract_channel(inlink->channel_layout, ch) & s->channels); \
341 next_pi(ctx, cc, cc->bypass); \
342 min_size = FFMIN(min_size, cc->pi_size); \
343 max_size = FFMAX(max_size, cc->pi_size); \
346 av_assert0(min_size > 0); \
347 for (int ch = 0; ch < inlink->channels; ch++) { \
348 ChannelContext *cc = &s->cc[ch]; \
352 gain = FFMIN(gain, min_gain(ctx, cc, max_size)); \
355 for (int ch = 0; ch < inlink->channels; ch++) { \
356 ChannelContext *cc = &s->cc[ch]; \
357 const ptype *src = (const ptype *)in->extended_data[ch]; \
358 ptype *dst = (ptype *)out->extended_data[ch]; \
360 consume_pi(cc, min_size); \
364 for (int i = n; !ctx->is_disabled && i < n + min_size; i++) { \
365 ptype g = tlerp(s->prev_gain, gain, (i - n) / (ptype)min_size); \
366 dst[i] = src[i] * g; \
370 s->prev_gain = gain; \
385 while (
s->queue.available > 0) {
386 int min_pi_nb_samples;
394 if (min_pi_nb_samples < in->nb_samples && !
s->eof)
410 s->filter_channels[
s->link](
ctx, in,
out, in->nb_samples);
431 for (
int ch = 0; ch <
inlink->channels; ch++) {
461 s->queue.available == 0) {
466 if (
s->queue.available > 0) {
486 s->max_period =
inlink->sample_rate / 10;
493 for (
int ch = 0; ch <
inlink->channels; ch++) {
502 s->analyze_channel = analyze_channel_flt;
503 s->filter_channels[0] = filter_channels_flt;
504 s->filter_channels[1] = filter_link_channels_flt;
507 s->analyze_channel = analyze_channel_dbl;
508 s->filter_channels[0] = filter_channels_dbl;
509 s->filter_channels[1] = filter_link_channels_dbl;
519 char *res,
int res_len,
int flags)
558 .
name =
"speechnorm",
561 .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.
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 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.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
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.
@ AV_OPT_TYPE_CHANNEL_LAYOUT
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.
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)
@ 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.