FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/af_channelsplit.c
Date: 2024-07-14 13:34:57
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 518 AVFilterContext *ctx = outlink->src;
155 518 ChannelSplitContext *s = ctx->priv;
156 518 const int i = FF_OUTLINK_IDX(outlink);
157 518 enum AVChannel channel = av_channel_layout_channel_from_index(&buf->ch_layout, s->map[i]);
158 int ret;
159
160 av_assert1(channel >= 0);
161
162 518 AVFrame *buf_out = av_frame_clone(buf);
163
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 518 times.
518 if (!buf_out)
164 return AVERROR(ENOMEM);
165
166 518 buf_out->data[0] = buf_out->extended_data[0] = buf_out->extended_data[s->map[i]];
167 518 ret = av_channel_layout_from_mask(&buf_out->ch_layout, 1ULL << channel);
168
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 518 times.
518 if (ret < 0) {
169 av_frame_free(&buf_out);
170 return ret;
171 }
172
173 518 return ff_filter_frame(ctx->outputs[i], buf_out);
174 }
175
176 519 static int activate(AVFilterContext *ctx)
177 {
178 519 AVFilterLink *inlink = ctx->inputs[0];
179 int status, ret;
180 AVFrame *in;
181 int64_t pts;
182
183
2/2
✓ Branch 0 taken 1038 times.
✓ Branch 1 taken 519 times.
1557 for (int i = 0; i < ctx->nb_outputs; i++) {
184
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);
185 }
186
187 519 ret = ff_inlink_consume_frame(inlink, &in);
188
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 519 times.
519 if (ret < 0)
189 return ret;
190
2/2
✓ Branch 0 taken 259 times.
✓ Branch 1 taken 260 times.
519 if (ret > 0) {
191
2/2
✓ Branch 0 taken 518 times.
✓ Branch 1 taken 259 times.
777 for (int i = 0; i < ctx->nb_outputs; i++) {
192
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 518 times.
518 if (ff_outlink_get_status(ctx->outputs[i]))
193 continue;
194
195 518 ret = filter_frame(ctx->outputs[i], in);
196
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 518 times.
518 if (ret < 0)
197 break;
198 }
199
200 259 av_frame_free(&in);
201
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 259 times.
259 if (ret < 0)
202 return ret;
203 }
204
205
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 518 times.
519 if (ff_inlink_acknowledge_status(inlink, &status, &pts)) {
206
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 for (int i = 0; i < ctx->nb_outputs; i++) {
207
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (ff_outlink_get_status(ctx->outputs[i]))
208 continue;
209 2 ff_outlink_set_status(ctx->outputs[i], status, pts);
210 }
211 1 return 0;
212 }
213
214
2/2
✓ Branch 0 taken 777 times.
✓ Branch 1 taken 259 times.
1036 for (int i = 0; i < ctx->nb_outputs; i++) {
215
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 777 times.
777 if (ff_outlink_get_status(ctx->outputs[i]))
216 continue;
217
218
2/2
✓ Branch 1 taken 259 times.
✓ Branch 2 taken 518 times.
777 if (ff_outlink_frame_wanted(ctx->outputs[i])) {
219 259 ff_inlink_request_frame(inlink);
220 259 return 0;
221 }
222 }
223
224 259 return FFERROR_NOT_READY;
225 }
226
227 const AVFilter ff_af_channelsplit = {
228 .name = "channelsplit",
229 .description = NULL_IF_CONFIG_SMALL("Split audio into per-channel streams."),
230 .priv_size = sizeof(ChannelSplitContext),
231 .priv_class = &channelsplit_class,
232 .init = init,
233 .activate = activate,
234 .uninit = uninit,
235 FILTER_INPUTS(ff_audio_default_filterpad),
236 .outputs = NULL,
237 FILTER_QUERY_FUNC(query_formats),
238 .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
239 };
240