FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/af_channelsplit.c
Date: 2022-12-05 20:26:17
Exec Total Coverage
Lines: 79 106 74.5%
Functions: 5 5 100.0%
Branches: 44 70 62.9%

Line Branch Exec Source
1 /*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 /**
20 * @file
21 * Channel split filter
22 *
23 * Split an audio stream into per-channel streams.
24 */
25
26 #include "libavutil/attributes.h"
27 #include "libavutil/channel_layout.h"
28 #include "libavutil/internal.h"
29 #include "libavutil/opt.h"
30
31 #include "audio.h"
32 #include "avfilter.h"
33 #include "filters.h"
34 #include "formats.h"
35 #include "internal.h"
36
37 #define MAX_CH 64
38
39 typedef struct ChannelSplitContext {
40 const AVClass *class;
41
42 AVChannelLayout channel_layout;
43 char *channel_layout_str;
44 char *channels_str;
45
46 int map[64];
47 } ChannelSplitContext;
48
49 #define OFFSET(x) offsetof(ChannelSplitContext, x)
50 #define A AV_OPT_FLAG_AUDIO_PARAM
51 #define F AV_OPT_FLAG_FILTERING_PARAM
52 static const AVOption channelsplit_options[] = {
53 { "channel_layout", "Input channel layout.", OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, { .str = "stereo" }, .flags = A|F },
54 { "channels", "Channels to extract.", OFFSET(channels_str), AV_OPT_TYPE_STRING, { .str = "all" }, .flags = A|F },
55 { NULL }
56 };
57
58 AVFILTER_DEFINE_CLASS(channelsplit);
59
60 2 static av_cold int init(AVFilterContext *ctx)
61 {
62 2 ChannelSplitContext *s = ctx->priv;
63 2 AVChannelLayout channel_layout = { 0 };
64 2 int all = 0, ret = 0, i;
65
66
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if ((ret = av_channel_layout_from_string(&s->channel_layout, s->channel_layout_str)) < 0) {
67 av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout '%s'.\n",
68 s->channel_layout_str);
69 ret = AVERROR(EINVAL);
70 goto fail;
71 }
72
73
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (!strcmp(s->channels_str, "all")) {
74
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if ((ret = av_channel_layout_copy(&channel_layout, &s->channel_layout)) < 0)
75 goto fail;
76 2 all = 1;
77 } else {
78 if ((ret = av_channel_layout_from_string(&channel_layout, s->channels_str)) < 0)
79 goto fail;
80 }
81
82
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (channel_layout.nb_channels > MAX_CH) {
83 av_log(ctx, AV_LOG_ERROR, "Too many channels\n");
84 goto fail;
85 }
86
87
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 for (i = 0; i < channel_layout.nb_channels; i++) {
88 4 enum AVChannel channel = av_channel_layout_channel_from_index(&channel_layout, i);
89 char buf[64];
90 4 AVFilterPad pad = { .flags = AVFILTERPAD_FLAG_FREE_NAME };
91
92 4 av_channel_name(buf, sizeof(buf), channel);
93 4 pad.type = AVMEDIA_TYPE_AUDIO;
94 4 pad.name = av_strdup(buf);
95
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!pad.name) {
96 ret = AVERROR(ENOMEM);
97 goto fail;
98 }
99
100
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (all) {
101 4 s->map[i] = i;
102 } else {
103 if ((ret = av_channel_layout_index_from_channel(&s->channel_layout, channel)) < 0) {
104 av_log(ctx, AV_LOG_ERROR, "Channel name '%s' not present in channel layout '%s'.\n",
105 pad.name, s->channel_layout_str);
106 av_freep(&pad.name);
107 goto fail;
108 }
109
110 s->map[i] = ret;
111 }
112
113
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 if ((ret = ff_append_outpad(ctx, &pad)) < 0)
114 goto fail;
115 }
116
117 2 fail:
118 2 av_channel_layout_uninit(&channel_layout);
119 2 return ret;
120 }
121
122 2 static av_cold void uninit(AVFilterContext *ctx)
123 {
124 2 ChannelSplitContext *s = ctx->priv;
125
126 2 av_channel_layout_uninit(&s->channel_layout);
127 2 }
128
129 1 static int query_formats(AVFilterContext *ctx)
130 {
131 1 ChannelSplitContext *s = ctx->priv;
132 1 AVFilterChannelLayouts *in_layouts = NULL;
133 int i, ret;
134
135
2/4
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
2 if ((ret = ff_set_common_formats(ctx, ff_planar_sample_fmts())) < 0 ||
136 1 (ret = ff_set_common_all_samplerates(ctx)) < 0)
137 return ret;
138
139
2/4
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
2 if ((ret = ff_add_channel_layout(&in_layouts, &s->channel_layout)) < 0 ||
140 1 (ret = ff_channel_layouts_ref(in_layouts, &ctx->inputs[0]->outcfg.channel_layouts)) < 0)
141 return ret;
142
143
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 for (i = 0; i < ctx->nb_outputs; i++) {
144 2 AVChannelLayout channel_layout = { 0 };
145 2 AVFilterChannelLayouts *out_layouts = NULL;
146 2 enum AVChannel channel = av_channel_layout_channel_from_index(&s->channel_layout, s->map[i]);
147
148
2/4
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
4 if ((ret = av_channel_layout_from_mask(&channel_layout, 1ULL << channel)) < 0 ||
149
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
4 (ret = ff_add_channel_layout(&out_layouts, &channel_layout)) < 0 ||
150 2 (ret = ff_channel_layouts_ref(out_layouts, &ctx->outputs[i]->incfg.channel_layouts)) < 0)
151 return ret;
152 }
153
154 1 return 0;
155 }
156
157 518 static int filter_frame(AVFilterLink *outlink, AVFrame *buf)
158 {
159 518 AVFilterContext *ctx = outlink->src;
160 518 ChannelSplitContext *s = ctx->priv;
161 518 const int i = FF_OUTLINK_IDX(outlink);
162 518 enum AVChannel channel = av_channel_layout_channel_from_index(&buf->ch_layout, s->map[i]);
163 int ret;
164
165 518 AVFrame *buf_out = av_frame_clone(buf);
166
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 518 times.
518 if (!buf_out)
167 return AVERROR(ENOMEM);
168
169 518 buf_out->data[0] = buf_out->extended_data[0] = buf_out->extended_data[s->map[i]];
170 518 ret = av_channel_layout_from_mask(&buf_out->ch_layout, 1ULL << channel);
171
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 518 times.
518 if (ret < 0)
172 return ret;
173 #if FF_API_OLD_CHANNEL_LAYOUT
174 FF_DISABLE_DEPRECATION_WARNINGS
175 518 buf_out->channel_layout =
176 518 av_channel_layout_extract_channel(buf->channel_layout, s->map[i]);
177 518 buf_out->channels = 1;
178 FF_ENABLE_DEPRECATION_WARNINGS
179 #endif
180
181 518 return ff_filter_frame(ctx->outputs[i], buf_out);
182 }
183
184 521 static int activate(AVFilterContext *ctx)
185 {
186 521 AVFilterLink *inlink = ctx->inputs[0];
187 int status, ret;
188 AVFrame *in;
189 int64_t pts;
190
191
2/2
✓ Branch 0 taken 1040 times.
✓ Branch 1 taken 519 times.
1559 for (int i = 0; i < ctx->nb_outputs; i++) {
192
4/4
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1038 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 2 times.
1042 FF_FILTER_FORWARD_STATUS_BACK_ALL(ctx->outputs[i], ctx);
193 }
194
195 519 ret = ff_inlink_consume_frame(inlink, &in);
196
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 519 times.
519 if (ret < 0)
197 return ret;
198
2/2
✓ Branch 0 taken 259 times.
✓ Branch 1 taken 260 times.
519 if (ret > 0) {
199
2/2
✓ Branch 0 taken 518 times.
✓ Branch 1 taken 259 times.
777 for (int i = 0; i < ctx->nb_outputs; i++) {
200
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 518 times.
518 if (ff_outlink_get_status(ctx->outputs[i]))
201 continue;
202
203 518 ret = filter_frame(ctx->outputs[i], in);
204
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 518 times.
518 if (ret < 0)
205 break;
206 }
207
208 259 av_frame_free(&in);
209
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 259 times.
259 if (ret < 0)
210 return ret;
211 }
212
213
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 518 times.
519 if (ff_inlink_acknowledge_status(inlink, &status, &pts)) {
214
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 for (int i = 0; i < ctx->nb_outputs; i++) {
215
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (ff_outlink_get_status(ctx->outputs[i]))
216 continue;
217 2 ff_outlink_set_status(ctx->outputs[i], status, pts);
218 }
219 1 return 0;
220 }
221
222
2/2
✓ Branch 0 taken 777 times.
✓ Branch 1 taken 259 times.
1036 for (int i = 0; i < ctx->nb_outputs; i++) {
223
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 777 times.
777 if (ff_outlink_get_status(ctx->outputs[i]))
224 continue;
225
226
2/2
✓ Branch 1 taken 259 times.
✓ Branch 2 taken 518 times.
777 if (ff_outlink_frame_wanted(ctx->outputs[i])) {
227 259 ff_inlink_request_frame(inlink);
228 259 return 0;
229 }
230 }
231
232 259 return FFERROR_NOT_READY;
233 }
234
235 static const AVFilterPad avfilter_af_channelsplit_inputs[] = {
236 {
237 .name = "default",
238 .type = AVMEDIA_TYPE_AUDIO,
239 },
240 };
241
242 const AVFilter ff_af_channelsplit = {
243 .name = "channelsplit",
244 .description = NULL_IF_CONFIG_SMALL("Split audio into per-channel streams."),
245 .priv_size = sizeof(ChannelSplitContext),
246 .priv_class = &channelsplit_class,
247 .init = init,
248 .activate = activate,
249 .uninit = uninit,
250 FILTER_INPUTS(avfilter_af_channelsplit_inputs),
251 .outputs = NULL,
252 FILTER_QUERY_FUNC(query_formats),
253 .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
254 };
255