Allows to drop custom parsing code, and also the assumption that query_formats() is not called more than once.
Unified · Split
| @@ -26,6 +26,7 @@ · 25 unmodified lines | |||
| 26 | #include "libavutil/avstring.h" | 26 | #include "libavutil/avstring.h" |
| 27 | #include "libavutil/channel_layout.h" | 27 | #include "libavutil/channel_layout.h" |
| 28 | #include "libavutil/common.h" | 28 | #include "libavutil/common.h" |
| 29 | #include "libavutil/mem.h" | ||
| 29 | #include "libavutil/opt.h" | 30 | #include "libavutil/opt.h" |
| 30 | 31 | ||
| 31 | #include "audio.h" | 32 | #include "audio.h" |
| @@ -36,138 +37,103 @@ · 4 unmodified lines | |||
| 36 | typedef struct AFormatContext { | 37 | typedef struct AFormatContext { |
| 37 | const AVClass *class; | 38 | const AVClass *class; |
| 38 | 39 | ||
| 39 | AVFilterFormats *formats; | 40 | enum AVSampleFormat *formats; |
| 40 | AVFilterFormats *sample_rates; | 41 | unsigned nb_formats; |
| 41 | AVFilterChannelLayouts *channel_layouts; | ||
| 42 | 42 | ||
| 43 | char *formats_str; | 43 | int *sample_rates; |
| 44 | char *sample_rates_str; | 44 | unsigned nb_sample_rates; |
| 45 | char *channel_layouts_str; | 45 | |
| 46 | AVChannelLayout *channel_layouts; | ||
| 47 | unsigned nb_channel_layouts; | ||
| 46 | } AFormatContext; | 48 | } AFormatContext; |
| 47 | 49 | ||
| 50 | static const AVOptionArrayDef array_def = { .sep = '|' }; | ||
| 51 | |||
| 48 | #define OFFSET(x) offsetof(AFormatContext, x) | 52 | #define OFFSET(x) offsetof(AFormatContext, x) |
| 49 | #define A AV_OPT_FLAG_AUDIO_PARAM | 53 | #define A AV_OPT_FLAG_AUDIO_PARAM |
| 50 | #define F AV_OPT_FLAG_FILTERING_PARAM | 54 | #define F AV_OPT_FLAG_FILTERING_PARAM |
| 51 | static const AVOption aformat_options[] = { | 55 | static const AVOption aformat_options[] = { |
| 52 | { "sample_fmts", "A '|'-separated list of sample formats.", OFFSET(formats_str), AV_OPT_TYPE_STRING, .flags = A|F }, | 56 | { "sample_fmts", "A '|'-separated list of sample formats.", OFFSET(formats), |
| 53 | { "f", "A '|'-separated list of sample formats.", OFFSET(formats_str), AV_OPT_TYPE_STRING, .flags = A|F }, | 57 | AV_OPT_TYPE_SAMPLE_FMT | AV_OPT_TYPE_FLAG_ARRAY, .default_val.arr = &array_def, .flags = A|F }, |
| 54 | { "sample_rates", "A '|'-separated list of sample rates.", OFFSET(sample_rates_str), AV_OPT_TYPE_STRING, .flags = A|F }, | 58 | { "f", "A '|'-separated list of sample formats.", OFFSET(formats), |
| 55 | { "r", "A '|'-separated list of sample rates.", OFFSET(sample_rates_str), AV_OPT_TYPE_STRING, .flags = A|F }, | 59 | AV_OPT_TYPE_SAMPLE_FMT | AV_OPT_TYPE_FLAG_ARRAY, .default_val.arr = &array_def, .flags = A|F }, |
| 56 | { "channel_layouts", "A '|'-separated list of channel layouts.", OFFSET(channel_layouts_str), AV_OPT_TYPE_STRING, .flags = A|F }, | 60 | { "sample_rates", "A '|'-separated list of sample rates.", OFFSET(sample_rates), |
| 57 | { "cl", "A '|'-separated list of channel layouts.", OFFSET(channel_layouts_str), AV_OPT_TYPE_STRING, .flags = A|F }, | 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 }, | ||
| 58 | { NULL } | 68 | { NULL } |
| 59 | }; | 69 | }; |
| 60 | 70 | ||
| 61 | AVFILTER_DEFINE_CLASS(aformat); | 71 | AVFILTER_DEFINE_CLASS(aformat); |
| 62 | 72 | ||
| 63 | #define PARSE_FORMATS(str, type, list, add_to_list, get_fmt, none, desc) \ | 73 | static av_cold int init(AVFilterContext *ctx) |
| 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) | ||
| 93 | { | 74 | { |
| 94 | AFormatContext *s = ctx->priv; | 75 | AFormatContext *s = ctx->priv; |
| 95 | char *next, *cur = s->channel_layouts_str; | ||
| 96 | AVChannelLayout fmt = { 0 }; | ||
| 97 | int ret; | ||
| 98 | 76 | ||
| 99 | while (cur) { | 77 | // terminate format lists for ff_set*_from_list() |
| 100 | next = strchr(cur, '|'); | 78 | if (s->nb_formats) { |
| 101 | if (next) | 79 | void *tmp = av_realloc_array(s->formats, s->nb_formats + 1, |
| 102 | *next++ = 0; | 80 | sizeof(*s->formats)); |
| 103 | 81 | if (!tmp) | |
| 104 | ret = av_channel_layout_from_string(&fmt, cur); | 82 | return AVERROR(ENOMEM); |
| 105 | if (ret < 0) { | 83 | s->formats = tmp; |
| 106 | av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout: %s.\n", cur); | 84 | s->formats[s->nb_formats] = AV_SAMPLE_FMT_NONE; |
| 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; | ||
| 113 | 85 | ||
| 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 }; | ||
| 115 | } | 102 | } |
| 116 | 103 | ||
| 117 | return 0; | 104 | return 0; |
| 118 | } | 105 | } |
| 119 | 106 | ||
| 120 | static av_cold int init(AVFilterContext *ctx) | 107 | static int query_formats(AVFilterContext *ctx) |
| 121 | { | 108 | { |
| 122 | AFormatContext *s = ctx->priv; | 109 | AFormatContext *s = ctx->priv; |
| 123 | int ret; | 110 | int ret; |
| 124 | 111 | ||
| 125 | PARSE_FORMATS(s->formats_str, enum AVSampleFormat, s->formats, | 112 | if (s->nb_formats) { |
| 126 | ff_add_format, av_get_sample_fmt, AV_SAMPLE_FMT_NONE, "sample format"); | 113 | ret = ff_set_common_formats_from_list(ctx, s->formats); |
| 127 | PARSE_FORMATS(s->sample_rates_str, int, s->sample_rates, ff_add_format, | 114 | if (ret < 0) |
| 128 | get_sample_rate, 0, "sample rate"); | 115 | return ret; |
| 129 | ret = parse_channel_layouts(ctx); | 116 | } |
| 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; | ||
| 139 | 117 | ||
| 140 | ff_formats_unref(&s->formats); | 118 | if (s->nb_sample_rates) { |
| 141 | ff_formats_unref(&s->sample_rates); | 119 | ret = ff_set_common_samplerates_from_list(ctx, s->sample_rates); |
| 142 | ff_channel_layouts_unref(&s->channel_layouts); | 120 | if (ret < 0) |
| 143 | } | 121 | return ret; |
| 122 | } | ||
| 144 | 123 | ||
| 145 | static int query_formats(AVFilterContext *ctx) | 124 | if (s->nb_channel_layouts) { |
| 146 | { | 125 | ret = ff_set_common_channel_layouts_from_list(ctx, s->channel_layouts); |
| 147 | AFormatContext *s = ctx->priv; | 126 | if (ret < 0) |
| 148 | int ret; | 127 | return ret; |
| 128 | } | ||
| 149 | 129 | ||
| 150 | ret = ff_set_common_formats(ctx, s->formats ? s->formats : | 130 | return 0; |
| 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; | ||
| 164 | } | 131 | } |
| 165 | 132 | ||
| 166 | const AVFilter ff_af_aformat = { | 133 | const AVFilter ff_af_aformat = { |
| 167 | .name = "aformat", | 134 | .name = "aformat", |
| 168 | .description = NULL_IF_CONFIG_SMALL("Convert the input audio to one of the specified formats."), | 135 | .description = NULL_IF_CONFIG_SMALL("Convert the input audio to one of the specified formats."), |
| 169 | .init = init, | 136 | .init = init, |
| 170 | .uninit = uninit, | ||
| 171 | .priv_size = sizeof(AFormatContext), | 137 | .priv_size = sizeof(AFormatContext), |
| 172 | .priv_class = &aformat_class, | 138 | .priv_class = &aformat_class, |
| 173 | .flags = AVFILTER_FLAG_METADATA_ONLY, | 139 | .flags = AVFILTER_FLAG_METADATA_ONLY, |