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) {
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 enum AVSampleFormat sample_fmts[]
#define ANALYZE_CHANNEL(name, ptype, zero)
static int available_samples(AVFilterContext *ctx)
#define FILTER_LINK_CHANNELS(name, ptype)
static void next_pi(AVFilterContext *ctx, ChannelContext *cc, int bypass)
static int query_formats(AVFilterContext *ctx)
static void consume_pi(ChannelContext *cc, int nb_samples)
static const AVOption speechnorm_options[]
static int config_input(AVFilterLink *inlink)
static const AVFilterPad inputs[]
AVFilter ff_af_speechnorm
static const AVFilterPad outputs[]
static double min_gain(AVFilterContext *ctx, ChannelContext *cc, int max_size)
static double next_gain(AVFilterContext *ctx, double pi_max_peak, int bypass, double state)
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
static int activate(AVFilterContext *ctx)
static av_cold void uninit(AVFilterContext *ctx)
static int filter_frame(AVFilterContext *ctx)
static double lerp(double min, double max, double mix)
#define FILTER_CHANNELS(name, ptype)
AVFILTER_DEFINE_CLASS(speechnorm)
static int get_pi_samples(PeriodItem *pi, int start, int end, int remain)
static void invert(float *h, int n)
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
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
int ff_inlink_acknowledge_status(AVFilterLink *link, int *rstatus, int64_t *rpts)
Test and acknowledge the change of status on the link.
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
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.
size_t ff_inlink_queued_frames(AVFilterLink *link)
Get the number of frames available on the link.
void ff_filter_set_ready(AVFilterContext *filter, unsigned priority)
Mark a filter ready and schedule it for activation.
int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe)
Take a frame from the link's FIFO and update the link's stats.
int ff_inlink_queued_samples(AVFilterLink *link)
Main libavfilter public API header.
static void ff_bufqueue_add(void *log, struct FFBufQueue *queue, AVFrame *buf)
Add a buffer to the queue.
static AVFrame * ff_bufqueue_peek(struct FFBufQueue *queue, unsigned index)
Get a buffer from the queue without altering it.
static void ff_bufqueue_discard_all(struct FFBufQueue *queue)
Unref and remove all buffers from the queue.
static AVFrame * ff_bufqueue_get(struct FFBufQueue *queue)
Get the first buffer from the queue and remove it.
#define flags(name, subs,...)
#define FF_FILTER_FORWARD_WANTED(outlink, inlink)
Forward the frame_wanted_out flag from an output link to an input link.
static void ff_outlink_set_status(AVFilterLink *link, int status, int64_t pts)
Set the status field of a link from the source filter.
#define FFERROR_NOT_READY
Filters implementation helper functions.
#define FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink)
Forward the status on an output link to an input link.
@ AV_OPT_TYPE_CHANNEL_LAYOUT
#define AVERROR_EOF
End of file.
int av_frame_make_writable(AVFrame *frame)
Ensure that the frame data is writable, avoiding data copy if possible.
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
AVSampleFormat
Audio sample formats.
@ AV_SAMPLE_FMT_FLTP
float, planar
@ AV_SAMPLE_FMT_DBLP
double, planar
static int mix(int c0, int c1)
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
enum MovChannelLayoutTag * layouts
typedef void(RENAME(mix_any_func_type))
Describe the class of an AVClass context structure.
A list of supported channel layouts.
A link between two filters.
int channels
Number of channels.
int sample_rate
samples per second
AVFilterContext * dst
dest filter
int format
agreed upon media format
A filter pad used for either input or output.
const char * name
Pad name.
const char * name
Filter name.
This structure describes decoded (raw) audio or video data.
Structure holding the queue.
void(* filter_channels[2])(AVFilterContext *ctx, AVFrame *in, int nb_samples)
void(* analyze_channel)(AVFilterContext *ctx, ChannelContext *cc, const uint8_t *srcp, int nb_samples)