FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/avf_abitscope.c
Date: 2024-07-17 14:05:47
Exec Total Coverage
Lines: 0 111 0.0%
Functions: 0 6 0.0%
Branches: 0 169 0.0%

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