[FFmpeg-devel] [PATCH v2] avfilter: add dumpwave filter.

Michael Niedermayer michael at niedermayer.cc
Fri Jan 12 04:49:09 EET 2018


On Thu, Jan 11, 2018 at 10:11:47PM +0100, Dmytro Humeniuk wrote:
[...]

> +static const AVOption dumpwave_options[] = {
> +    { "s", "set width and height", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "1800x140"}, 0, 0, FLAGS },
> +    { "size", "set width and height", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "1800x140"}, 0, 0, FLAGS },
> +    { "n", "set number of samples per value per channel",  OFFSET(nb_samples), AV_OPT_TYPE_INT64,  {.i64 = 128}, 1, INT64_MAX, FLAGS },
> +    { "nb_samples", "set number of samples per value per channel",  OFFSET(nb_samples), AV_OPT_TYPE_INT64,  {.i64 = 128}, 1, INT64_MAX, FLAGS },
> +    { "f", "set json dump file", OFFSET(json_filename), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, FLAGS },
> +    { "json", "set json dump file", OFFSET(json_filename), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, FLAGS },
> +    { NULL }
> +};
> +
> +AVFILTER_DEFINE_CLASS(dumpwave);
> +

> +static av_cold int init(AVFilterContext *ctx)
> +{
> +    DumpWaveContext *dumpwave = ctx->priv;
> +    
> +    dumpwave->sum = dumpwave->i = dumpwave->n = 0;
> +    
> +    if (!dumpwave->json_filename) {
> +        dumpwave->dump_fp = stdout;

stdout is not safe as a output for filters and a bad default
2 filters writing to stdout would interfere
it would interfere with a user application using stdout



> +    } else {
> +        dumpwave->dump_fp = fopen(dumpwave->json_filename, "w");
> +        if (!dumpwave->dump_fp) {
> +            int err = AVERROR(errno);
> +            char buf[128];
> +            av_strerror(err, buf, sizeof(buf));
> +            av_log(ctx, AV_LOG_ERROR, "Could not open json file %s: %s\n",
> +                   dumpwave->json_filename, buf);
> +            return err;
> +        }
> +    }
> +    return 0;
> +}
> +
> +static av_cold void uninit(AVFilterContext *ctx)
> +{
> +    DumpWaveContext *dumpwave = ctx->priv;
> +    fclose(dumpwave->dump_fp);
> +    av_freep(&dumpwave->str);
> +    av_freep(&dumpwave->values);
> +}
> +
> +static int query_formats(AVFilterContext *ctx)
> +{
> +    static const enum AVSampleFormat sample_fmts[] = {
> +        AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_U8P,
> +        AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16P,
> +        AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32P,
> +        AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S64P,
> +        AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLTP,
> +        AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBLP,
> +        AV_SAMPLE_FMT_NONE
> +    };
> +    AVFilterFormats *formats;
> +    AVFilterChannelLayouts *layouts;
> +    int ret;
> +    
> +    if (!(formats = ff_make_format_list(sample_fmts)))
> +        return AVERROR(ENOMEM);
> +    
> +    layouts = ff_all_channel_counts();
> +    if (!layouts)
> +        return AVERROR(ENOMEM);
> +    ret = ff_set_common_channel_layouts(ctx, layouts);
> +    if (ret < 0)
> +        return ret;
> +    
> +    return ff_set_common_formats(ctx, formats);
> +}
> +
> +static int config_output(AVFilterLink *outlink)
> +{
> +    AVFilterContext *ctx = outlink->src;
> +    DumpWaveContext *dumpwave = ctx->priv;
> +    const int width = dumpwave->w;
> +    dumpwave->values = av_realloc(NULL, width * sizeof(double));
> +    dumpwave->str = av_realloc(NULL, width * sizeof(int));
> +    dumpwave->max_samples = dumpwave->nb_samples * outlink->channels;
> +    
> +    return 0;
> +}
> +
> +static int dumpwave_request_frame(AVFilterLink *outlink)
> +{
> +    AVFilterContext *ctx = outlink->src;
> +    DumpWaveContext *dumpwave = ctx->priv;
> +    const int width = dumpwave->w;
> +    const int height = dumpwave->h;
> +    char *p, *result = dumpwave->str;
> +    
> +    AVFilterLink *inlink = ctx->inputs[0];
> +    int ret;
> +
> +    ret = ff_request_frame(inlink);
> +
> +    if (ret == AVERROR_EOF) {
> +        p = result;
> +
> +        for(int i = 0; i < width; i++)
> +            p += sprintf(p, "%d,", av_clip(dumpwave->h * dumpwave->values[i], 0, dumpwave->h));

sprintf is generally not safe as it does not check if there is enough space

[...]

-- 
Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

No great genius has ever existed without some touch of madness. -- Aristotle
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20180112/0ab71458/attachment.sig>


More information about the ffmpeg-devel mailing list