[FFmpeg-cvslog] avfilter/avf_showwaves: color support
Paul B Mahol
git at videolan.org
Thu Jan 21 20:49:06 CET 2016
ffmpeg | branch: master | Paul B Mahol <onemda at gmail.com> | Tue Jan 19 22:09:55 2016 +0100| [6ed3a10f644b7e96ec2bece7ab6c98629fdd5ff9] | committer: Paul B Mahol
avfilter/avf_showwaves: color support
Signed-off-by: Paul B Mahol <onemda at gmail.com>
> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=6ed3a10f644b7e96ec2bece7ab6c98629fdd5ff9
---
libavfilter/avf_showwaves.c | 275 ++++++++++++++++++++++++++++++++-----------
1 file changed, 207 insertions(+), 68 deletions(-)
diff --git a/libavfilter/avf_showwaves.c b/libavfilter/avf_showwaves.c
index 4c46d8e..6bb9ea3 100644
--- a/libavfilter/avf_showwaves.c
+++ b/libavfilter/avf_showwaves.c
@@ -24,6 +24,7 @@
*/
#include "libavutil/avassert.h"
+#include "libavutil/avstring.h"
#include "libavutil/channel_layout.h"
#include "libavutil/opt.h"
#include "libavutil/parseutils.h"
@@ -50,15 +51,18 @@ typedef struct {
const AVClass *class;
int w, h;
AVRational rate;
+ char *colors;
int buf_idx;
int16_t *buf_idy; /* y coordinate of previous sample for each channel */
AVFrame *outpicref;
int n;
+ int pixstep;
int sample_count_mod;
int mode; ///< ShowWavesMode
int split_channels;
+ uint8_t *fg;
void (*draw_sample)(uint8_t *buf, int height, int linesize,
- int16_t sample, int16_t *prev_y, int intensity);
+ int16_t sample, int16_t *prev_y, const uint8_t color[4]);
/* single picture */
int single_pic;
@@ -83,6 +87,7 @@ static const AVOption showwaves_options[] = {
{ "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_BOOL, {.i64 = 0}, 0, 1, FLAGS },
+ { "colors", "set channels colors", OFFSET(colors), AV_OPT_TYPE_STRING, {.str = "red|green|blue|yellow|orange|lime|pink|magenta|brown" }, 0, 0, FLAGS },
{ NULL }
};
@@ -94,6 +99,7 @@ static av_cold void uninit(AVFilterContext *ctx)
av_frame_free(&showwaves->outpicref);
av_freep(&showwaves->buf_idy);
+ av_freep(&showwaves->fg);
if (showwaves->single_pic) {
struct frame_node *node = showwaves->audio_frames;
@@ -116,7 +122,7 @@ static int query_formats(AVFilterContext *ctx)
AVFilterLink *inlink = ctx->inputs[0];
AVFilterLink *outlink = ctx->outputs[0];
static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE };
- static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE };
+ static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGBA, AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE };
int ret;
/* set input audio formats */
@@ -140,12 +146,147 @@ static int query_formats(AVFilterContext *ctx)
return 0;
}
+static void draw_sample_point_rgba(uint8_t *buf, int height, int linesize,
+ int16_t sample, int16_t *prev_y,
+ const uint8_t color[4])
+{
+ const int h = height/2 - av_rescale(sample, height/2, INT16_MAX);
+ if (h >= 0 && h < height) {
+ buf[h * linesize + 0] += color[0];
+ buf[h * linesize + 1] += color[1];
+ buf[h * linesize + 2] += color[2];
+ buf[h * linesize + 3] += color[3];
+ }
+}
+
+static void draw_sample_line_rgba(uint8_t *buf, int height, int linesize,
+ int16_t sample, int16_t *prev_y,
+ const uint8_t color[4])
+{
+ int k;
+ const int h = height/2 - av_rescale(sample, height/2, INT16_MAX);
+ 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 + 0] += color[0];
+ buf[k * linesize + 1] += color[1];
+ buf[k * linesize + 2] += color[2];
+ buf[k * linesize + 3] += color[3];
+ }
+}
+
+static void draw_sample_p2p_rgba(uint8_t *buf, int height, int linesize,
+ int16_t sample, int16_t *prev_y,
+ const uint8_t color[4])
+{
+ int k;
+ const int h = height/2 - av_rescale(sample, height/2, INT16_MAX);
+ if (h >= 0 && h < height) {
+ buf[h * linesize + 0] += color[0];
+ buf[h * linesize + 1] += color[1];
+ buf[h * linesize + 2] += color[2];
+ buf[h * linesize + 3] += color[3];
+ 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 + 0] += color[0];
+ buf[k * linesize + 1] += color[1];
+ buf[k * linesize + 2] += color[2];
+ buf[k * linesize + 3] += color[3];
+ }
+ }
+ }
+ *prev_y = h;
+}
+
+static void draw_sample_cline_rgba(uint8_t *buf, int height, int linesize,
+ int16_t sample, int16_t *prev_y,
+ const uint8_t color[4])
+{
+ int k;
+ const int h = av_rescale(abs(sample), height, INT16_MAX);
+ const int start = (height - h) / 2;
+ const int end = start + h;
+ for (k = start; k < end; k++) {
+ buf[k * linesize + 0] += color[0];
+ buf[k * linesize + 1] += color[1];
+ buf[k * linesize + 2] += color[2];
+ buf[k * linesize + 3] += color[3];
+ }
+}
+
+static void draw_sample_point_gray(uint8_t *buf, int height, int linesize,
+ int16_t sample, int16_t *prev_y,
+ const uint8_t color[4])
+{
+ const int h = height/2 - av_rescale(sample, height/2, INT16_MAX);
+ if (h >= 0 && h < height)
+ buf[h * linesize] += color[0];
+}
+
+static void draw_sample_line_gray(uint8_t *buf, int height, int linesize,
+ int16_t sample, int16_t *prev_y,
+ const uint8_t color[4])
+{
+ int k;
+ const int h = height/2 - av_rescale(sample, height/2, INT16_MAX);
+ 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] += color[0];
+}
+
+static void draw_sample_p2p_gray(uint8_t *buf, int height, int linesize,
+ int16_t sample, int16_t *prev_y,
+ const uint8_t color[4])
+{
+ int k;
+ const int h = height/2 - av_rescale(sample, height/2, INT16_MAX);
+ if (h >= 0 && h < height) {
+ buf[h * linesize] += color[0];
+ 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] += color[0];
+ }
+ }
+ *prev_y = h;
+}
+
+static void draw_sample_cline_gray(uint8_t *buf, int height, int linesize,
+ int16_t sample, int16_t *prev_y,
+ const uint8_t color[4])
+{
+ int k;
+ const int h = av_rescale(abs(sample), height, INT16_MAX);
+ const int start = (height - h) / 2;
+ const int end = start + h;
+ for (k = start; k < end; k++)
+ buf[k * linesize] += color[0];
+}
+
static int config_output(AVFilterLink *outlink)
{
AVFilterContext *ctx = outlink->src;
AVFilterLink *inlink = ctx->inputs[0];
ShowWavesContext *showwaves = ctx->priv;
int nb_channels = inlink->channels;
+ char *colors, *saveptr = NULL;
+ uint8_t x;
+ int ch;
+
+ if (showwaves->single_pic)
+ showwaves->n = 1;
if (!showwaves->n)
showwaves->n = FFMAX(1, ((double)inlink->sample_rate / (showwaves->w * av_q2d(showwaves->rate))) + 0.5);
@@ -164,6 +305,62 @@ static int config_output(AVFilterLink *outlink)
av_log(ctx, AV_LOG_VERBOSE, "s:%dx%d r:%f n:%d\n",
showwaves->w, showwaves->h, av_q2d(outlink->frame_rate), showwaves->n);
+
+ switch (outlink->format) {
+ case AV_PIX_FMT_GRAY8:
+ switch (showwaves->mode) {
+ case MODE_POINT: showwaves->draw_sample = draw_sample_point_gray; break;
+ case MODE_LINE: showwaves->draw_sample = draw_sample_line_gray; break;
+ case MODE_P2P: showwaves->draw_sample = draw_sample_p2p_gray; break;
+ case MODE_CENTERED_LINE: showwaves->draw_sample = draw_sample_cline_gray; break;
+ default:
+ return AVERROR_BUG;
+ }
+ showwaves->pixstep = 1;
+ break;
+ case AV_PIX_FMT_RGBA:
+ switch (showwaves->mode) {
+ case MODE_POINT: showwaves->draw_sample = draw_sample_point_rgba; break;
+ case MODE_LINE: showwaves->draw_sample = draw_sample_line_rgba; break;
+ case MODE_P2P: showwaves->draw_sample = draw_sample_p2p_rgba; break;
+ case MODE_CENTERED_LINE: showwaves->draw_sample = draw_sample_cline_rgba; break;
+ default:
+ return AVERROR_BUG;
+ }
+ showwaves->pixstep = 4;
+ break;
+ }
+
+ showwaves->fg = av_malloc_array(nb_channels, 4 * sizeof(*showwaves->fg));
+ if (!showwaves->fg)
+ return AVERROR(ENOMEM);
+
+ colors = av_strdup(showwaves->colors);
+ if (!colors)
+ return AVERROR(ENOMEM);
+
+ /* multiplication factor, pre-computed to avoid in-loop divisions */
+ x = 255 / ((showwaves->split_channels ? 1 : nb_channels) * showwaves->n);
+ if (outlink->format == AV_PIX_FMT_RGBA) {
+ uint8_t fg[4] = { 0xff, 0xff, 0xff, 0xff };
+
+ for (ch = 0; ch < nb_channels; ch++) {
+ char *color;
+
+ color = av_strtok(ch == 0 ? colors : NULL, " |", &saveptr);
+ if (color)
+ av_parse_color(fg, color, -1, ctx);
+ showwaves->fg[4*ch + 0] = fg[0] * x / 255.;
+ showwaves->fg[4*ch + 1] = fg[1] * x / 255.;
+ showwaves->fg[4*ch + 2] = fg[2] * x / 255.;
+ showwaves->fg[4*ch + 3] = fg[3] * x / 255.;
+ }
+ } else {
+ for (ch = 0; ch < nb_channels; ch++)
+ showwaves->fg[4 * ch + 0] = x;
+ }
+ av_free(colors);
+
return 0;
}
@@ -192,9 +389,9 @@ static int push_single_pic(AVFilterLink *outlink)
AVFrame *out = showwaves->outpicref;
struct frame_node *node;
const int nb_channels = inlink->channels;
- const int x = 255 / (showwaves->split_channels ? 1 : nb_channels);
const int ch_height = showwaves->split_channels ? outlink->h / nb_channels : outlink->h;
const int linesize = out->linesize[0];
+ const int pixstep = showwaves->pixstep;
int col = 0;
int64_t *sum = showwaves->sum;
@@ -220,11 +417,11 @@ static int push_single_pic(AVFilterLink *outlink)
if (n++ == max_samples) {
for (ch = 0; ch < nb_channels; ch++) {
int16_t sample = sum[ch] / max_samples;
- uint8_t *buf = out->data[0] + col;
+ uint8_t *buf = out->data[0] + col * pixstep;
if (showwaves->split_channels)
buf += ch*ch_height*linesize;
av_assert0(col < outlink->w);
- showwaves->draw_sample(buf, ch_height, linesize, sample, &showwaves->buf_idy[ch], x);
+ showwaves->draw_sample(buf, ch_height, linesize, sample, &showwaves->buf_idy[ch], &showwaves->fg[ch * 4]);
sum[ch] = 0;
}
col++;
@@ -254,57 +451,6 @@ static int request_frame(AVFilterLink *outlink)
return ret;
}
-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, INT16_MAX);
- 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, INT16_MAX);
- 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, INT16_MAX);
- 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, INT16_MAX);
- const int start = (height - h) / 2;
- const int end = start + h;
- for (k = start; k < end; k++)
- buf[k * linesize] += intensity;
-}
-
static int alloc_out_frame(ShowWavesContext *showwaves, const int16_t *p,
const AVFilterLink *inlink, AVFilterLink *outlink,
const AVFrame *in)
@@ -321,7 +467,7 @@ static int alloc_out_frame(ShowWavesContext *showwaves, const int16_t *p,
av_make_q(1, inlink->sample_rate),
outlink->time_base);
for (j = 0; j < outlink->h; j++)
- memset(out->data[0] + j*out->linesize[0], 0, outlink->w);
+ memset(out->data[0] + j*out->linesize[0], 0, outlink->w * showwaves->pixstep);
}
return 0;
}
@@ -335,14 +481,6 @@ static av_cold int init(AVFilterContext *ctx)
showwaves->mode = MODE_CENTERED_LINE;
}
- 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;
}
@@ -358,8 +496,8 @@ static int showwaves_filter_frame(AVFilterLink *inlink, AVFrame *insamples)
int16_t *p = (int16_t *)insamples->data[0];
int nb_channels = inlink->channels;
int i, j, ret = 0;
+ const int pixstep = showwaves->pixstep;
const int n = showwaves->n;
- 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 */
@@ -371,12 +509,12 @@ static int showwaves_filter_frame(AVFilterLink *inlink, AVFrame *insamples)
outpicref = showwaves->outpicref;
for (j = 0; j < nb_channels; j++) {
- uint8_t *buf = outpicref->data[0] + showwaves->buf_idx;
+ uint8_t *buf = outpicref->data[0] + showwaves->buf_idx * pixstep;
const int linesize = outpicref->linesize[0];
if (showwaves->split_channels)
buf += j*ch_height*linesize;
showwaves->draw_sample(buf, ch_height, linesize, *p++,
- &showwaves->buf_idy[j], x);
+ &showwaves->buf_idy[j], &showwaves->fg[j * 4]);
}
showwaves->sample_count_mod++;
@@ -437,6 +575,7 @@ static const AVOption showwavespic_options[] = {
{ "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 },
{ "split_channels", "draw channels separately", OFFSET(split_channels), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS },
+ { "colors", "set channels colors", OFFSET(colors), AV_OPT_TYPE_STRING, {.str = "red|green|blue|yellow|orange|lime|pink|magenta|brown" }, 0, 0, FLAGS },
{ NULL }
};
More information about the ffmpeg-cvslog
mailing list