History

lavfi/af_aformat: change options from strings to arrays

Allows to drop custom parsing code, and also the assumption that query_formats() is not called more than once.

Anton Khirnov committed 2 years ago

Commit 8bc419a1 parent dc488d81 file · +64 −98Browse files

Unified · Split

libavfilter/af_aformat.c+64−98
@@ -26,6 +26,7 @@ · 25 unmodified lines
2626 #include "libavutil/avstring.h"
2727 #include "libavutil/channel_layout.h"
2828 #include "libavutil/common.h"
29+#include "libavutil/mem.h"
2930 #include "libavutil/opt.h"
3031
3132 #include "audio.h"
@@ -36,138 +37,103 @@ · 4 unmodified lines
3637 typedef struct AFormatContext {
3738 const AVClass *class;
3839
39− AVFilterFormats *formats;
40− AVFilterFormats *sample_rates;
41− AVFilterChannelLayouts *channel_layouts;
40+ enum AVSampleFormat *formats;
41+ unsigned nb_formats;
4242
43− char *formats_str;
44− char *sample_rates_str;
45− char *channel_layouts_str;
43+ int *sample_rates;
44+ unsigned nb_sample_rates;
45+
46+ AVChannelLayout *channel_layouts;
47+ unsigned nb_channel_layouts;
4648 } AFormatContext;
4749
50+static const AVOptionArrayDef array_def = { .sep = '|' };
51+
4852 #define OFFSET(x) offsetof(AFormatContext, x)
4953 #define A AV_OPT_FLAG_AUDIO_PARAM
5054 #define F AV_OPT_FLAG_FILTERING_PARAM
5155 static const AVOption aformat_options[] = {
52− { "sample_fmts", "A '|'-separated list of sample formats.", OFFSET(formats_str), AV_OPT_TYPE_STRING, .flags = A|F },
53− { "f", "A '|'-separated list of sample formats.", OFFSET(formats_str), AV_OPT_TYPE_STRING, .flags = A|F },
54− { "sample_rates", "A '|'-separated list of sample rates.", OFFSET(sample_rates_str), AV_OPT_TYPE_STRING, .flags = A|F },
55− { "r", "A '|'-separated list of sample rates.", OFFSET(sample_rates_str), AV_OPT_TYPE_STRING, .flags = A|F },
56− { "channel_layouts", "A '|'-separated list of channel layouts.", OFFSET(channel_layouts_str), AV_OPT_TYPE_STRING, .flags = A|F },
57− { "cl", "A '|'-separated list of channel layouts.", OFFSET(channel_layouts_str), AV_OPT_TYPE_STRING, .flags = A|F },
56+ { "sample_fmts", "A '|'-separated list of sample formats.", OFFSET(formats),
57+ AV_OPT_TYPE_SAMPLE_FMT | AV_OPT_TYPE_FLAG_ARRAY, .default_val.arr = &array_def, .flags = A|F },
58+ { "f", "A '|'-separated list of sample formats.", OFFSET(formats),
59+ AV_OPT_TYPE_SAMPLE_FMT | AV_OPT_TYPE_FLAG_ARRAY, .default_val.arr = &array_def, .flags = A|F },
60+ { "sample_rates", "A '|'-separated list of sample rates.", OFFSET(sample_rates),
61+ AV_OPT_TYPE_INT | AV_OPT_TYPE_FLAG_ARRAY, .default_val.arr = &array_def, .min = 1, .max = INT_MAX, .flags = A|F },
62+ { "r", "A '|'-separated list of sample rates.", OFFSET(sample_rates),
63+ AV_OPT_TYPE_INT | AV_OPT_TYPE_FLAG_ARRAY, .default_val.arr = &array_def, .min = 1, .max = INT_MAX, .flags = A|F },
64+ { "channel_layouts", "A '|'-separated list of channel layouts.", OFFSET(channel_layouts),
65+ AV_OPT_TYPE_CHLAYOUT | AV_OPT_TYPE_FLAG_ARRAY, .default_val.arr = &array_def, .flags = A|F },
66+ { "cl", "A '|'-separated list of channel layouts.", OFFSET(channel_layouts),
67+ AV_OPT_TYPE_CHLAYOUT | AV_OPT_TYPE_FLAG_ARRAY, .default_val.arr = &array_def, .flags = A|F },
5868 { NULL }
5969 };
6070
6171 AVFILTER_DEFINE_CLASS(aformat);
6272
63−#define PARSE_FORMATS(str, type, list, add_to_list, get_fmt, none, desc) \
64−do { \
65− char *next, *cur = str; \
66− int ret; \
67− \
68− while (cur) { \
69− type fmt; \
70− next = strchr(cur, '|'); \
71− if (next) \
72− *next++ = 0; \
73− \
74− if ((fmt = get_fmt(cur)) == none) { \
75− av_log(ctx, AV_LOG_ERROR, "Error parsing " desc ": %s.\n", cur);\
76− return AVERROR(EINVAL); \
77− } \
78− if ((ret = add_to_list(&list, fmt)) < 0) { \
79− return ret; \
80− } \
81− \
82− cur = next; \
83− } \
84−} while (0)
85−
86−static int get_sample_rate(const char *samplerate)
87−{
88− int ret = strtol(samplerate, NULL, 0);
89− return FFMAX(ret, 0);
90−}
91−
92−static int parse_channel_layouts(AVFilterContext *ctx)
73+static av_cold int init(AVFilterContext *ctx)
9374 {
9475 AFormatContext *s = ctx->priv;
95− char *next, *cur = s->channel_layouts_str;
96− AVChannelLayout fmt = { 0 };
97− int ret;
9876
99− while (cur) {
100− next = strchr(cur, '|');
101− if (next)
102− *next++ = 0;
103−
104− ret = av_channel_layout_from_string(&fmt, cur);
105− if (ret < 0) {
106− av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout: %s.\n", cur);
107− return AVERROR(EINVAL);
108− }
109− ret = ff_add_channel_layout(&s->channel_layouts, &fmt);
110− av_channel_layout_uninit(&fmt);
111− if (ret < 0)
112− return ret;
77+ // terminate format lists for ff_set*_from_list()
78+ if (s->nb_formats) {
79+ void *tmp = av_realloc_array(s->formats, s->nb_formats + 1,
80+ sizeof(*s->formats));
81+ if (!tmp)
82+ return AVERROR(ENOMEM);
83+ s->formats = tmp;
84+ s->formats[s->nb_formats] = AV_SAMPLE_FMT_NONE;
11385
114− cur = next;
86+ }
87+ if (s->nb_sample_rates) {
88+ void *tmp = av_realloc_array(s->sample_rates, s->nb_sample_rates + 1,
89+ sizeof(*s->sample_rates));
90+ if (!tmp)
91+ return AVERROR(ENOMEM);
92+ s->sample_rates = tmp;
93+ s->sample_rates[s->nb_sample_rates] = -1;
94+ }
95+ if (s->nb_channel_layouts) {
96+ void *tmp = av_realloc_array(s->channel_layouts, s->nb_channel_layouts + 1,
97+ sizeof(*s->channel_layouts));
98+ if (!tmp)
99+ return AVERROR(ENOMEM);
100+ s->channel_layouts = tmp;
101+ s->channel_layouts[s->nb_channel_layouts] = (AVChannelLayout){ .nb_channels = 0 };
115102 }
116103
117104 return 0;
118105 }
119106
120−static av_cold int init(AVFilterContext *ctx)
107+static int query_formats(AVFilterContext *ctx)
121108 {
122109 AFormatContext *s = ctx->priv;
123110 int ret;
124111
125− PARSE_FORMATS(s->formats_str, enum AVSampleFormat, s->formats,
126− ff_add_format, av_get_sample_fmt, AV_SAMPLE_FMT_NONE, "sample format");
127− PARSE_FORMATS(s->sample_rates_str, int, s->sample_rates, ff_add_format,
128− get_sample_rate, 0, "sample rate");
129− ret = parse_channel_layouts(ctx);
130− if (ret < 0)
131− return ret;
132−
133− return 0;
134−}
135−
136−static av_cold void uninit(AVFilterContext *ctx)
137−{
138− AFormatContext *s = ctx->priv;
112+ if (s->nb_formats) {
113+ ret = ff_set_common_formats_from_list(ctx, s->formats);
114+ if (ret < 0)
115+ return ret;
116+ }
139117
140− ff_formats_unref(&s->formats);
141− ff_formats_unref(&s->sample_rates);
142− ff_channel_layouts_unref(&s->channel_layouts);
143−}
118+ if (s->nb_sample_rates) {
119+ ret = ff_set_common_samplerates_from_list(ctx, s->sample_rates);
120+ if (ret < 0)
121+ return ret;
122+ }
144123
145−static int query_formats(AVFilterContext *ctx)
146−{
147− AFormatContext *s = ctx->priv;
148− int ret;
124+ if (s->nb_channel_layouts) {
125+ ret = ff_set_common_channel_layouts_from_list(ctx, s->channel_layouts);
126+ if (ret < 0)
127+ return ret;
128+ }
149129
150− ret = ff_set_common_formats(ctx, s->formats ? s->formats :
151− ff_all_formats(AVMEDIA_TYPE_AUDIO));
152− s->formats = NULL;
153− if (ret < 0)
154− return ret;
155− ret = ff_set_common_samplerates(ctx, s->sample_rates ? s->sample_rates :
156− ff_all_samplerates());
157− s->sample_rates = NULL;
158− if (ret < 0)
159− return ret;
160− ret = ff_set_common_channel_layouts(ctx, s->channel_layouts ? s->channel_layouts :
161− ff_all_channel_counts());
162− s->channel_layouts = NULL;
163− return ret;
130+ return 0;
164131 }
165132
166133 const AVFilter ff_af_aformat = {
167134 .name = "aformat",
168135 .description = NULL_IF_CONFIG_SMALL("Convert the input audio to one of the specified formats."),
169136 .init = init,
170− .uninit = uninit,
171137 .priv_size = sizeof(AFormatContext),
172138 .priv_class = &aformat_class,
173139 .flags = AVFILTER_FLAG_METADATA_ONLY,

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t