Browse code

avfilter: add abitscope multimedia filter

Signed-off-by: Paul B Mahol <onemda@gmail.com>

Paul B Mahol authored on 2016/05/05 19:15:39
Showing 6 changed files
... ...
@@ -17,6 +17,7 @@ version <next>:
17 17
 - MIDI Sample Dump Standard demuxer
18 18
 - readeia608 filter
19 19
 - Sample Dump eXchange demuxer
20
+- abitscope multimedia filter
20 21
 
21 22
 version 3.2:
22 23
 - libopenmpt demuxer
... ...
@@ -15627,6 +15627,28 @@ tools.
15627 15627
 
15628 15628
 Below is a description of the currently available multimedia filters.
15629 15629
 
15630
+@section abitscope
15631
+
15632
+Convert input audio to a video output, displaying the audio bit scope.
15633
+
15634
+The filter accepts the following options:
15635
+
15636
+@table @option
15637
+@item rate, r
15638
+Set frame rate, expressed as number of frames per second. Default
15639
+value is "25".
15640
+
15641
+@item size, s
15642
+Specify the video size for the output. For the syntax of this option, check the
15643
+@ref{video size syntax,,"Video size" section in the ffmpeg-utils manual,ffmpeg-utils}.
15644
+Default value is @code{1024x256}.
15645
+
15646
+@item colors
15647
+Specify list of colors separated by space or by '|' which will be used to
15648
+draw channels. Unrecognized or missing colors will be replaced
15649
+by white color.
15650
+@end table
15651
+
15630 15652
 @section ahistogram
15631 15653
 
15632 15654
 Convert input audio to a video output, displaying the volume histogram.
... ...
@@ -333,6 +333,7 @@ OBJS-$(CONFIG_YUVTESTSRC_FILTER)             += vsrc_testsrc.o
333 333
 OBJS-$(CONFIG_NULLSINK_FILTER)               += vsink_nullsink.o
334 334
 
335 335
 # multimedia filters
336
+OBJS-$(CONFIG_ABITSCOPE_FILTER)              += avf_abitscope.o
336 337
 OBJS-$(CONFIG_ADRAWGRAPH_FILTER)             += f_drawgraph.o
337 338
 OBJS-$(CONFIG_AHISTOGRAM_FILTER)             += avf_ahistogram.o
338 339
 OBJS-$(CONFIG_APHASEMETER_FILTER)            += avf_aphasemeter.o
... ...
@@ -348,6 +348,7 @@ void avfilter_register_all(void)
348 348
     REGISTER_FILTER(NULLSINK,       nullsink,       vsink);
349 349
 
350 350
     /* multimedia filters */
351
+    REGISTER_FILTER(ABITSCOPE,      abitscope,      avf);
351 352
     REGISTER_FILTER(ADRAWGRAPH,     adrawgraph,     avf);
352 353
     REGISTER_FILTER(AHISTOGRAM,     ahistogram,     avf);
353 354
     REGISTER_FILTER(APHASEMETER,    aphasemeter,    avf);
