FFmpeg coverage


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