[FFmpeg-devel] [PATCH] smptebars filter

Paul B Mahol onemda at gmail.com
Wed Jun 20 18:59:26 CEST 2012


On 6/20/12, Stefano Sabatini <stefasab at gmail.com> wrote:
> On date Wednesday 2012-06-20 01:54:58 +0000, Paul B Mahol encoded:
>> Signed-off-by: Paul B Mahol <onemda at gmail.com>
>> ---
>>  doc/filters.texi             |   20 ++++
>>  libavfilter/Makefile         |    1 +
>>  libavfilter/allfilters.c     |    1 +
>>  libavfilter/vsrc_smptebars.c |  215
>> ++++++++++++++++++++++++++++++++++++++++++
>>  4 files changed, 237 insertions(+), 0 deletions(-)
>>  create mode 100644 libavfilter/vsrc_smptebars.c
>>
>> diff --git a/doc/filters.texi b/doc/filters.texi
>> index 566fbfc..0c0c490 100644
>> --- a/doc/filters.texi
>> +++ b/doc/filters.texi
>> @@ -3799,6 +3799,26 @@ the @code{mp=geq} filter:
>>  nullsrc=s=256x256, mp=geq=random(1)*255:128:128
>>  @end example
>>
>> + at section smptebars
>> +
>> +Generate SMPTE color bars.
>> +
>> +This source accepts a list of options in the form of
>> + at var{key}=@var{value} pairs separated by ":". A description of the
>> +accepted options follows.
>> +
>> + at table @option
>> + at item size, s
>> +Set the size of the output video.
>> +Default is 720x480.
>> +
>> + at item rate, r
>> +Set the video rate, that is the number of frames generated per second.
>> +Default is 30.
>> +
>> + at item sar
>> +Set the sample aspect ratio of the sourced video.
>> +
>>  @c man end VIDEO SOURCES
>>
>>  @chapter Video Sinks
>> diff --git a/libavfilter/Makefile b/libavfilter/Makefile
>> index 72bb640..193bfa1 100644
>> --- a/libavfilter/Makefile
>> +++ b/libavfilter/Makefile
>> @@ -136,6 +136,7 @@ OBJS-$(CONFIG_MOVIE_FILTER)                  +=
>> src_movie.o
>>  OBJS-$(CONFIG_MPTESTSRC_FILTER)              += vsrc_mptestsrc.o
>>  OBJS-$(CONFIG_NULLSRC_FILTER)                += vsrc_testsrc.o
>>  OBJS-$(CONFIG_RGBTESTSRC_FILTER)             += vsrc_testsrc.o
>> +OBJS-$(CONFIG_SMPTEBARS_FILTER)              += vsrc_smptebars.o
>>  OBJS-$(CONFIG_TESTSRC_FILTER)                += vsrc_testsrc.o
>>
>>  OBJS-$(CONFIG_BUFFERSINK_FILTER)             += sink_buffer.o
>> diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
>> index 35065d5..a4e354b 100644
>> --- a/libavfilter/allfilters.c
>> +++ b/libavfilter/allfilters.c
>> @@ -124,6 +124,7 @@ void avfilter_register_all(void)
>>      REGISTER_FILTER (MPTESTSRC,   mptestsrc,   vsrc);
>>      REGISTER_FILTER (NULLSRC,     nullsrc,     vsrc);
>>      REGISTER_FILTER (RGBTESTSRC,  rgbtestsrc,  vsrc);
>> +    REGISTER_FILTER (SMPTEBARS,   smptebars,   vsrc);
>>      REGISTER_FILTER (TESTSRC,     testsrc,     vsrc);
>>
>>      REGISTER_FILTER (BUFFERSINK,  buffersink,  vsink);
>> diff --git a/libavfilter/vsrc_smptebars.c b/libavfilter/vsrc_smptebars.c
>> new file mode 100644
>> index 0000000..70157e8
>> --- /dev/null
>> +++ b/libavfilter/vsrc_smptebars.c
>> @@ -0,0 +1,215 @@
>> +/*
>> + * Copyright (c) 2012 Paul B Mahol
>> + *
>> + * This file is part of FFmpeg.
>> + *
>> + * FFmpeg is free software; you can redistribute it and/or
>> + * modify it under the terms of the GNU Lesser General Public
>> + * License as published by the Free Software Foundation; either
>> + * version 2.1 of the License, or (at your option) any later version.
>> + *
>> + * FFmpeg is distributed in the hope that it will be useful,
>> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
>> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
>> + * Lesser General Public License for more details.
>> + *
>> + * You should have received a copy of the GNU Lesser General Public
>> + * License along with FFmpeg; if not, write to the Free Software
>> + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
>> 02110-1301 USA
>> + */
>> +
>> +#include "libavutil/imgutils.h"
>> +#include "libavutil/opt.h"
>> +#include "libavutil/parseutils.h"
>> +#include "avfilter.h"
>> +#include "formats.h"
>> +#include "video.h"
>> +#include "drawutils.h"
>> +
>> +typedef struct {
>> +    const AVClass *class;
>> +    int w, h;
>> +    AVRational time_base;
>> +    AVRational sar;
>> +    char *rate;
>> +    int nb_decimals;
>> +    FFDrawContext draw;
>> +} SMPTEBarsContext;
>> +
>> +#define OFFSET(x) offsetof(SMPTEBarsContext, x)
>> +
>> +static const AVOption smptebars_options[]= {
>> +    { "size",     "set video size",     OFFSET(w),
>> AV_OPT_TYPE_IMAGE_SIZE, {.str = "720x480"}, 0, 0 },
>> +    { "s",        "set video size",     OFFSET(w),
>> AV_OPT_TYPE_IMAGE_SIZE, {.str = "720x480"}, 0, 0 },
>> +    { "rate",     "set video rate",     OFFSET(rate),
>> AV_OPT_TYPE_STRING, {.str = "30"},      0, 0 },
>> +    { "r",        "set video rate",     OFFSET(rate),
>> AV_OPT_TYPE_STRING, {.str = "30"},      0, 0 },
>> +    { "sar",      "set video sample aspect ratio", OFFSET(sar),
>> AV_OPT_TYPE_RATIONAL, {.dbl= 1},  0, INT_MAX },
>> +    { NULL },
>> +};
>> +
>> +static const AVClass smptebars_class = {
>> +    .class_name = "smptebars",
>> +    .item_name  = av_default_item_name,
>> +    .option     = smptebars_options,
>> +    .version    = LIBAVUTIL_VERSION_INT,
>> +    .category   = AV_CLASS_CATEGORY_FILTER,
>> +};
>> +
>> +static av_cold int smptebars_init(AVFilterContext *ctx, const char *args,
>> void *opaque)
>> +{
>> +    SMPTEBarsContext *smpte = ctx->priv;
>> +    AVRational frame_rate_q;
>> +    int ret = 0;
>> +
>> +    smpte->class = &smptebars_class;
>> +
>> +    av_opt_set_defaults(smpte);
>> +    if ((ret = (av_set_options_string(smpte, args, "=", ":"))) < 0) {
>> +        av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n",
>> args);
>> +        ret = AVERROR(EINVAL);
>> +        goto end;
>> +    }
>> +    if (av_parse_video_rate(&frame_rate_q, smpte->rate) < 0 ||
>> +            frame_rate_q.den <= 0 || frame_rate_q.num <= 0) {
>> +        av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: %s\n",
>> smpte->rate);
>> +        ret = AVERROR(EINVAL);
>> +        goto end;
>> +    }
>> +
>> +    smpte->time_base.num = frame_rate_q.den;
>> +    smpte->time_base.den = frame_rate_q.num;
>> +
>> +end:
>> +    av_opt_free(smpte);
>> +    return ret;
>> +}
>
> This code seems the duplicated of that present in vsrc_testsrc.c,
> should be possible to easily reuse that (and documentation) for free.

As most of other filters.
>
>> +
>> +static int smptebars_query_formats(AVFilterContext *ctx)
>> +{
>> +    ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
>> +    return 0;
>> +}
>> +
>> +static uint8_t rainbow[7][4] = {
>> +    { 191, 191, 191, 255},
>> +    { 191, 191,   0, 255},
>> +    {   0, 191, 191, 255},
>> +    {   0, 191,   0, 255},
>> +    { 191,   0, 191, 255},
>> +    { 191,   0,   0, 255},
>> +    {   0,   0, 191, 255},
>> +};
>> +
>
>> +static uint8_t wobnair[7][4] = {
>> +    {   0,   0, 191, 255},
>> +    {   0,   0,   0, 255},
>> +    { 191,   0, 191, 255},
>> +    {   0,   0,   0, 255},
>> +    {   0, 191, 191, 255},
>> +    {   0,   0,   0, 255},
>> +    { 191, 191, 191, 255},
>> +};
>
> the name is funny but pretty obfuscate, please explain what it
> represents (in a comment)
>
>> +
>> +static uint8_t white[4] = { 255, 255, 255, 255 };
>> +static uint8_t black[4] = {   0,   0,   0, 255 };
>> +
>> +/* made up values */
>> +static uint8_t neg4ire[4] = {   7,   7,   7, 255 };
>> +static uint8_t pos4ire[4] = {  17,  17,  17, 255 };
>> +static uint8_t i_pixel[4] = { 224,   0,   0, 255 };
>> +static uint8_t q_pixel[4] = {   0,   0, 176, 255 };
>> +
>> +static void draw_smptebars(AVFilterContext *ctx, AVFilterBufferRef
>> *picref)
>> +{
>> +    SMPTEBarsContext *smpte = ctx->priv;
>> +    FFDrawColor color;
>> +    int r_w, r_h, w_h, p_w, p_h, i, x = 0;
>> +
>> +    r_w = (smpte->w + 6 ) / 7;
>> +    r_h = smpte->h * 2 / 3;
>> +    w_h = smpte->h * 3 / 4 - r_h;
>> +    p_w = r_w * 5 / 4;
>> +    p_h = smpte->h - w_h - r_h;
>> +    for (i = 0; i < 7; i++) {
>> +        ff_draw_color(&smpte->draw, &color, rainbow[i]);
>> +        ff_fill_rectangle(&smpte->draw, &color, picref->data,
>> picref->linesize, x,   0, r_w, r_h);
>> +        ff_draw_color(&smpte->draw, &color, wobnair[i]);
>> +        ff_fill_rectangle(&smpte->draw, &color, picref->data,
>> picref->linesize, x, r_h, r_w, w_h);
>> +        x += r_w;
>> +    }
>> +    x = 0;
>
>> +    ff_draw_color(&smpte->draw, &color, i_pixel);
>> +    ff_fill_rectangle(&smpte->draw, &color, picref->data,
>> picref->linesize, x, r_h + w_h, p_w, p_h);
>> +    x += p_w;
>
> macro?
>
>> +    ff_draw_color(&smpte->draw, &color, white);
>> +    ff_fill_rectangle(&smpte->draw, &color, picref->data,
>> picref->linesize, x, r_h + w_h, p_w, p_h);
>> +    x += p_w;
>> +    ff_draw_color(&smpte->draw, &color, q_pixel);
>> +    ff_fill_rectangle(&smpte->draw, &color, picref->data,
>> picref->linesize, x, r_h + w_h, p_w, p_h);
>> +    x += p_w;
>> +    ff_draw_color(&smpte->draw, &color, black);
>> +    ff_fill_rectangle(&smpte->draw, &color, picref->data,
>> picref->linesize, x, r_h + w_h, 5 * r_w - x, p_h);
>> +    x += 5 * r_w - x;
>> +    ff_draw_color(&smpte->draw, &color, neg4ire);
>> +    ff_fill_rectangle(&smpte->draw, &color, picref->data,
>> picref->linesize, x, r_h + w_h, r_w / 3, p_h);
>> +    x += r_w / 3;
>> +    ff_draw_color(&smpte->draw, &color, black);
>> +    ff_fill_rectangle(&smpte->draw, &color, picref->data,
>> picref->linesize, x, r_h + w_h, r_w / 3, p_h);
>> +    x += r_w / 3;
>> +    ff_draw_color(&smpte->draw, &color, pos4ire);
>> +    ff_fill_rectangle(&smpte->draw, &color, picref->data,
>> picref->linesize, x, r_h + w_h, r_w / 3, p_h);
>> +    x += r_w / 3;
>> +    ff_draw_color(&smpte->draw, &color, black);
>> +    ff_fill_rectangle(&smpte->draw, &color, picref->data,
>> picref->linesize, x, r_h + w_h, smpte->w - x, p_h);
>> +}
>> +
>
>> +static int request_frame(AVFilterLink *outlink)
>> +{
>> +    SMPTEBarsContext *smpte = outlink->src->priv;
>> +    AVFilterBufferRef *picref;
>> +
>> +    picref = ff_get_video_buffer(outlink, AV_PERM_WRITE, smpte->w,
>> smpte->h);
>> +    picref->video->sample_aspect_ratio = smpte->sar;
>> +
>> +    ff_start_frame(outlink, avfilter_ref_buffer(picref, ~0));
>> +    draw_smptebars(outlink->src, picref);
>> +    ff_draw_slice(outlink, 0, picref->video->h, 1);
>> +    ff_end_frame(outlink);
>> +    avfilter_unref_buffer(picref);
>> +    return 0;
>> +}
>
> Again this can be dropped in favor of the common code in vsrc_testsrc.c.

You mean to move this filter into that file or exporting function?
>
> Also, how are you setting the pts?

Ah, I forgot to copy that one ;-)
>
>> +
>> +static int config_props(AVFilterLink *outlink)
>> +{
>> +    AVFilterContext *ctx = outlink->src;
>> +    SMPTEBarsContext *smpte = ctx->priv;
>> +
>> +    ff_draw_init(&smpte->draw, outlink->format, 0);
>> +
>
>> +    if (av_image_check_size(smpte->w, smpte->h, 0, ctx) < 0)
>> +        return AVERROR(EINVAL);
>
> Is this really required? I wonder becasue I never used it, if required
> the check should be moved to the internal code.

It is copied from vsrc_color, and few other filters have it.

But isn't this checked when w & h values are read?

> [...]
>


More information about the ffmpeg-devel mailing list