354 355
new file mode 100644
... ...
@@ -0,0 +1,250 @@
0
+/*
1
+ * Copyright (c) 2016 Paul B Mahol
2
+ *
3
+ * This file is part of FFmpeg.
4
+ *
5
+ * FFmpeg is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * FFmpeg is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with FFmpeg; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+#include "libavutil/avassert.h"
21
+#include "libavutil/avstring.h"
22
+#include "libavutil/intreadwrite.h"
23
+#include "libavutil/opt.h"
24
+#include "libavutil/parseutils.h"
25
+#include "avfilter.h"
26
+#include "formats.h"
27
+#include "audio.h"
28
+#include "video.h"
29
+#include "internal.h"
30
+
31
+typedef struct AudioBitScopeContext {
32
+    const AVClass *class;
33
+    int w, h;
34
+    AVRational frame_rate;
35
+    char *colors;
36
+
37
+    int nb_channels;
38
+    int depth;
39
+    uint8_t *fg;
40
+
41
+    uint64_t counter[64];
42
+} AudioBitScopeContext;
43
+
44
+#define OFFSET(x) offsetof(AudioBitScopeContext, x)
45
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
46
+
47
+static const AVOption abitscope_options[] = {
48
+    { "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, 0, FLAGS },
49
+    { "r",    "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, 0, FLAGS },
50
+    { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="1024x256"}, 0, 0, FLAGS },
51
+    { "s",    "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="1024x256"}, 0, 0, FLAGS },
52
+    { "colors", "set channels colors", OFFSET(colors), AV_OPT_TYPE_STRING, {.str = "red|green|blue|yellow|orange|lime|pink|magenta|brown" }, 0, 0, FLAGS },
53
+    { NULL }
54
+};
55
+
56
+AVFILTER_DEFINE_CLASS(abitscope);
57
+
58
+static int query_formats(AVFilterContext *ctx)
59
+{
60
+    AVFilterFormats *formats = NULL;
61
+    AVFilterChannelLayouts *layouts;
62
+    AVFilterLink *inlink = ctx->inputs[0];
63
+    AVFilterLink *outlink = ctx->outputs[0];
64
+    static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16P, AV_SAMPLE_FMT_S32P, AV_SAMPLE_FMT_NONE };
65
+    static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGBA, AV_PIX_FMT_NONE };
66
+    int ret;
67
+
68
+    formats = ff_make_format_list(sample_fmts);
69
+    if ((ret = ff_formats_ref(formats, &inlink->out_formats)) < 0)
70
+        return ret;
71
+
72
+    layouts = ff_all_channel_counts();
73
+    if (!layouts)
74
+        return AVERROR(ENOMEM);
75
+    if ((ret = ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts)) < 0)
76
+        return ret;
77
+
78
+    formats = ff_all_samplerates();
79
+    if ((ret = ff_formats_ref(formats, &inlink->out_samplerates)) < 0)
80
+        return ret;
81
+
82
+    formats = ff_make_format_list(pix_fmts);
83
+    if ((ret = ff_formats_ref(formats, &outlink->in_formats)) < 0)
84
+        return ret;
85
+
86
+    return 0;
87
+}
88
+
89
+static int config_input(AVFilterLink *inlink)
90
+{
91
+    AVFilterContext *ctx = inlink->dst;
92
+    AudioBitScopeContext *s = ctx->priv;
93
+    int ch, nb_samples;
94
+    char *colors, *saveptr = NULL;
95
+
96
+    nb_samples = FFMAX(1024, ((double)inlink->sample_rate / av_q2d(s->frame_rate)) + 0.5);
97
+    inlink->partial_buf_size =
98
+    inlink->min_samples =
99
+    inlink->max_samples = nb_samples;
100
+    s->nb_channels = inlink->channels;
101
+    s->depth = inlink->format == AV_SAMPLE_FMT_S16P ? 16 : 32;
102
+
103
+    s->fg = av_malloc_array(s->nb_channels, 4 * sizeof(*s->fg));
104
+    if (!s->fg)
105
+        return AVERROR(ENOMEM);
106
+
107
+    colors = av_strdup(s->colors);
108
+    if (!colors)
109
+        return AVERROR(ENOMEM);
110
+
111
+    for (ch = 0; ch < s->nb_channels; ch++) {
112
+        uint8_t fg[4] = { 0xff, 0xff, 0xff, 0xff };
113
+        char *color;
114
+
115
+        color = av_strtok(ch == 0 ? colors : NULL, " |", &saveptr);
116
+        if (color)
117
+            av_parse_color(fg, color, -1, ctx);
118
+        s->fg[4 * ch + 0] = fg[0];
119
+        s->fg[4 * ch + 1] = fg[1];
120
+        s->fg[4 * ch + 2] = fg[2];
121
+        s->fg[4 * ch + 3] = fg[3];
122
+    }
123
+    av_free(colors);
124
+
125
+    return 0;
126
+}
127
+
128
+static int config_output(AVFilterLink *outlink)
129
+{
130
+    AudioBitScopeContext *s = outlink->src->priv;
131
+
132
+    outlink->w = s->w;
133
+    outlink->h = s->h;
134
+    outlink->sample_aspect_ratio = (AVRational){1,1};
135
+    outlink->frame_rate = s->frame_rate;
136
+
137
+    return 0;
138
+}
139
+
140
+static void count_bits(AudioBitScopeContext *s, uint32_t sample, int max)
141
+{
142
+    int i;
143
+
144
+    for (i = 0; i < max; i++) {
145
+        if (sample & (1 << i))
146
+            s->counter[i]++;
147
+    }
148
+}
149
+
150
+static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
151
+{
152
+    AVFilterContext *ctx = inlink->dst;
153
+    AVFilterLink *outlink = ctx->outputs[0];
154
+    AudioBitScopeContext *s = ctx->priv;
155
+    AVFrame *outpicref;
156
+    int ch, i, j, b;
157
+
158
+    outpicref = ff_get_video_buffer(outlink, outlink->w, outlink->h);
159
+    if (!outpicref) {
160
+        av_frame_free(&insamples);
161
+        return AVERROR(ENOMEM);
162
+    }
163
+
164
+    for (i = 0; i < outlink->h; i++)
165
+        memset(outpicref->data[0] + i * outpicref->linesize[0], 0, outlink->w * 4);
166
+
167
+    outpicref->pts = insamples->pts;
168
+    switch (insamples->format) {
169
+    case AV_SAMPLE_FMT_S16P:
170
+        for (ch = 0; ch < inlink->channels; ch++) {
171
+            uint16_t *in = (uint16_t *)insamples->extended_data[ch];
172
+            int w = outpicref->width / inlink->channels;
173
+            int h = outpicref->height / 16;
174
+            uint32_t color = AV_RN32(&s->fg[4 * ch]);
175
+
176
+            memset(s->counter, 0, sizeof(s->counter));
177
+            for (i = 0; i < insamples->nb_samples; i++)
178
+                count_bits(s, in[i], 16);
179
+
180
+            for (b = 0; b < 16; b++) {
181
+                for (j = 1; j < h - 1; j++) {
182
+                    uint8_t *dst = outpicref->data[0] + (b * h + j) * outpicref->linesize[0] + w * ch * 4;
183
+                    int ww = (s->counter[16 - b - 1] / (float)insamples->nb_samples) * (w - 1);
184
+
185
+                    for (i = 0; i < ww; i++) {
186
+                        AV_WN32(&dst[i * 4], color);
187
+                    }
188
+                }
189
+            }
190
+        }
191
+        break;
192
+    case AV_SAMPLE_FMT_S32P:
193
+        for (ch = 0; ch < inlink->channels; ch++) {
194
+            uint32_t *in = (uint32_t *)insamples->extended_data[ch];
195
+            int w = outpicref->width / inlink->channels;
196
+            int h = outpicref->height / 32;
197
+            uint32_t color = AV_RN32(&s->fg[4 * ch]);
198
+
199
+            memset(s->counter, 0, sizeof(s->counter));
200
+            for (i = 0; i < insamples->nb_samples; i++)
201
+                count_bits(s, in[i], 32);
202
+
203
+            for (b = 0; b < 32; b++) {
204
+                for (j = 1; j < h - 1; j++) {
205
+                    uint8_t *dst = outpicref->data[0] + (b * h + j) * outpicref->linesize[0] + w * ch * 4;
206
+                    int ww = (s->counter[32 - b - 1] / (float)insamples->nb_samples) * (w - 1);
207
+
208
+                    for (i = 0; i < ww; i++) {
209
+                        AV_WN32(&dst[i * 4], color);
210
+                    }
211
+                }
212
+            }
213
+        }
214
+        break;
215
+    }
216
+
217
+    av_frame_free(&insamples);
218
+
219
+    return ff_filter_frame(outlink, outpicref);
220
+}
221
+
222
+static const AVFilterPad inputs[] = {
223
+    {
224
+        .name         = "default",
225
+        .type         = AVMEDIA_TYPE_AUDIO,
226
+        .config_props = config_input,
227
+        .filter_frame = filter_frame,
228
+    },
229
+    { NULL }
230
+};
231
+
232
+static const AVFilterPad outputs[] = {
233
+    {
234
+        .name         = "default",
235
+        .type         = AVMEDIA_TYPE_VIDEO,
236
+        .config_props = config_output,
237
+    },
238
+    { NULL }
239
+};
240
+
241
+AVFilter ff_avf_abitscope = {
242
+    .name          = "abitscope",
243
+    .description   = NULL_IF_CONFIG_SMALL("Convert input audio to audio bit scope video output."),
244
+    .query_formats = query_formats,
245
+    .priv_size     = sizeof(AudioBitScopeContext),
246
+    .inputs        = inputs,
247
+    .outputs       = outputs,
248
+    .priv_class    = &abitscope_class,
249
+};
... ...
@@ -30,7 +30,7 @@
30 30
 #include "libavutil/version.h"
31 31
 
32 32
 #define LIBAVFILTER_VERSION_MAJOR   6
33
-#define LIBAVFILTER_VERSION_MINOR  70
33
+#define LIBAVFILTER_VERSION_MINOR  71
34 34
 #define LIBAVFILTER_VERSION_MICRO 100
35 35
 
36 36
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \