FFmpeg
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 VolDetectContext {
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  };
45  int ret;
46 
48  return AVERROR(ENOMEM);
49 
51  if (!layouts)
52  return AVERROR(ENOMEM);
54  if (ret < 0)
55  return ret;
56 
58 }
59 
61 {
62  AVFilterContext *ctx = inlink->dst;
63  VolDetectContext *vd = ctx->priv;
64  int nb_samples = samples->nb_samples;
65  int nb_channels = samples->channels;
66  int nb_planes = nb_channels;
67  int plane, i;
68  int16_t *pcm;
69 
70  if (!av_sample_fmt_is_planar(samples->format)) {
71  nb_samples *= nb_channels;
72  nb_planes = 1;
73  }
74  for (plane = 0; plane < nb_planes; plane++) {
75  pcm = (int16_t *)samples->extended_data[plane];
76  for (i = 0; i < nb_samples; i++)
77  vd->histogram[pcm[i] + 0x8000]++;
78  }
79 
80  return ff_filter_frame(inlink->dst->outputs[0], samples);
81 }
82 
83 #define MAX_DB 91
84 
85 static inline double logdb(uint64_t v)
86 {
87  double d = v / (double)(0x8000 * 0x8000);
88  if (!v)
89  return MAX_DB;
90  return -log10(d) * 10;
91 }
92 
94 {
95  VolDetectContext *vd = ctx->priv;
96  int i, max_volume, shift;
97  uint64_t nb_samples = 0, power = 0, nb_samples_shift = 0, sum = 0;
98  uint64_t histdb[MAX_DB + 1] = { 0 };
99 
100  for (i = 0; i < 0x10000; i++)
101  nb_samples += vd->histogram[i];
102  av_log(ctx, AV_LOG_INFO, "n_samples: %"PRId64"\n", nb_samples);
103  if (!nb_samples)
104  return;
105 
106  /* If nb_samples > 1<<34, there is a risk of overflow in the
107  multiplication or the sum: shift all histogram values to avoid that.
108  The total number of samples must be recomputed to avoid rounding
109  errors. */
110  shift = av_log2(nb_samples >> 33);
111  for (i = 0; i < 0x10000; i++) {
112  nb_samples_shift += vd->histogram[i] >> shift;
113  power += (i - 0x8000) * (i - 0x8000) * (vd->histogram[i] >> shift);
114  }
115  if (!nb_samples_shift)
116  return;
117  power = (power + nb_samples_shift / 2) / nb_samples_shift;
118  av_assert0(power <= 0x8000 * 0x8000);
119  av_log(ctx, AV_LOG_INFO, "mean_volume: %.1f dB\n", -logdb(power));
120 
121  max_volume = 0x8000;
122  while (max_volume > 0 && !vd->histogram[0x8000 + max_volume] &&
123  !vd->histogram[0x8000 - max_volume])
124  max_volume--;
125  av_log(ctx, AV_LOG_INFO, "max_volume: %.1f dB\n", -logdb(max_volume * max_volume));
126 
127  for (i = 0; i < 0x10000; i++)
128  histdb[(int)logdb((i - 0x8000) * (i - 0x8000))] += vd->histogram[i];
129  for (i = 0; i <= MAX_DB && !histdb[i]; i++);
130  for (; i <= MAX_DB && sum < nb_samples / 1000; i++) {
131  av_log(ctx, AV_LOG_INFO, "histogram_%ddb: %"PRId64"\n", i, histdb[i]);
132  sum += histdb[i];
133  }
134 }
135 
137 {
138  print_stats(ctx);
139 }
140 
142  {
143  .name = "default",
144  .type = AVMEDIA_TYPE_AUDIO,
145  .filter_frame = filter_frame,
146  },
147  { NULL }
148 };
149 
151  {
152  .name = "default",
153  .type = AVMEDIA_TYPE_AUDIO,
154  },
155  { NULL }
156 };
157 
159  .name = "volumedetect",
160  .description = NULL_IF_CONFIG_SMALL("Detect audio volume."),
161  .priv_size = sizeof(VolDetectContext),
163  .uninit = uninit,
166 };
formats
formats
Definition: signature.h:48
AVFilterChannelLayouts
A list of supported channel layouts.
Definition: formats.h:85
AVERROR
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
volumedetect_outputs
static const AVFilterPad volumedetect_outputs[]
Definition: af_volumedetect.c:150
ff_make_format_list
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:283
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_volumedetect.c:136
ff_set_common_channel_layouts
int ff_set_common_channel_layouts(AVFilterContext *ctx, AVFilterChannelLayouts *layouts)
A helper for query_formats() which sets all links to the same list of channel layouts/sample rates.
Definition: formats.c:549
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1080
sample_fmts
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:686
layouts
enum MovChannelLayoutTag * layouts
Definition: mov_chan.c:434
ff_af_volumedetect
AVFilter ff_af_volumedetect
Definition: af_volumedetect.c:158
inlink
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
Definition: filter_design.txt:212
ff_all_channel_counts
AVFilterChannelLayouts * ff_all_channel_counts(void)
Construct an AVFilterChannelLayouts coding for any channel layout, with known or unknown disposition.
Definition: formats.c:410
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:295
MAX_DB
#define MAX_DB
Definition: af_volumedetect.c:83
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:148
AVFilterFormats
A list of supported formats for one end of a filter link.
Definition: formats.h:64
plane
int plane
Definition: avisynth_c.h:384
VolDetectContext::histogram
uint64_t histogram[0x10001]
Number of samples at each PCM value.
Definition: af_volumedetect.c:33
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:54
avassert.h
av_cold
#define av_cold
Definition: attributes.h:84
ff_set_common_formats
int 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:568
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
av_sample_fmt_is_planar
int av_sample_fmt_is_planar(enum AVSampleFormat sample_fmt)
Check if the sample format is planar.
Definition: samplefmt.c:112
outputs
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
ctx
AVFormatContext * ctx
Definition: movenc.c:48
NULL
#define NULL
Definition: coverity.c:32
inputs
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
Definition: filter_design.txt:243
for
for(j=16;j >0;--j)
Definition: h264pred_template.c:469
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:188
query_formats
static int query_formats(AVFilterContext *ctx)
Definition: af_volumedetect.c:36
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:59
AV_SAMPLE_FMT_S16P
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
Definition: samplefmt.h:67
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
internal.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *samples)
Definition: af_volumedetect.c:60
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:61
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:60
AVFilter
Filter definition.
Definition: avfilter.h:144
ret
ret
Definition: filter_design.txt:187
channel_layout.h
avfilter.h
samples
Filter the word “frame” indicates either a video frame or a group of audio samples
Definition: filter_design.txt:8
logdb
static double logdb(uint64_t v)
Definition: af_volumedetect.c:85
AVFilterContext
An instance of a filter.
Definition: avfilter.h:338
shift
static int shift(int a, int b)
Definition: sonic.c:82
VolDetectContext
Definition: af_volumedetect.c:27
audio.h
print_stats
static void print_stats(AVFilterContext *ctx)
Definition: af_volumedetect.c:93
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
av_log2
int av_log2(unsigned v)
Definition: intmath.c:26
volumedetect_inputs
static const AVFilterPad volumedetect_inputs[]
Definition: af_volumedetect.c:141
nb_channels
int nb_channels
Definition: channel_layout.c:76