2 * Copyright (c) 2016 Paul B Mahol
4 * This file is part of FFmpeg.
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.
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.
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
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"
33 typedef struct AudioBitScopeContext {
36 AVRational frame_rate;
45 } AudioBitScopeContext;
47 #define OFFSET(x) offsetof(AudioBitScopeContext, x)
48 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
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 },
59 AVFILTER_DEFINE_CLASS(abitscope);
61 static int query_formats(AVFilterContext *ctx)
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 };
71 formats = ff_make_format_list(sample_fmts);
72 if ((ret = ff_formats_ref(formats, &inlink->outcfg.formats)) < 0)
75 layouts = ff_all_channel_counts();
77 return AVERROR(ENOMEM);
78 if ((ret = ff_channel_layouts_ref(layouts, &inlink->outcfg.channel_layouts)) < 0)
81 formats = ff_all_samplerates();
82 if ((ret = ff_formats_ref(formats, &inlink->outcfg.samplerates)) < 0)
85 formats = ff_make_format_list(pix_fmts);
86 if ((ret = ff_formats_ref(formats, &outlink->incfg.formats)) < 0)
92 static int config_input(AVFilterLink *inlink)
94 AVFilterContext *ctx = inlink->dst;
95 AudioBitScopeContext *s = ctx->priv;
97 char *colors, *saveptr = NULL;
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;
103 s->fg = av_malloc_array(s->nb_channels, 4 * sizeof(*s->fg));
105 return AVERROR(ENOMEM);
107 colors = av_strdup(s->colors);
109 return AVERROR(ENOMEM);
111 for (ch = 0; ch < s->nb_channels; ch++) {
112 uint8_t fg[4] = { 0xff, 0xff, 0xff, 0xff };
115 color = av_strtok(ch == 0 ? colors : NULL, " |", &saveptr);
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];
128 static int config_output(AVFilterLink *outlink)
130 AudioBitScopeContext *s = outlink->src->priv;
134 outlink->sample_aspect_ratio = (AVRational){1,1};
135 outlink->frame_rate = s->frame_rate;
140 static void count_bits(AudioBitScopeContext *s, uint32_t sample, int max)
144 for (i = 0; i < max; i++) {
145 if (sample & (1U << i))
150 static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
152 AVFilterContext *ctx = inlink->dst;
153 AVFilterLink *outlink = ctx->outputs[0];
154 AudioBitScopeContext *s = ctx->priv;
158 outpicref = ff_get_video_buffer(outlink, outlink->w, outlink->h);
160 av_frame_free(&insamples);
161 return AVERROR(ENOMEM);
164 for (i = 0; i < outlink->h; i++)
165 memset(outpicref->data[0] + i * outpicref->linesize[0], 0, outlink->w * 4);
167 outpicref->pts = insamples->pts;
168 outpicref->sample_aspect_ratio = (AVRational){1,1};
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]);
178 memset(s->counter, 0, sizeof(s->counter));
179 for (i = 0; i < insamples->nb_samples; i++)
180 count_bits(s, in[i], 16);
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);
187 for (i = 0; i < ww; i++) {
188 AV_WN32(&dst[i * 4], color);
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]);
201 memset(s->counter, 0, sizeof(s->counter));
202 for (i = 0; i < insamples->nb_samples; i++)
203 count_bits(s, in[i], 32);
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);
210 for (i = 0; i < ww; i++) {
211 AV_WN32(&dst[i * 4], color);
219 av_frame_free(&insamples);
221 return ff_filter_frame(outlink, outpicref);
224 static int activate(AVFilterContext *ctx)
226 AVFilterLink *inlink = ctx->inputs[0];
227 AVFilterLink *outlink = ctx->outputs[0];
228 AudioBitScopeContext *s = ctx->priv;
232 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
234 ret = ff_inlink_consume_samples(inlink, s->nb_samples, s->nb_samples, &in);
238 return filter_frame(inlink, in);
240 FF_FILTER_FORWARD_STATUS(inlink, outlink);
241 FF_FILTER_FORWARD_WANTED(outlink, inlink);
243 return FFERROR_NOT_READY;
246 static const AVFilterPad inputs[] = {
249 .type = AVMEDIA_TYPE_AUDIO,
250 .config_props = config_input,
255 static const AVFilterPad outputs[] = {
258 .type = AVMEDIA_TYPE_VIDEO,
259 .config_props = config_output,
264 AVFilter ff_avf_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),
271 .activate = activate,
272 .priv_class = &abitscope_class,