From d28cb849899abd98bf2dd72f5a3dd56d441aeb27 Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Mon, 25 Feb 2013 21:21:29 +0100 Subject: buffersrc: switch to an AVOptions-based system. --- libavfilter/buffersrc.c | 71 +++++++++++++++++++++++++++++-------------------- 1 file changed, 42 insertions(+), 29 deletions(-) (limited to 'libavfilter/buffersrc.c') diff --git a/libavfilter/buffersrc.c b/libavfilter/buffersrc.c index 2219ba5f81..ea18c128a0 100644 --- a/libavfilter/buffersrc.c +++ b/libavfilter/buffersrc.c @@ -23,6 +23,8 @@ * memory buffer source filter */ +#include + #include "libavutil/channel_layout.h" #include "libavutil/common.h" #include "libavutil/fifo.h" @@ -45,6 +47,7 @@ typedef struct { /* video only */ int h, w; enum AVPixelFormat pix_fmt; + char *pix_fmt_str; AVRational pixel_aspect; /* audio only */ @@ -236,21 +239,17 @@ fail: static av_cold int init_video(AVFilterContext *ctx, const char *args) { BufferSourceContext *c = ctx->priv; - char pix_fmt_str[128]; - int n = 0; - - if (!args || - (n = sscanf(args, "%d:%d:%127[^:]:%d:%d:%d:%d", &c->w, &c->h, pix_fmt_str, - &c->time_base.num, &c->time_base.den, - &c->pixel_aspect.num, &c->pixel_aspect.den)) != 7) { - av_log(ctx, AV_LOG_ERROR, "Expected 7 arguments, but %d found in '%s'\n", n, args); + + if (!c->pix_fmt_str || !c->w || !c->h || av_q2d(c->time_base) <= 0) { + av_log(ctx, AV_LOG_ERROR, "Invalid parameters provided.\n"); return AVERROR(EINVAL); } - if ((c->pix_fmt = av_get_pix_fmt(pix_fmt_str)) == AV_PIX_FMT_NONE) { + + if ((c->pix_fmt = av_get_pix_fmt(c->pix_fmt_str)) == AV_PIX_FMT_NONE) { char *tail; - c->pix_fmt = strtol(pix_fmt_str, &tail, 10); + c->pix_fmt = strtol(c->pix_fmt_str, &tail, 10); if (*tail || c->pix_fmt < 0 || c->pix_fmt >= AV_PIX_FMT_NB) { - av_log(ctx, AV_LOG_ERROR, "Invalid pixel format string '%s'\n", pix_fmt_str); + av_log(ctx, AV_LOG_ERROR, "Invalid pixel format string '%s'\n", c->pix_fmt_str); return AVERROR(EINVAL); } } @@ -264,6 +263,32 @@ static av_cold int init_video(AVFilterContext *ctx, const char *args) #define OFFSET(x) offsetof(BufferSourceContext, x) #define A AV_OPT_FLAG_AUDIO_PARAM +#define V AV_OPT_FLAG_VIDEO_PARAM + +static const AVOption video_options[] = { + { "width", NULL, OFFSET(w), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V }, + { "height", NULL, OFFSET(h), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V }, + { "pix_fmt", NULL, OFFSET(pix_fmt_str), AV_OPT_TYPE_STRING, .flags = V }, +#if FF_API_OLD_FILTER_OPTS + /* those 4 are for compatibility with the old option passing system where each filter + * did its own parsing */ + { "time_base_num", "deprecated, do not use", OFFSET(time_base.num), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V }, + { "time_base_den", "deprecated, do not use", OFFSET(time_base.den), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V }, + { "sar_num", "deprecated, do not use", OFFSET(pixel_aspect.num), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V }, + { "sar_den", "deprecated, do not use", OFFSET(pixel_aspect.den), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V }, +#endif + { "sar", "sample aspect ratio", OFFSET(pixel_aspect), AV_OPT_TYPE_RATIONAL, { .dbl = 1 }, 0, DBL_MAX, V }, + { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V }, + { NULL }, +}; + +static const AVClass buffer_class = { + .class_name = "buffer source", + .item_name = av_default_item_name, + .option = video_options, + .version = LIBAVUTIL_VERSION_INT, +}; + static const AVOption audio_options[] = { { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, INT_MAX, A }, { "sample_rate", NULL, OFFSET(sample_rate), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, A }, @@ -284,34 +309,22 @@ static av_cold int init_audio(AVFilterContext *ctx, const char *args) BufferSourceContext *s = ctx->priv; int ret = 0; - s->class = &abuffer_class; - av_opt_set_defaults(s); - - if ((ret = av_set_options_string(s, args, "=", ":")) < 0) { - av_log(ctx, AV_LOG_ERROR, "Error parsing options string: %s.\n", args); - goto fail; - } - s->sample_fmt = av_get_sample_fmt(s->sample_fmt_str); if (s->sample_fmt == AV_SAMPLE_FMT_NONE) { av_log(ctx, AV_LOG_ERROR, "Invalid sample format %s.\n", s->sample_fmt_str); - ret = AVERROR(EINVAL); - goto fail; + return AVERROR(EINVAL); } s->channel_layout = av_get_channel_layout(s->channel_layout_str); if (!s->channel_layout) { av_log(ctx, AV_LOG_ERROR, "Invalid channel layout %s.\n", s->channel_layout_str); - ret = AVERROR(EINVAL); - goto fail; + return AVERROR(EINVAL); } - if (!(s->fifo = av_fifo_alloc(sizeof(AVFrame*)))) { - ret = AVERROR(ENOMEM); - goto fail; - } + if (!(s->fifo = av_fifo_alloc(sizeof(AVFrame*)))) + return AVERROR(ENOMEM); if (!s->time_base.num) s->time_base = (AVRational){1, s->sample_rate}; @@ -320,8 +333,6 @@ static av_cold int init_audio(AVFilterContext *ctx, const char *args) "ch layout:%s\n", s->time_base.num, s->time_base.den, s->sample_fmt_str, s->sample_rate, s->channel_layout_str); -fail: - av_opt_free(s); return ret; } @@ -430,6 +441,7 @@ AVFilter avfilter_vsrc_buffer = { .name = "buffer", .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."), .priv_size = sizeof(BufferSourceContext), + .priv_class = &buffer_class, .query_formats = query_formats, .init = init_video, @@ -454,6 +466,7 @@ AVFilter avfilter_asrc_abuffer = { .name = "abuffer", .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them accessible to the filterchain."), .priv_size = sizeof(BufferSourceContext), + .priv_class = &abuffer_class, .query_formats = query_formats, .init = init_audio, -- cgit v1.2.3