Go to the documentation of this file.
33 #define FF_BUFQUEUE_SIZE (1024)
41 #define MAX_ITEMS 882000
42 #define MIN_PEAK (1. / 32768.)
84 const uint8_t *srcp,
int nb_samples);
89 #define OFFSET(x) offsetof(SpeechNormalizerContext, x)
90 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
150 if (pi[start].
type == 0)
154 while (start != end) {
158 if (pi[start].
type == 0)
161 sum += pi[start].
size;
171 int min_pi_nb_samples;
173 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);
174 for (
int ch = 1; ch <
inlink->channels && min_pi_nb_samples > 0; ch++) {
180 return min_pi_nb_samples;
185 if (cc->
pi_size >= nb_samples) {
195 const double expansion =
FFMIN(
s->max_expansion,
s->peak_value / pi_max_peak);
196 const double compression = 1. /
s->max_compression;
197 const int type =
s->invert ? pi_max_peak <=
s->threshold_value : pi_max_peak >=
s->threshold_value;
237 while (
size <= max_size) {
251 #define ANALYZE_CHANNEL(name, ptype, zero) \
252 static void analyze_channel_## name (AVFilterContext *ctx, ChannelContext *cc, \
253 const uint8_t *srcp, int nb_samples) \
255 SpeechNormalizerContext *s = ctx->priv; \
256 const ptype *src = (const ptype *)srcp; \
260 cc->state = src[0] >= zero; \
262 while (n < nb_samples) { \
263 if ((cc->state != (src[n] >= zero)) || \
264 (cc->pi[cc->pi_end].size > s->max_period)) { \
265 double max_peak = cc->pi[cc->pi_end].max_peak; \
266 int state = cc->state; \
267 cc->state = src[n] >= zero; \
268 av_assert0(cc->pi[cc->pi_end].size > 0); \
269 if (cc->pi[cc->pi_end].max_peak >= MIN_PEAK || \
270 cc->pi[cc->pi_end].size > s->max_period) { \
271 cc->pi[cc->pi_end].type = 1; \
273 if (cc->pi_end >= MAX_ITEMS) \
275 if (cc->state != state) \
276 cc->pi[cc->pi_end].max_peak = DBL_MIN; \
278 cc->pi[cc->pi_end].max_peak = max_peak; \
279 cc->pi[cc->pi_end].type = 0; \
280 cc->pi[cc->pi_end].size = 0; \
281 av_assert0(cc->pi_end != cc->pi_start); \
286 while (src[n] >= zero) { \
287 cc->pi[cc->pi_end].max_peak = FFMAX(cc->pi[cc->pi_end].max_peak, src[n]); \
288 cc->pi[cc->pi_end].size++; \
290 if (n >= nb_samples) \
294 while (src[n] < zero) { \
295 cc->pi[cc->pi_end].max_peak = FFMAX(cc->pi[cc->pi_end].max_peak, -src[n]); \
296 cc->pi[cc->pi_end].size++; \
298 if (n >= nb_samples) \
308 #define FILTER_CHANNELS(name, ptype) \
309 static void filter_channels_## name (AVFilterContext *ctx, \
310 AVFrame *in, int nb_samples) \
312 SpeechNormalizerContext *s = ctx->priv; \
313 AVFilterLink *inlink = ctx->inputs[0]; \
315 for (int ch = 0; ch < inlink->channels; ch++) { \
316 ChannelContext *cc = &s->cc[ch]; \
317 ptype *dst = (ptype *)in->extended_data[ch]; \
318 const int bypass = !(av_channel_layout_extract_channel(inlink->channel_layout, ch) & s->channels); \
321 while (n < nb_samples) { \
325 next_pi(ctx, cc, bypass); \
326 size = FFMIN(nb_samples - n, cc->pi_size); \
327 av_assert0(size > 0); \
328 gain = cc->gain_state; \
329 consume_pi(cc, size); \
330 for (int i = n; i < n + size; i++) \
345 #define FILTER_LINK_CHANNELS(name, ptype) \
346 static void filter_link_channels_## name (AVFilterContext *ctx, \
347 AVFrame *in, int nb_samples) \
349 SpeechNormalizerContext *s = ctx->priv; \
350 AVFilterLink *inlink = ctx->inputs[0]; \
353 while (n < nb_samples) { \
354 int min_size = nb_samples - n; \
356 ptype gain = s->max_expansion; \
358 for (int ch = 0; ch < inlink->channels; ch++) { \
359 ChannelContext *cc = &s->cc[ch]; \
361 cc->bypass = !(av_channel_layout_extract_channel(inlink->channel_layout, ch) & s->channels); \
363 next_pi(ctx, cc, cc->bypass); \
364 min_size = FFMIN(min_size, cc->pi_size); \
365 max_size = FFMAX(max_size, cc->pi_size); \
368 av_assert0(min_size > 0); \
369 for (int ch = 0; ch < inlink->channels; ch++) { \
370 ChannelContext *cc = &s->cc[ch]; \
374 gain = FFMIN(gain, min_gain(ctx, cc, max_size)); \
377 for (int ch = 0; ch < inlink->channels; ch++) { \
378 ChannelContext *cc = &s->cc[ch]; \
379 ptype *dst = (ptype *)in->extended_data[ch]; \
381 consume_pi(cc, min_size); \
385 for (int i = n; i < n + min_size; i++) { \
386 ptype g = lerp(s->prev_gain, gain, (i - n) / (double)min_size); \
391 s->prev_gain = gain; \
406 while (
s->queue.available > 0) {
407 int min_pi_nb_samples;
415 if (min_pi_nb_samples < in->nb_samples && !
s->eof)
422 s->filter_channels[
s->link](
ctx,
in,
in->nb_samples);
424 s->pts =
in->pts +
in->nb_samples;
440 for (
int ch = 0; ch <
inlink->channels; ch++) {
443 s->analyze_channel(
ctx, cc,
in->extended_data[ch],
in->nb_samples);
470 s->queue.available == 0) {
475 if (
s->queue.available > 0) {
479 if (nb_samples >=
in->nb_samples ||
s->eof) {
495 s->max_period =
inlink->sample_rate / 10;
502 for (
int ch = 0; ch <
inlink->channels; ch++) {
511 s->analyze_channel = analyze_channel_flt;
512 s->filter_channels[0] = filter_channels_flt;
513 s->filter_channels[1] = filter_link_channels_flt;
516 s->analyze_channel = analyze_channel_dbl;
517 s->filter_channels[0] = filter_channels_dbl;
518 s->filter_channels[1] = filter_link_channels_dbl;
528 char *res,
int res_len,
int flags)
569 .
name =
"speechnorm",
573 .priv_class = &speechnorm_class,
static const AVFilterPad inputs[]
@ AV_SAMPLE_FMT_FLTP
float, planar
A list of supported channel layouts.
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.
static enum AVSampleFormat sample_fmts[]
enum MovChannelLayoutTag * layouts
#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
This structure describes decoded (raw) audio or video data.
int av_frame_make_writable(AVFrame *frame)
Ensure that the frame data is writable, avoiding data copy if possible.
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.
AVFilter ff_af_speechnorm
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.
static const AVFilterPad outputs[]
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
#define FILTER_LINK_CHANNELS(name, ptype)
static double min_gain(AVFilterContext *ctx, ChannelContext *cc, int max_size)
Describe the class of an AVClass context structure.
#define ANALYZE_CHANNEL(name, ptype, zero)
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.
@ 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)
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method !=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2) { ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc) { av_free(ac);return NULL;} return ac;} in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar) { ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar ? ac->channels :1;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
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)
AVSampleFormat
Audio sample formats.
void(* filter_channels[2])(AVFilterContext *ctx, AVFrame *in, int nb_samples)
const char * name
Pad name.
int ff_inlink_queued_samples(AVFilterLink *link)
static int activate(AVFilterContext *ctx)
static av_cold void uninit(AVFilterContext *ctx)
static int config_input(AVFilterLink *inlink)
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
static void next_pi(AVFilterContext *ctx, ChannelContext *cc, int bypass)
@ AV_SAMPLE_FMT_DBLP
double, planar
static int filter_frame(AVFilterContext *ctx)
static int query_formats(AVFilterContext *ctx)
static double lerp(double min, double max, double mix)
#define flags(name, subs,...)
static const AVOption speechnorm_options[]
void(* analyze_channel)(AVFilterContext *ctx, ChannelContext *cc, const uint8_t *srcp, int nb_samples)
void ff_filter_set_ready(AVFilterContext *filter, unsigned priority)
Mark a filter ready and schedule it for activation.