FFmpeg
af_channelsplit.c
Go to the documentation of this file.
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"
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 
43  char *channels_str;
44 
45  int map[64];
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 
60 {
61  ChannelSplitContext *s = ctx->priv;
62  AVChannelLayout channel_layout = { 0 };
63  int all = 0, ret = 0, i;
64 
65  if (!strcmp(s->channels_str, "all")) {
66  if ((ret = av_channel_layout_copy(&channel_layout, &s->channel_layout)) < 0)
67  goto fail;
68  all = 1;
69  } else {
70  if ((ret = av_channel_layout_from_string(&channel_layout, s->channels_str)) < 0)
71  goto fail;
72  }
73 
74  if (channel_layout.nb_channels > MAX_CH) {
75  av_log(ctx, AV_LOG_ERROR, "Too many channels\n");
76  goto fail;
77  }
78 
79  for (i = 0; i < channel_layout.nb_channels; i++) {
81  char buf[64];
83 
84  av_channel_name(buf, sizeof(buf), channel);
86  pad.name = av_strdup(buf);
87  if (!pad.name) {
88  ret = AVERROR(ENOMEM);
89  goto fail;
90  }
91 
92  if (all) {
93  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  if ((ret = ff_append_outpad(ctx, &pad)) < 0)
108  goto fail;
109  }
110 
111 fail:
112  av_channel_layout_uninit(&channel_layout);
113  return ret;
114 }
115 
117 {
118  ChannelSplitContext *s = ctx->priv;
119 
120  av_channel_layout_uninit(&s->channel_layout);
121 }
122 
124 {
125  ChannelSplitContext *s = ctx->priv;
126  AVFilterChannelLayouts *in_layouts = NULL;
127  int i, ret;
128 
131  return ret;
132 
133  if ((ret = ff_add_channel_layout(&in_layouts, &s->channel_layout)) < 0 ||
134  (ret = ff_channel_layouts_ref(in_layouts, &ctx->inputs[0]->outcfg.channel_layouts)) < 0)
135  return ret;
136 
137  for (i = 0; i < ctx->nb_outputs; i++) {
138  AVChannelLayout channel_layout = { 0 };
139  AVFilterChannelLayouts *out_layouts = NULL;
140  enum AVChannel channel = av_channel_layout_channel_from_index(&s->channel_layout, s->map[i]);
141 
142  if ((ret = av_channel_layout_from_mask(&channel_layout, 1ULL << channel)) < 0 ||
143  (ret = ff_add_channel_layout(&out_layouts, &channel_layout)) < 0 ||
144  (ret = ff_channel_layouts_ref(out_layouts, &ctx->outputs[i]->incfg.channel_layouts)) < 0)
145  return ret;
146  }
147 
148  return 0;
149 }
150 
151 static int filter_frame(AVFilterLink *outlink, AVFrame *buf)
152 {
153  AVFilterContext *ctx = outlink->src;
154  ChannelSplitContext *s = ctx->priv;
155  const int i = FF_OUTLINK_IDX(outlink);
157  int ret;
158 
159  av_assert1(channel >= 0);
160 
161  AVFrame *buf_out = av_frame_clone(buf);
162  if (!buf_out)
163  return AVERROR(ENOMEM);
164 
165  buf_out->data[0] = buf_out->extended_data[0] = buf_out->extended_data[s->map[i]];
166  ret = av_channel_layout_from_mask(&buf_out->ch_layout, 1ULL << channel);
167  if (ret < 0)
168  return ret;
169 
170  return ff_filter_frame(ctx->outputs[i], buf_out);
171 }
172 
174 {
175  AVFilterLink *inlink = ctx->inputs[0];
176  int status, ret;
177  AVFrame *in;
178  int64_t pts;
179 
180  for (int i = 0; i < ctx->nb_outputs; i++) {
182  }
183 
185  if (ret < 0)
186  return ret;
187  if (ret > 0) {
188  for (int i = 0; i < ctx->nb_outputs; i++) {
189  if (ff_outlink_get_status(ctx->outputs[i]))
190  continue;
191 
192  ret = filter_frame(ctx->outputs[i], in);
193  if (ret < 0)
194  break;
195  }
196 
197  av_frame_free(&in);
198  if (ret < 0)
199  return ret;
200  }
201 
203  for (int i = 0; i < ctx->nb_outputs; i++) {
204  if (ff_outlink_get_status(ctx->outputs[i]))
205  continue;
206  ff_outlink_set_status(ctx->outputs[i], status, pts);
207  }
208  return 0;
209  }
210 
211  for (int i = 0; i < ctx->nb_outputs; i++) {
212  if (ff_outlink_get_status(ctx->outputs[i]))
213  continue;
214 
215  if (ff_outlink_frame_wanted(ctx->outputs[i])) {
217  return 0;
218  }
219  }
220 
221  return FFERROR_NOT_READY;
222 }
223 
225  .name = "channelsplit",
226  .description = NULL_IF_CONFIG_SMALL("Split audio into per-channel streams."),
227  .priv_size = sizeof(ChannelSplitContext),
228  .priv_class = &channelsplit_class,
229  .init = init,
230  .activate = activate,
231  .uninit = uninit,
233  .outputs = NULL,
236 };
AVFilterChannelLayouts
A list of supported channel layouts.
Definition: formats.h:85
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1018
ff_channel_layouts_ref
int ff_channel_layouts_ref(AVFilterChannelLayouts *f, AVFilterChannelLayouts **ref)
Add *ref as a new reference to f.
Definition: formats.c:673
FFERROR_NOT_READY
return FFERROR_NOT_READY
Definition: filter_design.txt:204
int64_t
long long int64_t
Definition: coverity.c:34
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:130
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:344
av_channel_layout_channel_from_index
enum AVChannel av_channel_layout_channel_from_index(const AVChannelLayout *channel_layout, unsigned int idx)
Get the channel with the given index in a channel layout.
Definition: channel_layout.c:664
AVOption
AVOption.
Definition: opt.h:346
FILTER_QUERY_FUNC
#define FILTER_QUERY_FUNC(func)
Definition: internal.h:159
ff_set_common_all_samplerates
int ff_set_common_all_samplerates(AVFilterContext *ctx)
Equivalent to ff_set_common_samplerates(ctx, ff_all_samplerates())
Definition: formats.c:821
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:170
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:313
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:365
formats.h
ff_inlink_consume_frame
int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe)
Take a frame from the link's FIFO and update the link's stats.
Definition: avfilter.c:1445
FF_FILTER_FORWARD_STATUS_BACK_ALL
#define FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, filter)
Forward the status on an output link to all input links.
Definition: filters.h:212
fail
#define fail()
Definition: checkasm.h:179
AVFrame::ch_layout
AVChannelLayout ch_layout
Channel layout of the audio data.
Definition: frame.h:745
pts
static int64_t pts
Definition: transcode_aac.c:643
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:33
F
#define F
Definition: af_channelsplit.c:50
avassert.h
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
A
#define A
Definition: af_channelsplit.c:49
ff_set_common_formats
int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:867
av_channel_layout_describe
int av_channel_layout_describe(const AVChannelLayout *channel_layout, char *buf, size_t buf_size)
Get a human-readable string describing the channel layout properties.
Definition: channel_layout.c:644
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(channelsplit)
ff_outlink_set_status
static void ff_outlink_set_status(AVFilterLink *link, int status, int64_t pts)
Set the status field of a link from the source filter.
Definition: filters.h:189
ff_inlink_request_frame
void ff_inlink_request_frame(AVFilterLink *link)
Mark that a frame is wanted on the link.
Definition: avfilter.c:1571
s
#define s(width, name)
Definition: cbs_vp9.c:198
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
FF_OUTLINK_IDX
#define FF_OUTLINK_IDX(link)
Definition: internal.h:332
av_channel_layout_from_mask
int av_channel_layout_from_mask(AVChannelLayout *channel_layout, uint64_t mask)
Initialize a native channel layout from a bitmask indicating which channels are present.
Definition: channel_layout.c:242
OFFSET
#define OFFSET(x)
Definition: af_channelsplit.c:48
filters.h
ctx
AVFormatContext * ctx
Definition: movenc.c:48
av_frame_clone
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:563
ChannelSplitContext
Definition: af_channelsplit.c:39
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:182
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
filter_frame
static int filter_frame(AVFilterLink *outlink, AVFrame *buf)
Definition: af_channelsplit.c:151
activate
static int activate(AVFilterContext *ctx)
Definition: af_channelsplit.c:173
ff_audio_default_filterpad
const AVFilterPad ff_audio_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_AUDIO.
Definition: audio.c:33
AV_OPT_TYPE_CHLAYOUT
@ AV_OPT_TYPE_CHLAYOUT
Definition: opt.h:252
ff_add_channel_layout
int ff_add_channel_layout(AVFilterChannelLayouts **l, const AVChannelLayout *channel_layout)
Definition: formats.c:521
ff_inlink_acknowledge_status
int ff_inlink_acknowledge_status(AVFilterLink *link, int *rstatus, int64_t *rpts)
Test and acknowledge the change of status on the link.
Definition: avfilter.c:1392
AVFILTER_FLAG_DYNAMIC_OUTPUTS
#define AVFILTER_FLAG_DYNAMIC_OUTPUTS
The number of the filter outputs is not determined just by AVFilter.outputs.
Definition: avfilter.h:112
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:106
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:303
attributes.h
init
static av_cold int init(AVFilterContext *ctx)
Definition: af_channelsplit.c:59
internal.h
AVChannel
AVChannel
Definition: channel_layout.h:47
av_channel_layout_from_string
int av_channel_layout_from_string(AVChannelLayout *channel_layout, const char *str)
Initialize a channel layout from a given string description.
Definition: channel_layout.c:302
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
channelsplit_options
static const AVOption channelsplit_options[]
Definition: af_channelsplit.c:51
internal.h
av_channel_name
int av_channel_name(char *buf, size_t buf_size, enum AVChannel channel_id)
Get a human readable string in an abbreviated form describing a given channel.
Definition: channel_layout.c:97
AVFrame::extended_data
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:405
av_assert1
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:56
AVFilterPad::flags
int flags
A combination of AVFILTERPAD_FLAG_* flags.
Definition: internal.h:62
ff_planar_sample_fmts
AVFilterFormats * ff_planar_sample_fmts(void)
Construct a formats list containing all planar sample formats.
Definition: formats.c:593
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:39
AVFilter
Filter definition.
Definition: avfilter.h:166
ret
ret
Definition: filter_design.txt:187
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_channelsplit.c:116
AVFilterPad::type
enum AVMediaType type
AVFilterPad type.
Definition: internal.h:44
status
ov_status_e status
Definition: dnn_backend_openvino.c:120
channel_layout.h
ChannelSplitContext::channels_str
char * channels_str
Definition: af_channelsplit.c:43
av_channel_layout_index_from_channel
int av_channel_layout_index_from_channel(const AVChannelLayout *channel_layout, enum AVChannel channel)
Get the index of a given channel in a channel layout.
Definition: channel_layout.c:704
avfilter.h
av_channel_layout_uninit
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
Definition: channel_layout.c:432
ChannelSplitContext::channel_layout
AVChannelLayout channel_layout
Definition: af_channelsplit.c:42
ff_outlink_get_status
int ff_outlink_get_status(AVFilterLink *link)
Get the status on an output link.
Definition: avfilter.c:1596
AVFilterContext
An instance of a filter.
Definition: avfilter.h:407
av_channel_layout_copy
int av_channel_layout_copy(AVChannelLayout *dst, const AVChannelLayout *src)
Make a copy of a channel layout.
Definition: channel_layout.c:439
ChannelSplitContext::map
int map[64]
Definition: af_channelsplit.c:45
av_strdup
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:270
audio.h
ff_append_outpad
int ff_append_outpad(AVFilterContext *f, AVFilterPad *p)
Definition: avfilter.c:137
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
MAX_CH
#define MAX_CH
Definition: af_channelsplit.c:37
ff_outlink_frame_wanted
the definition of that something depends on the semantic of the filter The callback must examine the status of the filter s links and proceed accordingly The status of output links is stored in the status_in and status_out fields and tested by the ff_outlink_frame_wanted() function. If this function returns true
AV_OPT_TYPE_STRING
@ AV_OPT_TYPE_STRING
Definition: opt.h:239
ff_af_channelsplit
const AVFilter ff_af_channelsplit
Definition: af_channelsplit.c:224
AVFILTERPAD_FLAG_FREE_NAME
#define AVFILTERPAD_FLAG_FREE_NAME
The pad's name is allocated and should be freed generically.
Definition: internal.h:57
query_formats
static int query_formats(AVFilterContext *ctx)
Definition: af_channelsplit.c:123
channel
channel
Definition: ebur128.h:39