1 /*
2 * Copyright (c) 2016 Paul B Mahol
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
8 * License 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 GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include "libavutil/avassert.h"
22 #include "libavutil/avstring.h"
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/opt.h"
25 #include "libavutil/parseutils.h"
26 #include "avfilter.h"
27 #include "filters.h"
28 #include "formats.h"
29 #include "audio.h"
30 #include "video.h"
31 #include "internal.h"
32
33 typedef struct AudioBitScopeContext {
34 const AVClass *class;
35 int w, h;
36 AVRational frame_rate;
37 char *colors;
38
39 int nb_channels;
40 int nb_samples;
41 int depth;
42 uint8_t *fg;
43
44 uint64_t counter[64];
45 } AudioBitScopeContext;
46
47 #define OFFSET(x) offsetof(AudioBitScopeContext, x)
48 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
49
50 static const AVOption abitscope_options[] = {
51 { "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
52 { "r", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
53 { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="1024x256"}, 0, 0, FLAGS },
54 { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="1024x256"}, 0, 0, FLAGS },
55 { "colors", "set channels colors", OFFSET(colors), AV_OPT_TYPE_STRING, {.str = "red|green|blue|yellow|orange|lime|pink|magenta|brown" }, 0, 0, FLAGS },
56 { NULL }
57 };
58
59 AVFILTER_DEFINE_CLASS(abitscope);
60
query_formats(AVFilterContext * ctx)61 static int query_formats(AVFilterContext *ctx)
62 {
63 AVFilterFormats *formats = NULL;
64 AVFilterChannelLayouts *layouts;
65 AVFilterLink *inlink = ctx->inputs[0];
66 AVFilterLink *outlink = ctx->outputs[0];
67 static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16P, AV_SAMPLE_FMT_S32P, AV_SAMPLE_FMT_NONE };
68 static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGBA, AV_PIX_FMT_NONE };
69 int ret;
70
71 formats = ff_make_format_list(sample_fmts);
72 if ((ret = ff_formats_ref(formats, &inlink->outcfg.formats)) < 0)
73 return ret;
74
75 layouts = ff_all_channel_counts();
76 if (!layouts)
77 return AVERROR(ENOMEM);
78 if ((ret = ff_channel_layouts_ref(layouts, &inlink->outcfg.channel_layouts)) < 0)
79 return ret;
80
81 formats = ff_all_samplerates();
82 if ((ret = ff_formats_ref(formats, &inlink->outcfg.samplerates)) < 0)
83 return ret;
84
85 formats = ff_make_format_list(pix_fmts);
86 if ((ret = ff_formats_ref(formats, &outlink->incfg.formats)) < 0)
87 return ret;
88
89 return 0;
90 }
91
config_input(AVFilterLink * inlink)92 static int config_input(AVFilterLink *inlink)
93 {
94 AVFilterContext *ctx = inlink->dst;
95 AudioBitScopeContext *s = ctx->priv;
96 int ch;
97 char *colors, *saveptr = NULL;
98
99 s->nb_samples = FFMAX(1, av_rescale(inlink->sample_rate, s->frame_rate.den, s->frame_rate.num));
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
config_output(AVFilterLink * outlink)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
count_bits(AudioBitScopeContext * s,uint32_t sample,int max)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 & (1U << i))
146 s->counter[i]++;
147 }
148 }
149
filter_frame(AVFilterLink * inlink,AVFrame * insamples)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 outpicref->sample_aspect_ratio = (AVRational){1,1};
169
170 switch (insamples->format) {
171 case AV_SAMPLE_FMT_S16P:
172 for (ch = 0; ch < inlink->channels; ch++) {
173 uint16_t *in = (uint16_t *)insamples->extended_data[ch];
174 int w = outpicref->width / inlink->channels;
175 int h = outpicref->height / 16;
176 uint32_t color = AV_RN32(&s->fg[4 * ch]);
177
178 memset(s->counter, 0, sizeof(s->counter));
179 for (i = 0; i < insamples->nb_samples; i++)
180 count_bits(s, in[i], 16);
181
182 for (b = 0; b < 16; b++) {
183 for (j = 1; j < h - 1; j++) {
184 uint8_t *dst = outpicref->data[0] + (b * h + j) * outpicref->linesize[0] + w * ch * 4;
185 int ww = (s->counter[16 - b - 1] / (float)insamples->nb_samples) * (w - 1);
186
187 for (i = 0; i < ww; i++) {
188 AV_WN32(&dst[i * 4], color);
189 }
190 }
191 }
192 }
193 break;
194 case AV_SAMPLE_FMT_S32P:
195 for (ch = 0; ch < inlink->channels; ch++) {
196 uint32_t *in = (uint32_t *)insamples->extended_data[ch];
197 int w = outpicref->width / inlink->channels;
198 int h = outpicref->height / 32;
199 uint32_t color = AV_RN32(&s->fg[4 * ch]);
200
201 memset(s->counter, 0, sizeof(s->counter));
202 for (i = 0; i < insamples->nb_samples; i++)
203 count_bits(s, in[i], 32);
204
205 for (b = 0; b < 32; b++) {
206 for (j = 1; j < h - 1; j++) {
207 uint8_t *dst = outpicref->data[0] + (b * h + j) * outpicref->linesize[0] + w * ch * 4;
208 int ww = (s->counter[32 - b - 1] / (float)insamples->nb_samples) * (w - 1);
209
210 for (i = 0; i < ww; i++) {
211 AV_WN32(&dst[i * 4], color);
212 }
213 }
214 }
215 }
216 break;
217 }
218
219 av_frame_free(&insamples);
220
221 return ff_filter_frame(outlink, outpicref);
222 }
223
activate(AVFilterContext * ctx)224 static int activate(AVFilterContext *ctx)
225 {
226 AVFilterLink *inlink = ctx->inputs[0];
227 AVFilterLink *outlink = ctx->outputs[0];
228 AudioBitScopeContext *s = ctx->priv;
229 AVFrame *in;
230 int ret;
231
232 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
233
234 ret = ff_inlink_consume_samples(inlink, s->nb_samples, s->nb_samples, &in);
235 if (ret < 0)
236 return ret;
237 if (ret > 0)
238 return filter_frame(inlink, in);
239
240 FF_FILTER_FORWARD_STATUS(inlink, outlink);
241 FF_FILTER_FORWARD_WANTED(outlink, inlink);
242
243 return FFERROR_NOT_READY;
244 }
245
246 static const AVFilterPad inputs[] = {
247 {
248 .name = "default",
249 .type = AVMEDIA_TYPE_AUDIO,
250 .config_props = config_input,
251 },
252 { NULL }
253 };
254
255 static const AVFilterPad outputs[] = {
256 {
257 .name = "default",
258 .type = AVMEDIA_TYPE_VIDEO,
259 .config_props = config_output,
260 },
261 { NULL }
262 };
263
264 AVFilter ff_avf_abitscope = {
265 .name = "abitscope",
266 .description = NULL_IF_CONFIG_SMALL("Convert input audio to audio bit scope video output."),
267 .query_formats = query_formats,
268 .priv_size = sizeof(AudioBitScopeContext),
269 .inputs = inputs,
270 .outputs = outputs,
271 .activate = activate,
272 .priv_class = &abitscope_class,
273 };
274