• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/avstring.h"
22 #include "libavutil/intreadwrite.h"
23 #include "libavutil/opt.h"
24 #include "libavutil/parseutils.h"
25 #include "avfilter.h"
26 #include "filters.h"
27 #include "formats.h"
28 #include "audio.h"
29 #include "video.h"
30 #include "internal.h"
31 
32 typedef struct AudioBitScopeContext {
33     const AVClass *class;
34     int w, h;
35     AVRational frame_rate;
36     char *colors;
37     int mode;
38 
39     int nb_channels;
40     int nb_samples;
41     int depth;
42     int current_vpos;
43     uint8_t *fg;
44 
45     uint64_t counter[64];
46 
47     AVFrame *outpicref;
48 } AudioBitScopeContext;
49 
50 #define OFFSET(x) offsetof(AudioBitScopeContext, x)
51 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
52 
53 static const AVOption abitscope_options[] = {
54     { "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
55     { "r",    "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
56     { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="1024x256"}, 0, 0, FLAGS },
57     { "s",    "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="1024x256"}, 0, 0, FLAGS },
58     { "colors", "set channels colors", OFFSET(colors), AV_OPT_TYPE_STRING, {.str = "red|green|blue|yellow|orange|lime|pink|magenta|brown" }, 0, 0, FLAGS },
59     { "mode", "set output mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 1, FLAGS, "mode" },
60     { "m",    "set output mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 1, FLAGS, "mode" },
61     { "bars",  NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, 0, 0, FLAGS, "mode" },
62     { "trace", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 1 }, 0, 0, FLAGS, "mode" },
63     { NULL }
64 };
65 
66 AVFILTER_DEFINE_CLASS(abitscope);
67 
query_formats(AVFilterContext * ctx)68 static int query_formats(AVFilterContext *ctx)
69 {
70     AVFilterFormats *formats = NULL;
71     AVFilterChannelLayouts *layouts;
72     AVFilterLink *inlink = ctx->inputs[0];
73     AVFilterLink *outlink = ctx->outputs[0];
74     static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16P, AV_SAMPLE_FMT_S32P,
75                                                        AV_SAMPLE_FMT_U8P,  AV_SAMPLE_FMT_S64P,
76                                                        AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_DBLP,
77                                                        AV_SAMPLE_FMT_NONE };
78     static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGBA, AV_PIX_FMT_NONE };
79     int ret;
80 
81     formats = ff_make_format_list(sample_fmts);
82     if ((ret = ff_formats_ref(formats, &inlink->outcfg.formats)) < 0)
83         return ret;
84 
85     layouts = ff_all_channel_counts();
86     if (!layouts)
87         return AVERROR(ENOMEM);
88     if ((ret = ff_channel_layouts_ref(layouts, &inlink->outcfg.channel_layouts)) < 0)
89         return ret;
90 
91     formats = ff_all_samplerates();
92     if ((ret = ff_formats_ref(formats, &inlink->outcfg.samplerates)) < 0)
93         return ret;
94 
95     formats = ff_make_format_list(pix_fmts);
96     if ((ret = ff_formats_ref(formats, &outlink->incfg.formats)) < 0)
97         return ret;
98 
99     return 0;
100 }
101 
config_input(AVFilterLink * inlink)102 static int config_input(AVFilterLink *inlink)
103 {
104     AVFilterContext *ctx = inlink->dst;
105     AudioBitScopeContext *s = ctx->priv;
106     int ch;
107     char *colors, *saveptr = NULL;
108 
109     s->nb_samples = FFMAX(1, av_rescale(inlink->sample_rate, s->frame_rate.den, s->frame_rate.num));
110     s->nb_channels = inlink->ch_layout.nb_channels;
111     s->depth = inlink->format == AV_SAMPLE_FMT_S16P ? 16 : 32;
112 
113     s->fg = av_malloc_array(s->nb_channels, 4 * sizeof(*s->fg));
114     if (!s->fg)
115         return AVERROR(ENOMEM);
116 
117     colors = av_strdup(s->colors);
118     if (!colors)
119         return AVERROR(ENOMEM);
120 
121     for (ch = 0; ch < s->nb_channels; ch++) {
122         uint8_t fg[4] = { 0xff, 0xff, 0xff, 0xff };
123         char *color;
124 
125         color = av_strtok(ch == 0 ? colors : NULL, " |", &saveptr);
126         if (color)
127             av_parse_color(fg, color, -1, ctx);
128         s->fg[4 * ch + 0] = fg[0];
129         s->fg[4 * ch + 1] = fg[1];
130         s->fg[4 * ch + 2] = fg[2];
131         s->fg[4 * ch + 3] = fg[3];
132     }
133     av_free(colors);
134 
135     return 0;
136 }
137 
config_output(AVFilterLink * outlink)138 static int config_output(AVFilterLink *outlink)
139 {
140     AudioBitScopeContext *s = outlink->src->priv;
141 
142     outlink->w = s->w;
143     outlink->h = s->h;
144     outlink->sample_aspect_ratio = (AVRational){1,1};
145     outlink->frame_rate = s->frame_rate;
146 
147     return 0;
148 }
149 
150 #define BARS(type, depth, one)                                              \
151     for (int ch = 0; ch < inlink->ch_layout.nb_channels; ch++) {            \
152         const type *in = (const type *)insamples->extended_data[ch];        \
153         const int w = outpicref->width / inlink->ch_layout.nb_channels;     \
154         const int h = outpicref->height / depth;                            \
155         const uint32_t color = AV_RN32(&s->fg[4 * ch]);                     \
156                                                                             \
157         memset(s->counter, 0, sizeof(s->counter));                          \
158         for (int i = 0; i < insamples->nb_samples; i++) {                   \
159             for (int j = 0; j < depth; j++) {                               \
160                 if (in[i] & (one << j))                                     \
161                     s->counter[j]++;                                        \
162             }                                                               \
163         }                                                                   \
164                                                                             \
165         for (int b = 0; b < depth; b++) {                                   \
166             for (int j = 1; j < h - 1; j++) {                               \
167                 uint8_t *dst = outpicref->data[0] + (b * h + j) * outpicref->linesize[0] + w * ch * 4; \
168                 const int ww = (s->counter[depth - b - 1] / (float)insamples->nb_samples) * (w - 1); \
169                                                                             \
170                 for (int i = 0; i < ww; i++) {                              \
171                     AV_WN32(&dst[i * 4], color);                            \
172                 }                                                           \
173             }                                                               \
174         }                                                                   \
175     }
176 
177 #define DO_TRACE(type, depth, one)                                          \
178     for (int ch = 0; ch < inlink->ch_layout.nb_channels; ch++) {            \
179         const int w = outpicref->width / inlink->ch_layout.nb_channels;     \
180         const type *in = (const type *)insamples->extended_data[ch];        \
181         const int wb = w / depth;                                           \
182         int wv;                                                             \
183                                                                             \
184         memset(s->counter, 0, sizeof(s->counter));                          \
185         for (int i = 0; i < insamples->nb_samples; i++) {                   \
186             for (int j = 0; j < depth; j++) {                               \
187                 if (in[i] & (one << j))                                     \
188                     s->counter[j]++;                                        \
189             }                                                               \
190         }                                                                   \
191                                                                             \
192         for (int b = 0; b < depth; b++) {                                   \
193             uint8_t colors[4];                                              \
194             uint32_t color;                                                 \
195             uint8_t *dst = outpicref->data[0] + w * ch * 4 + wb * b * 4 +   \
196                            s->current_vpos * outpicref->linesize[0];        \
197             wv = (s->counter[depth - b - 1] * 255) / insamples->nb_samples; \
198             colors[0] = (wv * s->fg[ch * 4 + 0] + 127) / 255;               \
199             colors[1] = (wv * s->fg[ch * 4 + 1] + 127) / 255;               \
200             colors[2] = (wv * s->fg[ch * 4 + 2] + 127) / 255;               \
201             colors[3] = (wv * s->fg[ch * 4 + 3] + 127) / 255;               \
202             color = AV_RN32(colors);                                        \
203                                                                             \
204             for (int x = 0; x < wb; x++)                                    \
205                 AV_WN32(&dst[x * 4], color);                                \
206         }                                                                   \
207     }
208 
filter_frame(AVFilterLink * inlink,AVFrame * insamples)209 static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
210 {
211     AVFilterContext *ctx = inlink->dst;
212     AVFilterLink *outlink = ctx->outputs[0];
213     AudioBitScopeContext *s = ctx->priv;
214     AVFrame *outpicref;
215 
216     if (s->mode == 0 || !s->outpicref) {
217         outpicref = ff_get_video_buffer(outlink, outlink->w, outlink->h);
218         if (!outpicref) {
219             av_frame_free(&insamples);
220             return AVERROR(ENOMEM);
221         }
222 
223         for (int i = 0; i < outlink->h; i++)
224             memset(outpicref->data[0] + i * outpicref->linesize[0], 0, outlink->w * 4);
225         if (!s->outpicref && s->mode == 1)
226             s->outpicref = outpicref;
227     }
228 
229     if (s->mode == 1) {
230         av_frame_make_writable(s->outpicref);
231         outpicref = av_frame_clone(s->outpicref);
232         if (!outpicref)
233             return AVERROR(ENOMEM);
234     }
235 
236     outpicref->pts = insamples->pts;
237     outpicref->sample_aspect_ratio = (AVRational){1,1};
238 
239     switch (insamples->format) {
240     case AV_SAMPLE_FMT_U8P:
241         if (s->mode == 0) { BARS(uint8_t,   8, 1) } else { DO_TRACE(uint8_t,   8, 1) }
242         break;
243     case AV_SAMPLE_FMT_S16P:
244         if (s->mode == 0) { BARS(uint16_t, 16, 1) } else { DO_TRACE(uint16_t, 16, 1) }
245         break;
246     case AV_SAMPLE_FMT_FLTP:
247     case AV_SAMPLE_FMT_S32P:
248         if (s->mode == 0) { BARS(uint32_t, 32, 1U) } else { DO_TRACE(uint32_t, 32, 1U) }
249         break;
250     case AV_SAMPLE_FMT_DBLP:
251     case AV_SAMPLE_FMT_S64P:
252         if (s->mode == 0) { BARS(uint64_t, 64, 1ULL) } else { DO_TRACE(uint64_t, 64, 1ULL) }
253         break;
254     }
255 
256     s->current_vpos++;
257     if (s->current_vpos >= outlink->h)
258         s->current_vpos = 0;
259     av_frame_free(&insamples);
260 
261     return ff_filter_frame(outlink, outpicref);
262 }
263 
activate(AVFilterContext * ctx)264 static int activate(AVFilterContext *ctx)
265 {
266     AVFilterLink *inlink = ctx->inputs[0];
267     AVFilterLink *outlink = ctx->outputs[0];
268     AudioBitScopeContext *s = ctx->priv;
269     AVFrame *in;
270     int ret;
271 
272     FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
273 
274     ret = ff_inlink_consume_samples(inlink, s->nb_samples, s->nb_samples, &in);
275     if (ret < 0)
276         return ret;
277     if (ret > 0)
278         return filter_frame(inlink, in);
279 
280     FF_FILTER_FORWARD_STATUS(inlink, outlink);
281     FF_FILTER_FORWARD_WANTED(outlink, inlink);
282 
283     return FFERROR_NOT_READY;
284 }
285 
uninit(AVFilterContext * ctx)286 static av_cold void uninit(AVFilterContext *ctx)
287 {
288     AudioBitScopeContext *s = ctx->priv;
289 
290     av_frame_free(&s->outpicref);
291 }
292 
293 static const AVFilterPad inputs[] = {
294     {
295         .name         = "default",
296         .type         = AVMEDIA_TYPE_AUDIO,
297         .config_props = config_input,
298     },
299 };
300 
301 static const AVFilterPad outputs[] = {
302     {
303         .name         = "default",
304         .type         = AVMEDIA_TYPE_VIDEO,
305         .config_props = config_output,
306     },
307 };
308 
309 const AVFilter ff_avf_abitscope = {
310     .name          = "abitscope",
311     .description   = NULL_IF_CONFIG_SMALL("Convert input audio to audio bit scope video output."),
312     .priv_size     = sizeof(AudioBitScopeContext),
313     FILTER_INPUTS(inputs),
314     FILTER_OUTPUTS(outputs),
315     FILTER_QUERY_FUNC(query_formats),
316     .uninit        = uninit,
317     .activate      = activate,
318     .priv_class    = &abitscope_class,
319 };
320