FFmpeg: libavfilter/avf_showwaves.c Source File
Go to the documentation of this file. 1 /*
2 * Copyright (c) 2012 Stefano Sabatini
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 /**
22 * @file
23 * audio to video multimedia filter
24 */
25
26 #include "config_components.h"
27
40
47 };
48
55 };
56
61 };
62
67 };
68
72 };
73
80 int16_t *
buf_idy;
/* y coordinate of previous sample for each channel */
87 int mode;
///< ShowWavesMode
93
96 int16_t *prev_y,
const uint8_t
color[4],
int h);
97
98 /* single picture */
105
106 #define OFFSET(x) offsetof(ShowWavesContext, x)
107 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
108
121 {
"colors",
"set channels colors",
OFFSET(colors),
AV_OPT_TYPE_STRING, {.str =
"red|green|blue|yellow|orange|lime|pink|magenta|brown" }, 0, 0,
FLAGS },
131 };
132
134
136 {
138
143
146 while (node) {
148
152 }
155 }
156 }
157
161 {
166
167 /* set input audio formats */
171
172 /* set output video format */
176
177 return 0;
178 }
179
181 {
183 }
184
186 {
188 }
189
191 {
193 }
194
196 {
198 }
199
201 {
203 }
204
206 {
208 }
209
211 {
213 }
214
216 {
218 }
219
221 int16_t *prev_y,
222 const uint8_t
color[4],
int h)
223 {
225 buf[
h * linesize + 0] +=
color[0];
226 buf[
h * linesize + 1] +=
color[1];
227 buf[
h * linesize + 2] +=
color[2];
228 buf[
h * linesize + 3] +=
color[3];
229 }
230 }
231
233 int16_t *prev_y,
234 const uint8_t
color[4],
int h)
235 {
239 }
240
242 int16_t *prev_y,
243 const uint8_t
color[4],
int h)
244 {
247 uint8_t *bufk;
248 if (start > end)
249 FFSWAP(int16_t, start, end);
250 bufk = buf + start * linesize;
251 for (int k = start; k < end; k++, bufk += linesize) {
256 }
257 }
258
260 int16_t *prev_y,
261 const uint8_t
color[4],
int h)
262 {
266 uint8_t *bufk;
267 if (start > end)
268 FFSWAP(int16_t, start, end);
269 bufk = buf + start * linesize;
270 for (int k = start; k < end; k++, bufk += linesize)
272 }
273
275 int16_t *prev_y,
276 const uint8_t
color[4],
int h)
277 {
279 buf[
h * linesize + 0] +=
color[0];
280 buf[
h * linesize + 1] +=
color[1];
281 buf[
h * linesize + 2] +=
color[2];
282 buf[
h * linesize + 3] +=
color[3];
283 if (*prev_y &&
h != *prev_y) {
284 int start = *prev_y;
285 uint8_t *bufk;
287 if (start > end)
288 FFSWAP(int16_t, start, end);
289 bufk = buf + (start + 1) * linesize;
290 for (int k = start + 1; k < end; k++, bufk += linesize) {
295 }
296 }
297 }
299 }
300
302 int16_t *prev_y,
303 const uint8_t
color[4],
int h)
304 {
308 if (*prev_y &&
h != *prev_y) {
309 int start = *prev_y;
310 uint8_t *bufk;
312 if (start > end)
313 FFSWAP(int16_t, start, end);
314 bufk = buf + (start + 1) * linesize;
315 for (int k = start + 1; k < end; k++, bufk += linesize)
317 }
318 }
320 }
321
323 int16_t *prev_y,
324 const uint8_t
color[4],
int h)
325 {
326 const int start = (
height -
h) / 2;
327 const int end = start +
h;
328 uint8_t *bufk = buf + start * linesize;
329 for (int k = start; k < end; k++, bufk += linesize) {
334 }
335 }
336
338 int16_t *prev_y,
339 const uint8_t
color[4],
int h)
340 {
342 const int start = (
height -
h) / 2;
343 const int end = start +
h;
344 uint8_t *bufk = buf + start * linesize;
345 for (int k = start; k < end; k++, bufk += linesize)
347 }
348
350 int16_t *prev_y,
351 const uint8_t
color[4],
int h)
352 {
354 buf[
h * linesize] +=
color[0];
355 }
356
358 int16_t *prev_y,
359 const uint8_t
color[4],
int h)
360 {
361 int k;
364 if (start > end)
365 FFSWAP(int16_t, start, end);
366 for (k = start; k < end; k++)
367 buf[k * linesize] +=
color[0];
368 }
369
371 int16_t *prev_y,
372 const uint8_t
color[4],
int h)
373 {
374 int k;
376 buf[
h * linesize] +=
color[0];
377 if (*prev_y &&
h != *prev_y) {
378 int start = *prev_y;
380 if (start > end)
381 FFSWAP(int16_t, start, end);
382 for (k = start + 1; k < end; k++)
383 buf[k * linesize] +=
color[0];
384 }
385 }
387 }
388
390 int16_t *prev_y,
391 const uint8_t
color[4],
int h)
392 {
393 int k;
394 const int start = (
height -
h) / 2;
395 const int end = start +
h;
396 for (k = start; k < end; k++)
397 buf[k * linesize] +=
color[0];
398 }
399
401 {
406 int nb_channels =
inlink->ch_layout.nb_channels;
407 char *colors, *saveptr =
NULL;
408 uint8_t x;
409 int ch;
410
413
417 } else {
418 if (!showwaves->
n.
num || !showwaves->
n.
den) {
422 } else {
424 }
425 }
426
431 }
432
434 showwaves->
n.
num, showwaves->
n.
den);
441
443 outlink->
w = showwaves->
w;
444 outlink->
h = showwaves->
h;
446
449
450 switch (outlink->
format) {
452 switch (showwaves->
mode) {
457 default:
459 }
461 break;
463 switch (showwaves->
mode) {
468 default:
470 }
472 break;
473 }
474
475 switch (showwaves->
scale) {
477 switch (showwaves->
mode) {
482 default:
484 }
485 break;
487 switch (showwaves->
mode) {
492 default:
494 }
495 break;
497 switch (showwaves->
mode) {
502 default:
504 }
505 break;
507 switch (showwaves->
mode) {
512 default:
514 }
515 break;
516 }
517
521
523 if (!colors)
525
527 /* multiplication factor, pre-computed to avoid in-loop divisions */
529 } else {
530 x = 255;
531 }
533 uint8_t fg[4] = { 0xff, 0xff, 0xff, 0xff };
534
535 for (ch = 0; ch < nb_channels; ch++) {
537
541 showwaves->
fg[4*ch + 0] = fg[0] * x / 255.;
542 showwaves->
fg[4*ch + 1] = fg[1] * x / 255.;
543 showwaves->
fg[4*ch + 2] = fg[2] * x / 255.;
544 showwaves->
fg[4*ch + 3] = fg[3] * x / 255.;
545 }
546 } else {
547 for (ch = 0; ch < nb_channels; ch++)
548 showwaves->
fg[4 * ch + 0] = x;
549 }
551
552 return 0;
553 }
554
556 {
560 int nb_channels =
inlink->ch_layout.nb_channels;
562
567
571 for (
int i = 0;
i < nb_channels;
i++)
574 }
575
577 {
583 int64_t last_column_samples = column_max_samples + remaining_samples;
586 const int nb_channels =
inlink->ch_layout.nb_channels;
587 const int ch_height = showwaves->
split_channels ? outlink->
h / nb_channels : outlink->
h;
588 const int linesize =
out->linesize[0];
589 const int pixstep = showwaves->
pixstep;
590 int col = 0;
592
593 if (column_max_samples == 0) {
596 }
597
598 av_log(
ctx,
AV_LOG_DEBUG,
"Create frame averaging %"PRId64
" samples per column\n", column_max_samples);
599
600 memset(sum, 0, nb_channels * sizeof(*sum));
601
605 const int16_t *
p = (
const int16_t *)
frame->data[0];
606
608 int64_t max_samples = col == outlink->
w - 1 ? last_column_samples: column_max_samples;
609 int ch;
610
613 for (ch = 0; ch < nb_channels; ch++)
614 sum[ch] +=
abs(
p[ch +
i*nb_channels]);
615 break;
617 for (ch = 0; ch < nb_channels; ch++)
618 sum[ch] =
FFMAX(sum[ch],
abs(
p[ch +
i*nb_channels]));
619 break;
620 }
621
622 n++;
623 if (n == max_samples) {
624 for (ch = 0; ch < nb_channels; ch++) {
626 uint8_t *buf =
out->data[0] + col * pixstep;
628
630 buf += ch*ch_height*linesize;
633 showwaves->
draw_sample(buf, ch_height, linesize, &showwaves->
buf_idy[ch], &showwaves->
fg[ch * 4],
h);
634 sum[ch] = 0;
635 }
636 col++;
637 n = 0;
638 }
639 }
640 }
641
643 }
644
645
647 {
651
655 }
656
658 }
659
662 {
668 out->width = outlink->
w;
669 out->height = outlink->
h;
670 for (
int j = 0; j < outlink->
h; j++)
671 memset(
out->data[0] + j*
out->linesize[0], 0, outlink->
w * showwaves->
pixstep);
672 }
673 return 0;
674 }
675
677 {
679
680 if (!strcmp(
ctx->filter->name,
"showwavespic")) {
683 }
684
685 return 0;
686 }
687
688 #if CONFIG_SHOWWAVES_FILTER
689
691 {
697 const int16_t *
p = (
const int16_t *)insamples->
data[0];
698 int16_t *history = showwaves->
history;
699 const int nb_channels =
inlink->ch_layout.nb_channels;
700 int i, j,
ret = 0, linesize;
701 const int pixstep = showwaves->
pixstep;
702 const int ch_height = showwaves->
split_channels ? outlink->
h / nb_channels : outlink->
h;
708 int16_t *buf_idy = showwaves->
buf_idy;
710 int buf_idx = showwaves->
buf_idx;
711 const uint8_t *fg = showwaves->
fg;
712 const int w = showwaves->
w;
714
715 for (int n = 0; n < nb_samples * nb_channels; n++) {
716 history[idx++] =
p[n];
717 if (idx >= history_nb_samples)
718 idx = 0;
719 }
721
724 goto end;
727
728 /* draw data in the buffer */
730 for (
i = 0;
i < history_nb_samples;
i++) {
731 for (j = 0; j < nb_channels; j++) {
732 uint8_t *buf =
dst + buf_idx * pixstep;
734
735 if (split_channels)
736 buf += j*ch_height*linesize;
737 h = showwaves->
get_h(history[idx++], ch_height);
738 if (idx >= history_nb_samples)
739 idx = 0;
741 &buf_idy[j], &fg[j * 4],
h);
742 }
743
747 buf_idx++;
748 }
750 break;
751 }
752
754
756 goto end;
758 end:
761 }
762
764 {
770 int nb_samples;
772
774
776 nb_samples = (q.
num + (q.
den / 2)) / q.
den;
782 return showwaves_filter_frame(
inlink, in);
783 }
784
787
789 }
790
792 {
796 },
797 };
798
800 .
p.
name =
"showwaves",
802 .p.priv_class = &showwaves_class,
810 };
811
812 #endif // CONFIG_SHOWWAVES_FILTER
813
814 #if CONFIG_SHOWWAVESPIC_FILTER
815
816 #define OFFSET(x) offsetof(ShowWavesContext, x)
817 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
818
819 static const AVOption showwavespic_options[] = {
823 {
"colors",
"set channels colors",
OFFSET(colors),
AV_OPT_TYPE_STRING, {.str =
"red|green|blue|yellow|orange|lime|pink|magenta|brown" }, 0, 0,
FLAGS },
836 };
837
839
841 {
844
849 }
850
851 return 0;
852 }
853
855 {
860
863
866 goto end;
867
868 /* queue the audio frame */
872 goto end;
873 }
874 f->frame = insamples;
879 } else {
882 }
884
885 return 0;
886 }
887
888 end:
891 }
892
894 {
897 .config_props = showwavespic_config_input,
898 .filter_frame = showwavespic_filter_frame,
899 },
900 };
901
902 static const AVFilterPad showwavespic_outputs[] = {
903 {
908 },
909 };
910
912 .
p.
name =
"showwavespic",
914 .p.priv_class = &showwavespic_class,
921 };
922
923 #endif // CONFIG_SHOWWAVESPIC_FILTER
#define FF_ALLOCZ_TYPED_ARRAY(p, nelem)
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
AVPixelFormat
Pixel format.
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
static int get_cbrt_h(int16_t sample, int height)
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
static enum AVSampleFormat sample_fmts[]
int64_t duration
Duration of the frame, in the same units as pts.
#define AVERROR_EOF
End of file.
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.
@ AV_OPT_TYPE_VIDEO_RATE
Underlying C type is AVRational.
AVRational av_div_q(AVRational b, AVRational c)
Divide one rational by another.
int(* get_h)(int16_t sample, int height)
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
static void draw_sample_point_rgba_scale(uint8_t *buf, int height, int linesize, int16_t *prev_y, const uint8_t color[4], int h)
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
#define FILTER_INPUTS(array)
This structure describes decoded (raw) audio or video data.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
#define AV_LOG_VERBOSE
Detailed information.
static int get_cbrt_h2(int16_t sample, int height)
static const AVOption showwaves_options[]
AVFILTER_DEFINE_CLASS(showwaves)
const char * name
Filter name.
AVRational av_sub_q(AVRational b, AVRational c)
Subtract one rational from another.
@ AV_OPT_TYPE_RATIONAL
Underlying C type is AVRational.
static int get_sqrt_h2(int16_t sample, int height)
const FFFilter ff_avf_showwaves
A link between two filters.
#define FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink)
Forward the status on an output link to an input link.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
static void draw_sample_line_rgba_scale(uint8_t *buf, int height, int linesize, int16_t *prev_y, const uint8_t color[4], int h)
Link properties exposed to filter code, but not external callers.
static void draw_sample_cline_rgba_full(uint8_t *buf, int height, int linesize, int16_t *prev_y, const uint8_t color[4], int h)
static int get_log_h2(int16_t sample, int height)
void * priv
private data for use by the filter
static int get_sqrt_h(int16_t sample, int height)
static int get_lin_h2(int16_t sample, int height)
static int get_lin_h(int16_t sample, int height)
A filter pad used for either input or output.
static void draw_sample_point_gray(uint8_t *buf, int height, int linesize, int16_t *prev_y, const uint8_t color[4], int h)
static void draw_sample_p2p_gray(uint8_t *buf, int height, int linesize, int16_t *prev_y, const uint8_t color[4], int h)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
struct frame_node * last_frame
static double av_q2d(AVRational a)
Convert an AVRational to a double.
struct frame_node * audio_frames
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().
AVRational sample_aspect_ratio
agreed upon sample aspect ratio
#define av_assert0(cond)
assert() equivalent, that is always enabled.
static enum AVPixelFormat pix_fmts[]
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
static av_cold int init(AVFilterContext *ctx)
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
static int push_frame(AVFilterLink *outlink, int i, int64_t pts)
#define FILTER_OUTPUTS(array)
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
static int config_output(AVFilterLink *outlink)
Describe the class of an AVClass context structure.
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.
Rational number (pair of numerator and denominator).
static int get_log_h(int16_t sample, int height)
filter_frame For filters that do not use the activate() callback
@ AV_OPT_TYPE_IMAGE_SIZE
Underlying C type is two consecutive integers.
AVFilterLink ** inputs
array of pointers to input links
static av_cold void uninit(AVFilterContext *ctx)
const AVFilterPad ff_audio_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_AUDIO.
static int request_frame(AVFilterLink *outlink)
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
int draw_mode
ShowWavesDrawMode.
static FilterLink * ff_filter_link(AVFilterLink *link)
static void draw_sample_cline_gray(uint8_t *buf, int height, int linesize, int16_t *prev_y, const uint8_t color[4], int h)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
static void draw_sample_p2p_rgba_full(uint8_t *buf, int height, int linesize, int16_t *prev_y, const uint8_t color[4], int h)
int format
agreed upon media format
static const uint32_t color[16+AV_CLASS_CATEGORY_NB]
static AVRational av_make_q(int num, int den)
Create an AVRational.
AVFilterContext * src
source filter
FF_FILTER_FORWARD_WANTED(outlink, inlink)
static int push_single_pic(AVFilterLink *outlink)
int nb_samples
number of audio samples (per channel) described by this frame
#define i(width, name, range_min, range_max)
int w
agreed upon image width
const FFFilter ff_avf_showwavespic
#define av_malloc_array(a, b)
AVSampleFormat
Audio sample formats.
static void draw_sample_line_gray(uint8_t *buf, int height, int linesize, int16_t *prev_y, const uint8_t color[4], int h)
#define FILTER_QUERY_FUNC2(func)
@ AV_SAMPLE_FMT_S16
signed 16 bits
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
static void draw_sample_p2p_rgba_scale(uint8_t *buf, int height, int linesize, int16_t *prev_y, const uint8_t color[4], int h)
const char * name
Pad name.
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
void * av_calloc(size_t nmemb, size_t size)
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
static int av_cmp_q(AVRational a, AVRational b)
Compare two rationals.
void(* draw_sample)(uint8_t *buf, int height, int linesize, int16_t *prev_y, const uint8_t color[4], int h)
#define FFSWAP(type, a, b)
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
int h
agreed upon image height
@ AV_OPT_TYPE_INT
Underlying C type is int.
static void draw_sample_cline_rgba_scale(uint8_t *buf, int height, int linesize, int16_t *prev_y, const uint8_t color[4], int h)
static int alloc_out_frame(ShowWavesContext *showwaves, AVFilterLink *outlink)
AVRational time_base
Define the time base used by the PTS of the frames/samples which will pass through this link.
IDirect3DDxgiInterfaceAccess _COM_Outptr_ void ** p
AVRational av_mul_q(AVRational b, AVRational c)
Multiply two rationals.
char * av_strdup(const char *s)
Duplicate a string.
AVFilter p
The public AVFilter.
static void scale(int *out, const int *in, const int w, const int h, const int shift)
AVRational av_add_q(AVRational b, AVRational c)
Add two rationals.
FF_FILTER_FORWARD_STATUS(inlink, outlink)
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
AVRational frame_rate
Frame rate of the stream on the link, or 1/0 if unknown or variable.
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
static void draw_sample_line_rgba_full(uint8_t *buf, int height, int linesize, int16_t *prev_y, const uint8_t color[4], int h)
static void draw_sample_point_rgba_full(uint8_t *buf, int height, int linesize, int16_t *prev_y, const uint8_t color[4], int h)
Generated on Fri Oct 17 2025 19:22:55 for FFmpeg by
doxygen
1.8.17