FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/af_channelsplit.c
Date: 2024-07-26 21:54:09
Exec Total Coverage
Lines: 75 101 74.3%
Functions: 5 5 100.0%
Branches: 40 68 58.8%

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 #include "libavutil/avassert.h"
26 #include "libavutil/attributes.h"
27 #include "libavutil/channel_layout.h"
28 #include "libavutil/internal.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 #include "internal.h"
37
38 #define MAX_CH 64
39
40 typedef struct ChannelSplitContext {
41 const AVClass *class;
42
43 AVChannelLayout channel_layout;
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), AV_OPT_TYPE_CHLAYOUT, { .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 0 taken 2 times.
✗ Branch 1 not taken.
2 if (!strcmp(s->channels_str, "all")) {
67
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if ((ret = av_channel_layout_copy(&channel_layout, &s->channel_layout)) < 0)
68 goto fail;
69 2 all = 1;
70 } else {
71 if ((ret = av_channel_layout_from_string(&channel_layout, s->channels_str)) < 0)
72 goto fail;
73 }
74
75
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (channel_layout.nb_channels > MAX_CH) {
76 av_log(ctx, AV_LOG_ERROR, "Too many channels\n");
77 goto fail;
78 }
79
80
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 for (i = 0; i < channel_layout.nb_channels; i++) {
81 4 enum AVChannel channel = av_channel_layout_channel_from_index(&channel_layout, i);
82 char buf[64];
83 4 AVFilterPad pad = { .flags = AVFILTERPAD_FLAG_FREE_NAME };
84
85 4 av_channel_name(buf, sizeof(buf), channel);
86 4 pad.type = AVMEDIA_TYPE_AUDIO;
87 4 pad.name = av_strdup(buf);
88
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!pad.name) {
89 ret = AVERROR(ENOMEM);
90 goto fail;
91 }
92
93
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (all) {
94 4 s->map[i] = i;
95 } else {
96 char buf[128];
97 av_channel_layout_describe(&s->channel_layout, buf, sizeof(buf));
98 if ((ret = av_channel_layout_index_from_channel(&s->channel_layout, channel)) < 0) {
99 av_log(ctx, AV_LOG_ERROR, "Channel name '%s' not present in channel layout '%s'.\n",
100 pad.name, buf);
101 av_freep(&pad.name);
102 goto fail;
103 }
104
105 s->map[i] = ret;
106 }
107
108
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 if ((ret = ff_append_outpad(ctx, &pad)) < 0)
109 goto fail;
110 }
111
112 2 fail:
113 2 av_channel_layout_uninit(&channel_layout);
114 2 return ret;
115 }
116
117 2 static av_cold void uninit(AVFilterContext *ctx)
118 {
119 2 ChannelSplitContext *s = ctx->priv;
120
121 2 av_channel_layout_uninit(&s->channel_layout);
122 2 }
123
124 1 static int query_formats(AVFilterContext *ctx)
125 {
126 1 ChannelSplitContext *s = ctx->priv;
127 1 AVFilterChannelLayouts *in_layouts = NULL;
128 int i, ret;
129
130
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 ||
131 1 (ret = ff_set_common_all_samplerates(ctx)) < 0)
132 return ret;
133
134
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 ||
135 1 (ret = ff_channel_layouts_ref(in_layouts, &ctx->inputs[0]->outcfg.channel_layouts)) < 0)
136 return ret;
137
138
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 for (i = 0; i < ctx->nb_outputs; i++) {
139 2 AVChannelLayout channel_layout = { 0 };
140 2 AVFilterChannelLayouts *out_layouts = NULL;
141 2 enum AVChannel channel = av_channel_layout_channel_from_index(&s->channel_layout, s->map[i]);
142
143
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 ||
144
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
4 (ret = ff_add_channel_layout(&out_layouts, &channel_layout)) < 0 ||
145 2 (ret = ff_channel_layouts_ref(out_layouts, &ctx->outputs[i]->incfg.channel_layouts)) < 0)
146 return ret;
147 }
148
149 1 return 0;
150 }
151
152 518 static int filter_frame(AVFilterLink *outlink, AVFrame *buf)
153 {
154 AVFrame *buf_out;
155 518 AVFilterContext *ctx = outlink->src;
156 518 ChannelSplitContext *s = ctx->priv;
157 518 const int i = FF_OUTLINK_IDX(outlink);
158 518 enum AVChannel channel = av_channel_layout_channel_from_index(&buf->ch_layout, s->map[i]);
159 int ret;
160
161 av_assert1(channel >= 0);
162
163 518 buf_out = av_frame_clone(buf);
164
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 518 times.
518 if (!buf_out)
165 return AVERROR(ENOMEM);
166
167 518 buf_out->data[0] = buf_out->extended_data[0] = buf_out->extended_data[s->map[i]];
168 518 ret = av_channel_layout_from_mask(&buf_out->ch_layout, 1ULL << channel);
169
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 518 times.
518 if (ret < 0) {
170 av_frame_free(&buf_out);
171 return ret;
172 }
173
174 518 return ff_filter_frame(ctx->outputs[i], buf_out);
175 }
176
177 519 static int activate(AVFilterContext *ctx)
178 {
179 519 AVFilterLink *inlink = ctx->inputs[0];
180 int status, ret;
181 AVFrame *in;
182 int64_t pts;
183
184
2/2
✓ Branch 0 taken 1038 times.
✓ Branch 1 taken 519 times.
1557 for (int i = 0; i < ctx->nb_outputs; i++) {
185
1/4
✗ Branch 1 not taken.
✓ Branch 2 taken 1038 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
1038 FF_FILTER_FORWARD_STATUS_BACK_ALL(ctx->outputs[i], ctx);
186 }
187
188 519 ret = ff_inlink_consume_frame(inlink, &in);
189
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 519 times.
519 if (ret < 0)
190 return ret;
191
2/2
✓ Branch 0 taken 259 times.
✓ Branch 1 taken 260 times.
519 if (ret > 0) {
192
2/2
✓ Branch 0 taken 518 times.
✓ Branch 1 taken 259 times.
777 for (int i = 0; i < ctx->nb_outputs; i++) {
193
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 518 times.
518 if (ff_outlink_get_status(ctx->outputs[i]))
194 continue;
195
196 518 ret = filter_frame(ctx->outputs[i], in);
197
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 518 times.
518 if (ret < 0)
198 break;
199 }
200
201 259 av_frame_free(&in);
202
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 259 times.
259 if (ret < 0)
203 return ret;
204 }
205
206
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 518 times.
519 if (ff_inlink_acknowledge_status(inlink, &status, &pts)) {
207
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 for (int i = 0; i < ctx->nb_outputs; i++) {
208
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (ff_outlink_get_status(ctx->outputs[i]))
209 continue;
210 2 ff_outlink_set_status(ctx->outputs[i], status, pts);
211 }
212 1 return 0;
213 }
214
215
2/2
✓ Branch 0 taken 777 times.
✓ Branch 1 taken 259 times.
1036 for (int i = 0; i < ctx->nb_outputs; i++) {
216
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 777 times.
777 if (ff_outlink_get_status(ctx->outputs[i]))
217 continue;
218
219
2/2
✓ Branch 1 taken 259 times.
✓ Branch 2 taken 518 times.
777 if (ff_outlink_frame_wanted(ctx->outputs[i])) {
220 259 ff_inlink_request_frame(inlink);
221 259 return 0;
222 }
223 }
224
225 259 return FFERROR_NOT_READY;
226 }
227
228 const AVFilter ff_af_channelsplit = {
229 .name = "channelsplit",
230 .description = NULL_IF_CONFIG_SMALL("Split audio into per-channel streams."),
231 .priv_size = sizeof(ChannelSplitContext),
232 .priv_class = &channelsplit_class,
233 .init = init,
234 .activate = activate,
235 .uninit = uninit,
236 FILTER_INPUTS(ff_audio_default_filterpad),
237 .outputs = NULL,
238 FILTER_QUERY_FUNC(query_formats),
239 .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
240 };
241