FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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  return ff_set_common_formats(ctx, formats);
48 }
49 
50 static int filter_frame(AVFilterLink *inlink, AVFrame *samples)
51 {
52  AVFilterContext *ctx = inlink->dst;
53  VolDetectContext *vd = ctx->priv;
54  int64_t layout = samples->channel_layout;
55  int nb_samples = samples->nb_samples;
57  int nb_planes = nb_channels;
58  int plane, i;
59  int16_t *pcm;
60 
61  if (!av_sample_fmt_is_planar(samples->format)) {
62  nb_samples *= nb_channels;
63  nb_planes = 1;
64  }
65  for (plane = 0; plane < nb_planes; plane++) {
66  pcm = (int16_t *)samples->extended_data[plane];
67  for (i = 0; i < nb_samples; i++)
68  vd->histogram[pcm[i] + 0x8000]++;
69  }
70 
71  return ff_filter_frame(inlink->dst->outputs[0], samples);
72 }
73 
74 #define MAX_DB 91
75 
76 static inline double logdb(uint64_t v)
77 {
78  double d = v / (double)(0x8000 * 0x8000);
79  if (!v)
80  return MAX_DB;
81  return log(d) * -4.3429448190325182765112891891660508229; /* -10/log(10) */
82 }
83 
84 static void print_stats(AVFilterContext *ctx)
85 {
86  VolDetectContext *vd = ctx->priv;
87  int i, max_volume, shift;
88  uint64_t nb_samples = 0, power = 0, nb_samples_shift = 0, sum = 0;
89  uint64_t histdb[MAX_DB + 1] = { 0 };
90 
91  for (i = 0; i < 0x10000; i++)
92  nb_samples += vd->histogram[i];
93  av_log(ctx, AV_LOG_INFO, "n_samples: %"PRId64"\n", nb_samples);
94  if (!nb_samples)
95  return;
96 
97  /* If nb_samples > 1<<34, there is a risk of overflow in the
98  multiplication or the sum: shift all histogram values to avoid that.
99  The total number of samples must be recomputed to avoid rounding
100  errors. */
101  shift = av_log2(nb_samples >> 33);
102  for (i = 0; i < 0x10000; i++) {
103  nb_samples_shift += vd->histogram[i] >> shift;
104  power += (i - 0x8000) * (i - 0x8000) * (vd->histogram[i] >> shift);
105  }
106  if (!nb_samples_shift)
107  return;
108  power = (power + nb_samples_shift / 2) / nb_samples_shift;
109  av_assert0(power <= 0x8000 * 0x8000);
110  av_log(ctx, AV_LOG_INFO, "mean_volume: %.1f dB\n", -logdb(power));
111 
112  max_volume = 0x8000;
113  while (max_volume > 0 && !vd->histogram[0x8000 + max_volume] &&
114  !vd->histogram[0x8000 - max_volume])
115  max_volume--;
116  av_log(ctx, AV_LOG_INFO, "max_volume: %.1f dB\n", -logdb(max_volume * max_volume));
117 
118  for (i = 0; i < 0x10000; i++)
119  histdb[(int)logdb((i - 0x8000) * (i - 0x8000))] += vd->histogram[i];
120  for (i = 0; i <= MAX_DB && !histdb[i]; i++);
121  for (; i <= MAX_DB && sum < nb_samples / 1000; i++) {
122  av_log(ctx, AV_LOG_INFO, "histogram_%ddb: %"PRId64"\n", i, histdb[i]);
123  sum += histdb[i];
124  }
125 }
126 
127 static av_cold void uninit(AVFilterContext *ctx)
128 {
129  print_stats(ctx);
130 }
131 
133  {
134  .name = "default",
135  .type = AVMEDIA_TYPE_AUDIO,
136  .filter_frame = filter_frame,
137  },
138  { NULL }
139 };
140 
142  {
143  .name = "default",
144  .type = AVMEDIA_TYPE_AUDIO,
145  },
146  { NULL }
147 };
148 
150  .name = "volumedetect",
151  .description = NULL_IF_CONFIG_SMALL("Detect audio volume."),
152  .priv_size = sizeof(VolDetectContext),
154  .uninit = uninit,
155  .inputs = volumedetect_inputs,
156  .outputs = volumedetect_outputs,
157 };
int plane
Definition: avisynth_c.h:291
#define NULL
Definition: coverity.c:32
float v
static int shift(int a, int b)
Definition: sonic.c:82
This structure describes decoded (raw) audio or video data.
Definition: frame.h:171
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:248
Main libavfilter public API header.
static enum AVSampleFormat formats[]
static double logdb(uint64_t v)
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:283
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
static int query_formats(AVFilterContext *ctx)
const char * name
Pad name.
Definition: internal.h:69
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1158
#define av_cold
Definition: attributes.h:74
uint64_t histogram[0x10001]
Number of samples at each PCM value.
static int filter_frame(AVFilterLink *inlink, AVFrame *samples)
#define av_log(a,...)
int av_sample_fmt_is_planar(enum AVSampleFormat sample_fmt)
Check if the sample format is planar.
Definition: samplefmt.c:110
A filter pad used for either input or output.
Definition: internal.h:63
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:542
static const AVFilterPad volumedetect_outputs[]
#define AVERROR(e)
Definition: error.h:43
static av_cold void uninit(AVFilterContext *ctx)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:175
void * priv
private data for use by the filter
Definition: avfilter.h:654
simple assert() macros that are a bit more flexible than ISO C assert().
uint64_t channel_layout
Channel layout of the audio data.
Definition: frame.h:427
static void print_stats(AVFilterContext *ctx)
#define MAX_DB
audio channel layout utility functions
#define av_log2
Definition: intmath.h:100
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:232
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:59
Filter definition.
Definition: avfilter.h:470
static const AVFilterPad inputs[]
Definition: af_ashowinfo.c:239
static const AVFilterPad volumedetect_inputs[]
const char * name
Filter name.
Definition: avfilter.h:474
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:648
signed 16 bits
Definition: samplefmt.h:62
AVFilter ff_af_volumedetect
A list of supported formats for one end of a filter link.
Definition: formats.h:64
uint64_t layout
An instance of a filter.
Definition: avfilter.h:633
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:701
signed 16 bits, planar
Definition: samplefmt.h:68
int nb_channels
internal API functions
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:215
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:225
for(j=16;j >0;--j)