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;
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) {
236 min_gain =
FFMIN(min_gain, gain_state);
237 while (size <= max_size) {
241 min_gain =
FFMIN(min_gain, gain_state);
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++) \ 342 return min + (max -
min) * mix;
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; \ 407 int min_pi_nb_samples;
415 if (min_pi_nb_samples < in->nb_samples && !s->
eof)
424 s->
pts = in->pts + in->nb_samples;
440 for (
int ch = 0; ch < inlink->
channels; ch++) {
502 for (
int ch = 0; ch < inlink->
channels; ch++) {
528 char *res,
int res_len,
int flags)
569 .
name =
"speechnorm",
573 .priv_class = &speechnorm_class,
static AVFrame * ff_bufqueue_get(struct FFBufQueue *queue)
Get the first buffer from the queue and remove it.
static int filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe)
Take a frame from the link's FIFO and update the link's stats.
This structure describes decoded (raw) audio or video data.
Main libavfilter public API header.
static void ff_outlink_set_status(AVFilterLink *link, int status, int64_t pts)
Set the status field of a link from the source filter.
void(* analyze_channel)(AVFilterContext *ctx, ChannelContext *cc, const uint8_t *srcp, int nb_samples)
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
Structure holding the queue.
void(* filter_channels[2])(AVFilterContext *ctx, AVFrame *in, int nb_samples)
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
const char * name
Pad name.
AVFilterLink ** inputs
array of pointers to input links
#define av_assert0(cond)
assert() equivalent, that is always enabled.
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
static av_cold int end(AVCodecContext *avctx)
#define AVERROR_EOF
End of file.
#define FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink)
Forward the status on an output link to an input link.
#define ANALYZE_CHANNEL(name, ptype, zero)
A filter pad used for either input or output.
A link between two filters.
int ff_inlink_acknowledge_status(AVFilterLink *link, int *rstatus, int64_t *rpts)
Test and acknowledge the change of status on the link.
int sample_rate
samples per second
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
AVFILTER_DEFINE_CLASS(speechnorm)
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...
void * priv
private data for use by the filter
static int available_samples(AVFilterContext *ctx)
simple assert() macros that are a bit more flexible than ISO C assert().
static double lerp(double min, double max, double mix)
AVFrame * queue[FF_BUFQUEUE_SIZE]
#define FILTER_LINK_CHANNELS(name, ptype)
#define FILTER_CHANNELS(name, ptype)
typedef void(APIENTRY *FF_PFNGLACTIVETEXTUREPROC)(GLenum texture)
static const AVFilterPad inputs[]
int ff_inlink_queued_samples(AVFilterLink *link)
static const AVOption speechnorm_options[]
static int get_pi_samples(PeriodItem *pi, int start, int end, int remain)
static void invert(float *h, int n)
static void ff_bufqueue_discard_all(struct FFBufQueue *queue)
Unref and remove all buffers from the queue.
int format
agreed upon media format
A list of supported channel layouts.
static int mix(int c0, int c1)
AVSampleFormat
Audio sample formats.
unsigned short available
number of available buffers
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
Describe the class of an AVClass context structure.
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 double next_gain(AVFilterContext *ctx, double pi_max_peak, int bypass, double state)
const char * name
Filter name.
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
size_t ff_inlink_queued_frames(AVFilterLink *link)
Get the number of frames available on the link.
static int filter_frame(AVFilterContext *ctx)
AVFilterLink ** outputs
array of pointers to output links
enum MovChannelLayoutTag * layouts
static void next_pi(AVFilterContext *ctx, ChannelContext *cc, int bypass)
AVFilter ff_af_speechnorm
int av_frame_make_writable(AVFrame *frame)
Ensure that the frame data is writable, avoiding data copy if possible.
#define flags(name, subs,...)
static int query_formats(AVFilterContext *ctx)
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 ff_filter_set_ready(AVFilterContext *filter, unsigned priority)
Mark a filter ready and schedule it for activation.
int channels
Number of channels.
FF_FILTER_FORWARD_WANTED(outlink, inlink)
static int activate(AVFilterContext *ctx)
static int config_input(AVFilterLink *inlink)
AVFilterContext * dst
dest filter
static av_cold void uninit(AVFilterContext *ctx)
static enum AVSampleFormat sample_fmts[]
static double min_gain(AVFilterContext *ctx, ChannelContext *cc, int max_size)
static void ff_bufqueue_add(void *log, struct FFBufQueue *queue, AVFrame *buf)
Add a buffer to the queue.
static void consume_pi(ChannelContext *cc, int nb_samples)
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
uint8_t ** extended_data
pointers to the data planes/channels.
int nb_samples
number of audio samples (per channel) described by this frame
static const AVFilterPad outputs[]
static AVFrame * ff_bufqueue_peek(struct FFBufQueue *queue, unsigned index)
Get a buffer from the queue without altering it.