af_volumedetect.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012 Nicolas George
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License
8  * as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with FFmpeg; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
22 #include "libavutil/avassert.h"
23 #include "audio.h"
24 #include "avfilter.h"
25 #include "internal.h"
26 
27 typedef struct {
28  /**
29  * Number of samples at each PCM value.
30  * histogram[0x8000 + i] is the number of samples at value i.
31  * The extra element is there for symmetry.
32  */
33  uint64_t histogram[0x10001];
35 
37 {
38  static const enum AVSampleFormat sample_fmts[] = {
42  };
44 
45  if (!(formats = ff_make_format_list(sample_fmts)))
46  return AVERROR(ENOMEM);
47  ff_set_common_formats(ctx, formats);
48 
49  return 0;
50 }
51 
53 {
54  AVFilterContext *ctx = inlink->dst;
55  VolDetectContext *vd = ctx->priv;
56  int64_t layout = samples->channel_layout;
57  int nb_samples = samples->nb_samples;
59  int nb_planes = nb_channels;
60  int plane, i;
61  int16_t *pcm;
62 
63  if (!av_sample_fmt_is_planar(samples->format)) {
64  nb_samples *= nb_channels;
65  nb_planes = 1;
66  }
67  for (plane = 0; plane < nb_planes; plane++) {
68  pcm = (int16_t *)samples->extended_data[plane];
69  for (i = 0; i < nb_samples; i++)
70  vd->histogram[pcm[i] + 0x8000]++;
71  }
72 
73  return ff_filter_frame(inlink->dst->outputs[0], samples);
74 }
75 
76 #define MAX_DB 91
77 
78 static inline double logdb(uint64_t v)
79 {
80  double d = v / (double)(0x8000 * 0x8000);
81  if (!v)
82  return MAX_DB;
83  return log(d) * -4.3429448190325182765112891891660508229; /* -10/log(10) */
84 }
85 
86 static void print_stats(AVFilterContext *ctx)
87 {
88  VolDetectContext *vd = ctx->priv;
89  int i, max_volume, shift;
90  uint64_t nb_samples = 0, power = 0, nb_samples_shift = 0, sum = 0;
91  uint64_t histdb[MAX_DB + 1] = { 0 };
92 
93  for (i = 0; i < 0x10000; i++)
94  nb_samples += vd->histogram[i];
95  av_log(ctx, AV_LOG_INFO, "n_samples: %"PRId64"\n", nb_samples);
96  if (!nb_samples)
97  return;
98 
99  /* If nb_samples > 1<<34, there is a risk of overflow in the
100  multiplication or the sum: shift all histogram values to avoid that.
101  The total number of samples must be recomputed to avoid rounding
102  errors. */
103  shift = av_log2(nb_samples >> 33);
104  for (i = 0; i < 0x10000; i++) {
105  nb_samples_shift += vd->histogram[i] >> shift;
106  power += (i - 0x8000) * (i - 0x8000) * (vd->histogram[i] >> shift);
107  }
108  if (!nb_samples_shift)
109  return;
110  power = (power + nb_samples_shift / 2) / nb_samples_shift;
111  av_assert0(power <= 0x8000 * 0x8000);
112  av_log(ctx, AV_LOG_INFO, "mean_volume: %.1f dB\n", -logdb(power));
113 
114  max_volume = 0x8000;
115  while (max_volume > 0 && !vd->histogram[0x8000 + max_volume] &&
116  !vd->histogram[0x8000 - max_volume])
117  max_volume--;
118  av_log(ctx, AV_LOG_INFO, "max_volume: %.1f dB\n", -logdb(max_volume * max_volume));
119 
120  for (i = 0; i < 0x10000; i++)
121  histdb[(int)logdb((i - 0x8000) * (i - 0x8000))] += vd->histogram[i];
122  for (i = 0; i <= MAX_DB && !histdb[i]; i++);
123  for (; i <= MAX_DB && sum < nb_samples / 1000; i++) {
124  av_log(ctx, AV_LOG_INFO, "histogram_%ddb: %"PRId64"\n", i, histdb[i]);
125  sum += histdb[i];
126  }
127 }
128 
129 static void uninit(AVFilterContext *ctx)
130 {
131  print_stats(ctx);
132 }
133 
135  {
136  .name = "default",
137  .type = AVMEDIA_TYPE_AUDIO,
138  .get_audio_buffer = ff_null_get_audio_buffer,
139  .filter_frame = filter_frame,
140  },
141  { NULL }
142 };
143 
145  {
146  .name = "default",
147  .type = AVMEDIA_TYPE_AUDIO,
148  },
149  { NULL }
150 };
151 
153  .name = "volumedetect",
154  .description = NULL_IF_CONFIG_SMALL("Detect audio volume."),
155 
156  .priv_size = sizeof(VolDetectContext),
158  .uninit = uninit,
159  .inputs = volumedetect_inputs,
160  .outputs = volumedetect_outputs,
161 };
float v
static int shift(int a, int b)
Definition: sonic.c:86
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:117
external API header
signed 16 bits
Definition: samplefmt.h:52
static double logdb(uint64_t v)
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:308
set threshold d
static int query_formats(AVFilterContext *ctx)
const char * name
Pad name.
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
it can be given away to ff_start_frame *A reference passed to ff_filter_frame(or the deprecated ff_start_frame) is given away and must no longer be used.*A reference created with avfilter_ref_buffer belongs to the code that created it.*A reference obtained with ff_get_video_buffer or ff_get_audio_buffer belongs to the code that requested it.*A reference given as return value by the get_video_buffer or get_audio_buffer method is given away and must no longer be used.Link reference fields---------------------The AVFilterLink structure has a few AVFilterBufferRef fields.The cur_buf and out_buf were used with the deprecated start_frame/draw_slice/end_frame API and should no longer be used.src_buf
uint64_t histogram[0x10001]
Number of samples at each PCM value.
void ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:545
static int filter_frame(AVFilterLink *inlink, AVFrame *samples)
A filter pad used for either input or output.
static void uninit(AVFilterContext *ctx)
static const AVFilterPad volumedetect_outputs[]
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
void * priv
private data for use by the filter
Definition: avfilter.h:545
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
simple assert() macros that are a bit more flexible than ISO C assert().
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
AVFilter avfilter_af_volumedetect
uint64_t channel_layout
Channel layout of the audio data.
Definition: frame.h:331
static void print_stats(AVFilterContext *ctx)
#define MAX_DB
audio channel layout utility functions
AVFrame * ff_null_get_audio_buffer(AVFilterLink *link, int nb_samples)
get_audio_buffer() handler for filters which simply pass audio along
Definition: audio.c:36
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:134
NULL
Definition: eval.c:55
Filter definition.
Definition: avfilter.h:436
int av_sample_fmt_is_planar(enum AVSampleFormat sample_fmt)
Check if the sample format is planar.
Definition: samplefmt.c:118
synthesis window for stochastic i
static const AVFilterPad volumedetect_inputs[]
const char * name
filter name
Definition: avfilter.h:437
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer 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.Buffer references ownership and permissions
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:539
The official guide to swscale for confused that consecutive non overlapping rectangles of slice_bottom special converter These generally are unscaled converters of common formats
Definition: swscale.txt:33
AVSampleFormat
Audio Sample Formats.
Definition: samplefmt.h:49
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel layout
#define av_log2
Definition: intmath.h:89
A list of supported formats for one end of a filter link.
Definition: formats.h:64
An instance of a filter.
Definition: avfilter.h:524
signed 16 bits, planar
Definition: samplefmt.h:58
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:700
#define AV_LOG_INFO
Definition: log.h:156
Filter the word “frame” indicates either a video frame or a group of audio samples
int nb_channels
internal API functions
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:117
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several inputs
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:127
for(j=16;j >0;--j)