| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2011 Mina Nagy Zaki | ||
| 3 | * | ||
| 4 | * This file is part of FFmpeg. | ||
| 5 | * | ||
| 6 | * FFmpeg is free software; you can redistribute it and/or | ||
| 7 | * modify it under the terms of the GNU Lesser General Public | ||
| 8 | * License as published by the Free Software Foundation; either | ||
| 9 | * version 2.1 of the License, or (at your option) any later version. | ||
| 10 | * | ||
| 11 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 14 | * Lesser General Public License for more details. | ||
| 15 | * | ||
| 16 | * You should have received a copy of the GNU Lesser General Public | ||
| 17 | * License along with FFmpeg; if not, write to the Free Software | ||
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 19 | */ | ||
| 20 | |||
| 21 | /** | ||
| 22 | * @file | ||
| 23 | * format audio filter | ||
| 24 | */ | ||
| 25 | |||
| 26 | #include "libavutil/avstring.h" | ||
| 27 | #include "libavutil/channel_layout.h" | ||
| 28 | #include "libavutil/common.h" | ||
| 29 | #include "libavutil/mem.h" | ||
| 30 | #include "libavutil/opt.h" | ||
| 31 | |||
| 32 | #include "audio.h" | ||
| 33 | #include "avfilter.h" | ||
| 34 | #include "filters.h" | ||
| 35 | #include "formats.h" | ||
| 36 | |||
| 37 | typedef struct AFormatContext { | ||
| 38 | const AVClass *class; | ||
| 39 | |||
| 40 | enum AVSampleFormat *formats; | ||
| 41 | unsigned nb_formats; | ||
| 42 | |||
| 43 | int *sample_rates; | ||
| 44 | unsigned nb_sample_rates; | ||
| 45 | |||
| 46 | AVChannelLayout *channel_layouts; | ||
| 47 | unsigned nb_channel_layouts; | ||
| 48 | } AFormatContext; | ||
| 49 | |||
| 50 | static const AVOptionArrayDef array_def = { .sep = '|' }; | ||
| 51 | |||
| 52 | #define OFFSET(x) offsetof(AFormatContext, x) | ||
| 53 | #define A AV_OPT_FLAG_AUDIO_PARAM | ||
| 54 | #define F AV_OPT_FLAG_FILTERING_PARAM | ||
| 55 | static const AVOption aformat_options[] = { | ||
| 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 }, | ||
| 68 | { NULL } | ||
| 69 | }; | ||
| 70 | |||
| 71 | AVFILTER_DEFINE_CLASS(aformat); | ||
| 72 | |||
| 73 | 1973 | static av_cold int init(AVFilterContext *ctx) | |
| 74 | { | ||
| 75 | 1973 | AFormatContext *s = ctx->priv; | |
| 76 | |||
| 77 | // terminate format lists for ff_set*_from_list() | ||
| 78 |
2/2✓ Branch 0 taken 1971 times.
✓ Branch 1 taken 2 times.
|
1973 | if (s->nb_formats) { |
| 79 | 1971 | void *tmp = av_realloc_array(s->formats, s->nb_formats + 1, | |
| 80 | sizeof(*s->formats)); | ||
| 81 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1971 times.
|
1971 | if (!tmp) |
| 82 | ✗ | return AVERROR(ENOMEM); | |
| 83 | 1971 | s->formats = tmp; | |
| 84 | 1971 | s->formats[s->nb_formats] = AV_SAMPLE_FMT_NONE; | |
| 85 | |||
| 86 | } | ||
| 87 |
2/2✓ Branch 0 taken 68 times.
✓ Branch 1 taken 1905 times.
|
1973 | if (s->nb_sample_rates) { |
| 88 | 68 | void *tmp = av_realloc_array(s->sample_rates, s->nb_sample_rates + 1, | |
| 89 | sizeof(*s->sample_rates)); | ||
| 90 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 68 times.
|
68 | if (!tmp) |
| 91 | ✗ | return AVERROR(ENOMEM); | |
| 92 | 68 | s->sample_rates = tmp; | |
| 93 | 68 | s->sample_rates[s->nb_sample_rates] = -1; | |
| 94 | } | ||
| 95 |
2/2✓ Branch 0 taken 93 times.
✓ Branch 1 taken 1880 times.
|
1973 | if (s->nb_channel_layouts) { |
| 96 | 93 | void *tmp = av_realloc_array(s->channel_layouts, s->nb_channel_layouts + 1, | |
| 97 | sizeof(*s->channel_layouts)); | ||
| 98 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
|
93 | if (!tmp) |
| 99 | ✗ | return AVERROR(ENOMEM); | |
| 100 | 93 | s->channel_layouts = tmp; | |
| 101 | 93 | s->channel_layouts[s->nb_channel_layouts] = (AVChannelLayout){ .nb_channels = 0 }; | |
| 102 | } | ||
| 103 | |||
| 104 | 1973 | return 0; | |
| 105 | } | ||
| 106 | |||
| 107 | 1656 | static int query_formats(const AVFilterContext *ctx, | |
| 108 | AVFilterFormatsConfig **cfg_in, | ||
| 109 | AVFilterFormatsConfig **cfg_out) | ||
| 110 | { | ||
| 111 | 1656 | const AFormatContext *s = ctx->priv; | |
| 112 | int ret; | ||
| 113 | |||
| 114 |
2/2✓ Branch 0 taken 1655 times.
✓ Branch 1 taken 1 times.
|
1656 | if (s->nb_formats) { |
| 115 | 1655 | ret = ff_set_common_formats_from_list2(ctx, cfg_in, cfg_out, s->formats); | |
| 116 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1655 times.
|
1655 | if (ret < 0) |
| 117 | ✗ | return ret; | |
| 118 | } | ||
| 119 | |||
| 120 |
2/2✓ Branch 0 taken 68 times.
✓ Branch 1 taken 1588 times.
|
1656 | if (s->nb_sample_rates) { |
| 121 | 68 | ret = ff_set_common_samplerates_from_list2(ctx, cfg_in, cfg_out, s->sample_rates); | |
| 122 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 68 times.
|
68 | if (ret < 0) |
| 123 | ✗ | return ret; | |
| 124 | } | ||
| 125 | |||
| 126 |
2/2✓ Branch 0 taken 92 times.
✓ Branch 1 taken 1564 times.
|
1656 | if (s->nb_channel_layouts) { |
| 127 | 92 | ret = ff_set_common_channel_layouts_from_list2(ctx, cfg_in, cfg_out, s->channel_layouts); | |
| 128 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 92 times.
|
92 | if (ret < 0) |
| 129 | ✗ | return ret; | |
| 130 | } | ||
| 131 | |||
| 132 | 1656 | return 0; | |
| 133 | } | ||
| 134 | |||
| 135 | const FFFilter ff_af_aformat = { | ||
| 136 | .p.name = "aformat", | ||
| 137 | .p.description = NULL_IF_CONFIG_SMALL("Convert the input audio to one of the specified formats."), | ||
| 138 | .p.priv_class = &aformat_class, | ||
| 139 | .p.flags = AVFILTER_FLAG_METADATA_ONLY, | ||
| 140 | .init = init, | ||
| 141 | .priv_size = sizeof(AFormatContext), | ||
| 142 | FILTER_INPUTS(ff_audio_default_filterpad), | ||
| 143 | FILTER_OUTPUTS(ff_audio_default_filterpad), | ||
| 144 | FILTER_QUERY_FUNC2(query_formats), | ||
| 145 | }; | ||
| 146 |