MODE_POINT,
MODE_LINE,
MODE_P2P,
+ MODE_CENTERED_LINE,
MODE_NB,
};
int n;
int sample_count_mod;
enum ShowWavesMode mode;
+ int split_channels;
+ void (*draw_sample)(uint8_t *buf, int height, int linesize,
+ int16_t sample, int16_t *prev_y, int intensity);
} ShowWavesContext;
#define OFFSET(x) offsetof(ShowWavesContext, x)
{ "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "600x240"}, 0, 0, FLAGS },
{ "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "600x240"}, 0, 0, FLAGS },
{ "mode", "select display mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=MODE_POINT}, 0, MODE_NB-1, FLAGS, "mode"},
- { "point", "draw a point for each sample", 0, AV_OPT_TYPE_CONST, {.i64=MODE_POINT}, .flags=FLAGS, .unit="mode"},
- { "line", "draw a line for each sample", 0, AV_OPT_TYPE_CONST, {.i64=MODE_LINE}, .flags=FLAGS, .unit="mode"},
- { "p2p", "draw a line between samples", 0, AV_OPT_TYPE_CONST, {.i64=MODE_P2P}, .flags=FLAGS, .unit="mode"},
+ { "point", "draw a point for each sample", 0, AV_OPT_TYPE_CONST, {.i64=MODE_POINT}, .flags=FLAGS, .unit="mode"},
+ { "line", "draw a line for each sample", 0, AV_OPT_TYPE_CONST, {.i64=MODE_LINE}, .flags=FLAGS, .unit="mode"},
+ { "p2p", "draw a line between samples", 0, AV_OPT_TYPE_CONST, {.i64=MODE_P2P}, .flags=FLAGS, .unit="mode"},
+ { "cline", "draw a centered line for each sample", 0, AV_OPT_TYPE_CONST, {.i64=MODE_CENTERED_LINE}, .flags=FLAGS, .unit="mode"},
{ "n", "set how many samples to show in the same point", OFFSET(n), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS },
{ "rate", "set video rate", OFFSET(rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, FLAGS },
{ "r", "set video rate", OFFSET(rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, FLAGS },
+ { "split_channels", "draw channels separately", OFFSET(split_channels), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, FLAGS },
{ NULL }
};
#define MAX_INT16 ((1<<15) -1)
+static void draw_sample_point(uint8_t *buf, int height, int linesize,
+ int16_t sample, int16_t *prev_y, int intensity)
+{
+ const int h = height/2 - av_rescale(sample, height/2, MAX_INT16);
+ if (h >= 0 && h < height)
+ buf[h * linesize] += intensity;
+}
+
+static void draw_sample_line(uint8_t *buf, int height, int linesize,
+ int16_t sample, int16_t *prev_y, int intensity)
+{
+ int k;
+ const int h = height/2 - av_rescale(sample, height/2, MAX_INT16);
+ int start = height/2;
+ int end = av_clip(h, 0, height-1);
+ if (start > end)
+ FFSWAP(int16_t, start, end);
+ for (k = start; k < end; k++)
+ buf[k * linesize] += intensity;
+}
+
+static void draw_sample_p2p(uint8_t *buf, int height, int linesize,
+ int16_t sample, int16_t *prev_y, int intensity)
+{
+ int k;
+ const int h = height/2 - av_rescale(sample, height/2, MAX_INT16);
+ if (h >= 0 && h < height) {
+ buf[h * linesize] += intensity;
+ if (*prev_y && h != *prev_y) {
+ int start = *prev_y;
+ int end = av_clip(h, 0, height-1);
+ if (start > end)
+ FFSWAP(int16_t, start, end);
+ for (k = start + 1; k < end; k++)
+ buf[k * linesize] += intensity;
+ }
+ }
+ *prev_y = h;
+}
+
+static void draw_sample_cline(uint8_t *buf, int height, int linesize,
+ int16_t sample, int16_t *prev_y, int intensity)
+{
+ int k;
+ const int h = av_rescale(abs(sample), height, UINT16_MAX);
+ const int start = (height - h) / 2;
+ const int end = start + h;
+ for (k = start; k < end; k++)
+ buf[k * linesize] += intensity;
+}
+
static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
{
AVFilterContext *ctx = inlink->dst;
int linesize = outpicref ? outpicref->linesize[0] : 0;
int16_t *p = (int16_t *)insamples->data[0];
int nb_channels = inlink->channels;
- int i, j, k, h, ret = 0;
+ int i, j, ret = 0;
const int n = showwaves->n;
- const int x = 255 / (nb_channels * n); /* multiplication factor, pre-computed to avoid in-loop divisions */
+ const int x = 255 / ((showwaves->split_channels ? 1 : nb_channels) * n); /* multiplication factor, pre-computed to avoid in-loop divisions */
+ const int ch_height = showwaves->split_channels ? outlink->h / nb_channels : outlink->h;
/* draw data in the buffer */
for (i = 0; i < nb_samples; i++) {
memset(outpicref->data[0] + j * linesize, 0, outlink->w);
}
for (j = 0; j < nb_channels; j++) {
- h = showwaves->h/2 - av_rescale(*p++, showwaves->h/2, MAX_INT16);
- switch (showwaves->mode) {
- case MODE_POINT:
- if (h >= 0 && h < outlink->h)
- *(outpicref->data[0] + showwaves->buf_idx + h * linesize) += x;
- break;
-
- case MODE_LINE:
- {
- int start = showwaves->h/2, end = av_clip(h, 0, outlink->h-1);
- if (start > end) FFSWAP(int16_t, start, end);
- for (k = start; k < end; k++)
- *(outpicref->data[0] + showwaves->buf_idx + k * linesize) += x;
- break;
- }
- case MODE_P2P:
- if (h >= 0 && h < outlink->h) {
- *(outpicref->data[0] + showwaves->buf_idx + h * linesize) += x;
- if (showwaves->buf_idy[j] && h != showwaves->buf_idy[j]) {
- int start = showwaves->buf_idy[j], end = av_clip(h, 0, outlink->h-1);
- if (start > end)
- FFSWAP(int16_t, start, end);
- for (k = start + 1; k < end; k++)
- *(outpicref->data[0] + showwaves->buf_idx + k * linesize) += x;
- }
- }
- break;
- }
- /* store current y coordinate for this channel */
- showwaves->buf_idy[j] = h;
+ uint8_t *buf = outpicref->data[0] + showwaves->buf_idx;
+ if (showwaves->split_channels)
+ buf += j*ch_height*linesize;
+ showwaves->draw_sample(buf, ch_height, linesize, *p++,
+ &showwaves->buf_idy[j], x);
}
showwaves->sample_count_mod++;
return ret;
}
+static av_cold int init(AVFilterContext *ctx)
+{
+ ShowWavesContext *showwaves = ctx->priv;
+
+ switch (showwaves->mode) {
+ case MODE_POINT: showwaves->draw_sample = draw_sample_point; break;
+ case MODE_LINE: showwaves->draw_sample = draw_sample_line; break;
+ case MODE_P2P: showwaves->draw_sample = draw_sample_p2p; break;
+ case MODE_CENTERED_LINE: showwaves->draw_sample = draw_sample_cline; break;
+ default:
+ return AVERROR_BUG;
+ }
+ return 0;
+}
+
static const AVFilterPad showwaves_inputs[] = {
{
.name = "default",
AVFilter ff_avf_showwaves = {
.name = "showwaves",
.description = NULL_IF_CONFIG_SMALL("Convert input audio to a video output."),
+ .init = init,
.uninit = uninit,
.query_formats = query_formats,
.priv_size = sizeof(ShowWavesContext),