FFmpeg
avf_abitscope.c
Go to the documentation of this file.
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;
36  char *colors;
37 
40  int depth;
41  uint8_t *fg;
42 
43  uint64_t counter[64];
45 
46 #define OFFSET(x) offsetof(AudioBitScopeContext, x)
47 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
48 
49 static const AVOption abitscope_options[] = {
50  { "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
51  { "r", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
52  { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="1024x256"}, 0, 0, FLAGS },
53  { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="1024x256"}, 0, 0, FLAGS },
54  { "colors", "set channels colors", OFFSET(colors), AV_OPT_TYPE_STRING, {.str = "red|green|blue|yellow|orange|lime|pink|magenta|brown" }, 0, 0, FLAGS },
55  { NULL }
56 };
57 
58 AVFILTER_DEFINE_CLASS(abitscope);
59 
61 {
64  AVFilterLink *inlink = ctx->inputs[0];
65  AVFilterLink *outlink = ctx->outputs[0];
67  static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGBA, AV_PIX_FMT_NONE };
68  int ret;
69 
71  if ((ret = ff_formats_ref(formats, &inlink->outcfg.formats)) < 0)
72  return ret;
73 
75  if (!layouts)
76  return AVERROR(ENOMEM);
77  if ((ret = ff_channel_layouts_ref(layouts, &inlink->outcfg.channel_layouts)) < 0)
78  return ret;
79 
81  if ((ret = ff_formats_ref(formats, &inlink->outcfg.samplerates)) < 0)
82  return ret;
83 
85  if ((ret = ff_formats_ref(formats, &outlink->incfg.formats)) < 0)
86  return ret;
87 
88  return 0;
89 }
90 
92 {
93  AVFilterContext *ctx = inlink->dst;
94  AudioBitScopeContext *s = ctx->priv;
95  int ch;
96  char *colors, *saveptr = NULL;
97 
98  s->nb_samples = FFMAX(1, av_rescale(inlink->sample_rate, s->frame_rate.den, s->frame_rate.num));
99  s->nb_channels = inlink->channels;
100  s->depth = inlink->format == AV_SAMPLE_FMT_S16P ? 16 : 32;
101 
102  s->fg = av_malloc_array(s->nb_channels, 4 * sizeof(*s->fg));
103  if (!s->fg)
104  return AVERROR(ENOMEM);
105 
106  colors = av_strdup(s->colors);
107  if (!colors)
108  return AVERROR(ENOMEM);
109 
110  for (ch = 0; ch < s->nb_channels; ch++) {
111  uint8_t fg[4] = { 0xff, 0xff, 0xff, 0xff };
112  char *color;
113 
114  color = av_strtok(ch == 0 ? colors : NULL, " |", &saveptr);
115  if (color)
116  av_parse_color(fg, color, -1, ctx);
117  s->fg[4 * ch + 0] = fg[0];
118  s->fg[4 * ch + 1] = fg[1];
119  s->fg[4 * ch + 2] = fg[2];
120  s->fg[4 * ch + 3] = fg[3];
121  }
122  av_free(colors);
123 
124  return 0;
125 }
126 
127 static int config_output(AVFilterLink *outlink)
128 {
129  AudioBitScopeContext *s = outlink->src->priv;
130 
131  outlink->w = s->w;
132  outlink->h = s->h;
133  outlink->sample_aspect_ratio = (AVRational){1,1};
134  outlink->frame_rate = s->frame_rate;
135 
136  return 0;
137 }
138 
139 static void count_bits(AudioBitScopeContext *s, uint32_t sample, int max)
140 {
141  int i;
142 
143  for (i = 0; i < max; i++) {
144  if (sample & (1U << i))
145  s->counter[i]++;
146  }
147 }
148 
149 static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
150 {
151  AVFilterContext *ctx = inlink->dst;
152  AVFilterLink *outlink = ctx->outputs[0];
153  AudioBitScopeContext *s = ctx->priv;
154  AVFrame *outpicref;
155  int ch, i, j, b;
156 
157  outpicref = ff_get_video_buffer(outlink, outlink->w, outlink->h);
158  if (!outpicref) {
159  av_frame_free(&insamples);
160  return AVERROR(ENOMEM);
161  }
162 
163  for (i = 0; i < outlink->h; i++)
164  memset(outpicref->data[0] + i * outpicref->linesize[0], 0, outlink->w * 4);
165 
166  outpicref->pts = insamples->pts;
167  outpicref->sample_aspect_ratio = (AVRational){1,1};
168 
169  switch (insamples->format) {
170  case AV_SAMPLE_FMT_S16P:
171  for (ch = 0; ch < inlink->channels; ch++) {
172  uint16_t *in = (uint16_t *)insamples->extended_data[ch];
173  int w = outpicref->width / inlink->channels;
174  int h = outpicref->height / 16;
175  uint32_t color = AV_RN32(&s->fg[4 * ch]);
176 
177  memset(s->counter, 0, sizeof(s->counter));
178  for (i = 0; i < insamples->nb_samples; i++)
179  count_bits(s, in[i], 16);
180 
181  for (b = 0; b < 16; b++) {
182  for (j = 1; j < h - 1; j++) {
183  uint8_t *dst = outpicref->data[0] + (b * h + j) * outpicref->linesize[0] + w * ch * 4;
184  int ww = (s->counter[16 - b - 1] / (float)insamples->nb_samples) * (w - 1);
185 
186  for (i = 0; i < ww; i++) {
187  AV_WN32(&dst[i * 4], color);
188  }
189  }
190  }
191  }
192  break;
193  case AV_SAMPLE_FMT_S32P:
194  for (ch = 0; ch < inlink->channels; ch++) {
195  uint32_t *in = (uint32_t *)insamples->extended_data[ch];
196  int w = outpicref->width / inlink->channels;
197  int h = outpicref->height / 32;
198  uint32_t color = AV_RN32(&s->fg[4 * ch]);
199 
200  memset(s->counter, 0, sizeof(s->counter));
201  for (i = 0; i < insamples->nb_samples; i++)
202  count_bits(s, in[i], 32);
203 
204  for (b = 0; b < 32; b++) {
205  for (j = 1; j < h - 1; j++) {
206  uint8_t *dst = outpicref->data[0] + (b * h + j) * outpicref->linesize[0] + w * ch * 4;
207  int ww = (s->counter[32 - b - 1] / (float)insamples->nb_samples) * (w - 1);
208 
209  for (i = 0; i < ww; i++) {
210  AV_WN32(&dst[i * 4], color);
211  }
212  }
213  }
214  }
215  break;
216  }
217 
218  av_frame_free(&insamples);
219 
220  return ff_filter_frame(outlink, outpicref);
221 }
222 
224 {
225  AVFilterLink *inlink = ctx->inputs[0];
226  AVFilterLink *outlink = ctx->outputs[0];
227  AudioBitScopeContext *s = ctx->priv;
228  AVFrame *in;
229  int ret;
230 
232 
233  ret = ff_inlink_consume_samples(inlink, s->nb_samples, s->nb_samples, &in);
234  if (ret < 0)
235  return ret;
236  if (ret > 0)
237  return filter_frame(inlink, in);
238 
241 
242  return FFERROR_NOT_READY;
243 }
244 
245 static const AVFilterPad inputs[] = {
246  {
247  .name = "default",
248  .type = AVMEDIA_TYPE_AUDIO,
249  .config_props = config_input,
250  },
251 };
252 
253 static const AVFilterPad outputs[] = {
254  {
255  .name = "default",
256  .type = AVMEDIA_TYPE_VIDEO,
257  .config_props = config_output,
258  },
259 };
260 
262  .name = "abitscope",
263  .description = NULL_IF_CONFIG_SMALL("Convert input audio to audio bit scope video output."),
264  .priv_size = sizeof(AudioBitScopeContext),
268  .activate = activate,
269  .priv_class = &abitscope_class,
270 };
formats
formats
Definition: signature.h:48
ff_get_video_buffer
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:98
AVFilterChannelLayouts
A list of supported channel layouts.
Definition: formats.h:85
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
ff_make_format_list
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:381
color
Definition: vf_paletteuse.c:599
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1018
sample_fmts
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:948
AudioBitScopeContext::nb_samples
int nb_samples
Definition: avf_abitscope.c:39
ff_channel_layouts_ref
int ff_channel_layouts_ref(AVFilterChannelLayouts *f, AVFilterChannelLayouts **ref)
Add *ref as a new reference to f.
Definition: formats.c:550
layouts
enum MovChannelLayoutTag * layouts
Definition: mov_chan.c:434
av_parse_color
int av_parse_color(uint8_t *rgba_color, const char *color_string, int slen, void *log_ctx)
Put the RGBA values that correspond to color_string in rgba_color.
Definition: parseutils.c:356
FFERROR_NOT_READY
return FFERROR_NOT_READY
Definition: filter_design.txt:204
AV_OPT_TYPE_VIDEO_RATE
@ AV_OPT_TYPE_VIDEO_RATE
offset must point to AVRational
Definition: opt.h:237
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:109
ff_all_channel_counts
AVFilterChannelLayouts * ff_all_channel_counts(void)
Construct an AVFilterChannelLayouts coding for any channel layout, with known or unknown disposition.
Definition: formats.c:525
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:317
AVFrame::pts
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:424
AVFrame::width
int width
Definition: frame.h:389
w
uint8_t w
Definition: llviddspenc.c:38
AVOption
AVOption.
Definition: opt.h:247
b
#define b
Definition: input.c:40
FILTER_QUERY_FUNC
#define FILTER_QUERY_FUNC(func)
Definition: internal.h:168
AV_SAMPLE_FMT_S32P
@ AV_SAMPLE_FMT_S32P
signed 32 bits, planar
Definition: samplefmt.h:68
max
#define max(a, b)
Definition: cuda_runtime.h:33
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:169
OFFSET
#define OFFSET(x)
Definition: avf_abitscope.c:46
video.h
FF_FILTER_FORWARD_STATUS_BACK
#define FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink)
Forward the status on an output link to an input link.
Definition: filters.h:199
abitscope_options
static const AVOption abitscope_options[]
Definition: avf_abitscope.c:49
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:338
AudioBitScopeContext::frame_rate
AVRational frame_rate
Definition: avf_abitscope.c:35
AVFilterFormats
A list of supported formats for one end of a filter link.
Definition: formats.h:64
formats.h
FLAGS
#define FLAGS
Definition: avf_abitscope.c:47
AudioBitScopeContext::fg
uint8_t * fg
Definition: avf_abitscope.c:41
AVFilterContext::priv
void * priv
private data for use by the filter
Definition: avfilter.h:417
U
#define U(x)
Definition: vp56_arith.h:37
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:50
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:257
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
ff_formats_ref
int ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add *ref as a new reference to formats.
Definition: formats.c:555
count_bits
static void count_bits(AudioBitScopeContext *s, uint32_t sample, int max)
Definition: avf_abitscope.c:139
av_strtok
char * av_strtok(char *s, const char *delim, char **saveptr)
Split the string into several tokens which can be accessed by successive calls to av_strtok().
Definition: avstring.c:186
filters.h
pix_fmts
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:296
ctx
AVFormatContext * ctx
Definition: movenc.c:48
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(abitscope)
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:191
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
Definition: avf_abitscope.c:149
AV_PIX_FMT_RGBA
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:93
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
ff_inlink_consume_samples
int ff_inlink_consume_samples(AVFilterLink *link, unsigned min, unsigned max, AVFrame **rframe)
Take samples from the link's FIFO and update the link's stats.
Definition: avfilter.c:1436
NULL
#define NULL
Definition: coverity.c:32
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
AudioBitScopeContext
Definition: avf_abitscope.c:32
AV_OPT_TYPE_IMAGE_SIZE
@ AV_OPT_TYPE_IMAGE_SIZE
offset must point to two consecutive integers
Definition: opt.h:234
AV_RN32
#define AV_RN32(p)
Definition: intreadwrite.h:364
parseutils.h
AudioBitScopeContext::counter
uint64_t counter[64]
Definition: avf_abitscope.c:43
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
query_formats
static int query_formats(AVFilterContext *ctx)
Definition: avf_abitscope.c:60
AV_WN32
#define AV_WN32(p, v)
Definition: intreadwrite.h:376
inputs
static const AVFilterPad inputs[]
Definition: avf_abitscope.c:245
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:59
sample
#define sample
Definition: flacdsp_template.c:44
color
static const uint32_t color[16+AV_CLASS_CATEGORY_NB]
Definition: log.c:92
ff_avf_abitscope
const AVFilter ff_avf_abitscope
Definition: avf_abitscope.c:261
AVFrame::format
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition: frame.h:404
AudioBitScopeContext::w
int w
Definition: avf_abitscope.c:34
FF_FILTER_FORWARD_WANTED
FF_FILTER_FORWARD_WANTED(outlink, inlink)
outputs
static const AVFilterPad outputs[]
Definition: avf_abitscope.c:253
AV_SAMPLE_FMT_S16P
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
Definition: samplefmt.h:67
internal.h
AudioBitScopeContext::h
int h
Definition: avf_abitscope.c:34
AVFrame::nb_samples
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:397
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
AVFrame::extended_data
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:378
av_malloc_array
#define av_malloc_array(a, b)
Definition: tableprint_vlc.h:32
AudioBitScopeContext::depth
int depth
Definition: avf_abitscope.c:40
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:56
av_rescale
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:128
AVFilter
Filter definition.
Definition: avfilter.h:165
ret
ret
Definition: filter_design.txt:187
AudioBitScopeContext::nb_channels
int nb_channels
Definition: avf_abitscope.c:38
AVFrame::sample_aspect_ratio
AVRational sample_aspect_ratio
Sample aspect ratio for the video frame, 0/1 if unknown/unspecified.
Definition: frame.h:419
config_input
static int config_input(AVFilterLink *inlink)
Definition: avf_abitscope.c:91
AVFrame::height
int height
Definition: frame.h:389
ff_all_samplerates
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:510
activate
static int activate(AVFilterContext *ctx)
Definition: avf_abitscope.c:223
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
avfilter.h
AVFilterContext
An instance of a filter.
Definition: avfilter.h:402
av_strdup
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:279
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
config_output
static int config_output(AVFilterLink *outlink)
Definition: avf_abitscope.c:127
audio.h
AVFilterFormatsConfig::formats
AVFilterFormats * formats
List of supported formats (pixel or sample).
Definition: avfilter.h:506
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
FF_FILTER_FORWARD_STATUS
FF_FILTER_FORWARD_STATUS(inlink, outlink)
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:192
AudioBitScopeContext::colors
char * colors
Definition: avf_abitscope.c:36
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:362
h
h
Definition: vp9dsp_template.c:2038
avstring.h
AV_OPT_TYPE_STRING
@ AV_OPT_TYPE_STRING
Definition: opt.h:228