FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/af_aformat.c
Date: 2024-04-24 02:45:42
Exec Total Coverage
Lines: 44 51 86.3%
Functions: 5 5 100.0%
Branches: 26 36 72.2%

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/opt.h"
30
31 #include "audio.h"
32 #include "avfilter.h"
33 #include "formats.h"
34 #include "internal.h"
35
36 typedef struct AFormatContext {
37 const AVClass *class;
38
39 AVFilterFormats *formats;
40 AVFilterFormats *sample_rates;
41 AVFilterChannelLayouts *channel_layouts;
42
43 char *formats_str;
44 char *sample_rates_str;
45 char *channel_layouts_str;
46 } AFormatContext;
47
48 #define OFFSET(x) offsetof(AFormatContext, x)
49 #define A AV_OPT_FLAG_AUDIO_PARAM
50 #define F AV_OPT_FLAG_FILTERING_PARAM
51 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 },
58 { NULL }
59 };
60
61 AVFILTER_DEFINE_CLASS(aformat);
62
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 336 static int get_sample_rate(const char *samplerate)
87 {
88 336 int ret = strtol(samplerate, NULL, 0);
89 336 return FFMAX(ret, 0);
90 }
91
92 1875 static int parse_channel_layouts(AVFilterContext *ctx)
93 {
94 1875 AFormatContext *s = ctx->priv;
95 1875 char *next, *cur = s->channel_layouts_str;
96 1875 AVChannelLayout fmt = { 0 };
97 int ret;
98
99
2/2
✓ Branch 0 taken 382 times.
✓ Branch 1 taken 1875 times.
2257 while (cur) {
100 382 next = strchr(cur, '|');
101
2/2
✓ Branch 0 taken 292 times.
✓ Branch 1 taken 90 times.
382 if (next)
102 292 *next++ = 0;
103
104 382 ret = av_channel_layout_from_string(&fmt, cur);
105
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 382 times.
382 if (ret < 0) {
106 av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout: %s.\n", cur);
107 return AVERROR(EINVAL);
108 }
109 382 ret = ff_add_channel_layout(&s->channel_layouts, &fmt);
110 382 av_channel_layout_uninit(&fmt);
111
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 382 times.
382 if (ret < 0)
112 return ret;
113
114 382 cur = next;
115 }
116
117 1875 return 0;
118 }
119
120 1875 static av_cold int init(AVFilterContext *ctx)
121 {
122 1875 AFormatContext *s = ctx->priv;
123 int ret;
124
125
6/8
✓ Branch 0 taken 85 times.
✓ Branch 1 taken 1875 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1960 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 1960 times.
✓ Branch 9 taken 1960 times.
✓ Branch 10 taken 1875 times.
3835 PARSE_FORMATS(s->formats_str, enum AVSampleFormat, s->formats,
126 ff_add_format, av_get_sample_fmt, AV_SAMPLE_FMT_NONE, "sample format");
127
6/8
✓ Branch 0 taken 268 times.
✓ Branch 1 taken 68 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 336 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 336 times.
✓ Branch 9 taken 336 times.
✓ Branch 10 taken 1875 times.
2211 PARSE_FORMATS(s->sample_rates_str, int, s->sample_rates, ff_add_format,
128 get_sample_rate, 0, "sample rate");
129 1875 ret = parse_channel_layouts(ctx);
130
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1875 times.
1875 if (ret < 0)
131 return ret;
132
133 1875 return 0;
134 }
135
136 1875 static av_cold void uninit(AVFilterContext *ctx)
137 {
138 1875 AFormatContext *s = ctx->priv;
139
140 1875 ff_formats_unref(&s->formats);
141 1875 ff_formats_unref(&s->sample_rates);
142 1875 ff_channel_layouts_unref(&s->channel_layouts);
143 1875 }
144
145 1559 static int query_formats(AVFilterContext *ctx)
146 {
147 1559 AFormatContext *s = ctx->priv;
148 int ret;
149
150
1/2
✓ Branch 0 taken 1559 times.
✗ Branch 1 not taken.
1559 ret = ff_set_common_formats(ctx, s->formats ? s->formats :
151 ff_all_formats(AVMEDIA_TYPE_AUDIO));
152 1559 s->formats = NULL;
153
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1559 times.
1559 if (ret < 0)
154 return ret;
155
2/2
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 1491 times.
3050 ret = ff_set_common_samplerates(ctx, s->sample_rates ? s->sample_rates :
156 1491 ff_all_samplerates());
157 1559 s->sample_rates = NULL;
158
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1559 times.
1559 if (ret < 0)
159 return ret;
160
2/2
✓ Branch 0 taken 90 times.
✓ Branch 1 taken 1469 times.
3028 ret = ff_set_common_channel_layouts(ctx, s->channel_layouts ? s->channel_layouts :
161 1469 ff_all_channel_counts());
162 1559 s->channel_layouts = NULL;
163 1559 return ret;
164 }
165
166 const AVFilter ff_af_aformat = {
167 .name = "aformat",
168 .description = NULL_IF_CONFIG_SMALL("Convert the input audio to one of the specified formats."),
169 .init = init,
170 .uninit = uninit,
171 .priv_size = sizeof(AFormatContext),
172 .priv_class = &aformat_class,
173 .flags = AVFILTER_FLAG_METADATA_ONLY,
174 FILTER_INPUTS(ff_audio_default_filterpad),
175 FILTER_OUTPUTS(ff_audio_default_filterpad),
176 FILTER_QUERY_FUNC(query_formats),
177 };
178