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"
32 typedef struct AudioBitScopeContext {
35 AVRational frame_rate;
43 } AudioBitScopeContext;
45 #define OFFSET(x) offsetof(AudioBitScopeContext, x)
46 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
48 static const AVOption abitscope_options[] = {
49 { "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
50 { "r", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
51 { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="1024x256"}, 0, 0, FLAGS },
52 { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="1024x256"}, 0, 0, FLAGS },
53 { "colors", "set channels colors", OFFSET(colors), AV_OPT_TYPE_STRING, {.str = "red|green|blue|yellow|orange|lime|pink|magenta|brown" }, 0, 0, FLAGS },
57 AVFILTER_DEFINE_CLASS(abitscope);
59 static int query_formats(AVFilterContext *ctx)
61 AVFilterFormats *formats = NULL;
62 AVFilterChannelLayouts *layouts;
63 AVFilterLink *inlink = ctx->inputs[0];
64 AVFilterLink *outlink = ctx->outputs[0];
65 static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16P, AV_SAMPLE_FMT_S32P, AV_SAMPLE_FMT_NONE };
66 static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGBA, AV_PIX_FMT_NONE };
69 formats = ff_make_format_list(sample_fmts);
70 if ((ret = ff_formats_ref(formats, &inlink->out_formats)) < 0)
73 layouts = ff_all_channel_counts();
75 return AVERROR(ENOMEM);
76 if ((ret = ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts)) < 0)
79 formats = ff_all_samplerates();
80 if ((ret = ff_formats_ref(formats, &inlink->out_samplerates)) < 0)
83 formats = ff_make_format_list(pix_fmts);
84 if ((ret = ff_formats_ref(formats, &outlink->in_formats)) < 0)
90 static int config_input(AVFilterLink *inlink)
92 AVFilterContext *ctx = inlink->dst;
93 AudioBitScopeContext *s = ctx->priv;
95 char *colors, *saveptr = NULL;
97 nb_samples = FFMAX(1024, ((double)inlink->sample_rate / av_q2d(s->frame_rate)) + 0.5);
98 inlink->partial_buf_size =
100 inlink->max_samples = nb_samples;
101 s->nb_channels = inlink->channels;
102 s->depth = inlink->format == AV_SAMPLE_FMT_S16P ? 16 : 32;
104 s->fg = av_malloc_array(s->nb_channels, 4 * sizeof(*s->fg));
106 return AVERROR(ENOMEM);
108 colors = av_strdup(s->colors);
110 return AVERROR(ENOMEM);
112 for (ch = 0; ch < s->nb_channels; ch++) {
113 uint8_t fg[4] = { 0xff, 0xff, 0xff, 0xff };
116 color = av_strtok(ch == 0 ? colors : NULL, " |", &saveptr);
118 av_parse_color(fg, color, -1, ctx);
119 s->fg[4 * ch + 0] = fg[0];
120 s->fg[4 * ch + 1] = fg[1];
121 s->fg[4 * ch + 2] = fg[2];
122 s->fg[4 * ch + 3] = fg[3];
129 static int config_output(AVFilterLink *outlink)
131 AudioBitScopeContext *s = outlink->src->priv;
135 outlink->sample_aspect_ratio = (AVRational){1,1};
136 outlink->frame_rate = s->frame_rate;
141 static void count_bits(AudioBitScopeContext *s, uint32_t sample, int max)
145 for (i = 0; i < max; i++) {
146 if (sample & (1 << i))
151 static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
153 AVFilterContext *ctx = inlink->dst;
154 AVFilterLink *outlink = ctx->outputs[0];
155 AudioBitScopeContext *s = ctx->priv;
159 outpicref = ff_get_video_buffer(outlink, outlink->w, outlink->h);
161 av_frame_free(&insamples);
162 return AVERROR(ENOMEM);
165 for (i = 0; i < outlink->h; i++)
166 memset(outpicref->data[0] + i * outpicref->linesize[0], 0, outlink->w * 4);
168 outpicref->pts = insamples->pts;
169 outpicref->sample_aspect_ratio = (AVRational){1,1};
171 switch (insamples->format) {
172 case AV_SAMPLE_FMT_S16P:
173 for (ch = 0; ch < inlink->channels; ch++) {
174 uint16_t *in = (uint16_t *)insamples->extended_data[ch];
175 int w = outpicref->width / inlink->channels;
176 int h = outpicref->height / 16;
177 uint32_t color = AV_RN32(&s->fg[4 * ch]);
179 memset(s->counter, 0, sizeof(s->counter));
180 for (i = 0; i < insamples->nb_samples; i++)
181 count_bits(s, in[i], 16);
183 for (b = 0; b < 16; b++) {
184 for (j = 1; j < h - 1; j++) {
185 uint8_t *dst = outpicref->data[0] + (b * h + j) * outpicref->linesize[0] + w * ch * 4;
186 int ww = (s->counter[16 - b - 1] / (float)insamples->nb_samples) * (w - 1);
188 for (i = 0; i < ww; i++) {
189 AV_WN32(&dst[i * 4], color);
195 case AV_SAMPLE_FMT_S32P:
196 for (ch = 0; ch < inlink->channels; ch++) {
197 uint32_t *in = (uint32_t *)insamples->extended_data[ch];
198 int w = outpicref->width / inlink->channels;
199 int h = outpicref->height / 32;
200 uint32_t color = AV_RN32(&s->fg[4 * ch]);
202 memset(s->counter, 0, sizeof(s->counter));
203 for (i = 0; i < insamples->nb_samples; i++)
204 count_bits(s, in[i], 32);
206 for (b = 0; b < 32; b++) {
207 for (j = 1; j < h - 1; j++) {
208 uint8_t *dst = outpicref->data[0] + (b * h + j) * outpicref->linesize[0] + w * ch * 4;
209 int ww = (s->counter[32 - b - 1] / (float)insamples->nb_samples) * (w - 1);
211 for (i = 0; i < ww; i++) {
212 AV_WN32(&dst[i * 4], color);
220 av_frame_free(&insamples);
222 return ff_filter_frame(outlink, outpicref);
225 static const AVFilterPad inputs[] = {
228 .type = AVMEDIA_TYPE_AUDIO,
229 .config_props = config_input,
230 .filter_frame = filter_frame,
235 static const AVFilterPad outputs[] = {
238 .type = AVMEDIA_TYPE_VIDEO,
239 .config_props = config_output,
244 AVFilter ff_avf_abitscope = {
246 .description = NULL_IF_CONFIG_SMALL("Convert input audio to audio bit scope video output."),
247 .query_formats = query_formats,
248 .priv_size = sizeof(AudioBitScopeContext),
251 .priv_class = &abitscope_class,