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 
26 #include "libavutil/attributes.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 
44  char *channels_str;
45 
46  int map[64];
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 
61 {
62  ChannelSplitContext *s = ctx->priv;
63  AVChannelLayout channel_layout = { 0 };
64  int all = 0, ret = 0, i;
65 
66  if (!strcmp(s->channels_str, "all")) {
67  if ((ret = av_channel_layout_copy(&channel_layout, &s->channel_layout)) < 0)
68  goto fail;
69  all = 1;
70  } else {
71  if ((ret = av_channel_layout_from_string(&channel_layout, s->channels_str)) < 0)
72  goto fail;
73  }
74 
75  if (channel_layout.nb_channels > MAX_CH) {
76  av_log(ctx, AV_LOG_ERROR, "Too many channels\n");
77  goto fail;
78  }
79 
80  for (i = 0; i < channel_layout.nb_channels; i++) {
82  char buf[64];
84 
85  av_channel_name(buf, sizeof(buf), channel);
87  pad.name = av_strdup(buf);
88  if (!pad.name) {
89  ret = AVERROR(ENOMEM);
90  goto fail;
91  }
92 
93  if (all) {
94  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  if ((ret = ff_append_outpad(ctx, &pad)) < 0)
109  goto fail;
110  }
111 
112 fail:
113  av_channel_layout_uninit(&channel_layout);
114  return ret;
115 }
116 
118 {
119  ChannelSplitContext *s = ctx->priv;
120 
121  av_channel_layout_uninit(&s->channel_layout);
122 }
123 
125 {
126  ChannelSplitContext *s = ctx->priv;
127  AVFilterChannelLayouts *in_layouts = NULL;
128  int i, ret;
129 
132  return ret;
133 
134  if ((ret = ff_add_channel_layout(&in_layouts, &s->channel_layout)) < 0 ||
135  (ret = ff_channel_layouts_ref(in_layouts, &ctx->inputs[0]->outcfg.channel_layouts)) < 0)
136  return ret;
137 
138  for (i = 0; i < ctx->nb_outputs; i++) {
139  AVChannelLayout channel_layout = { 0 };
140  AVFilterChannelLayouts *out_layouts = NULL;
141  enum AVChannel channel = av_channel_layout_channel_from_index(&s->channel_layout, s->map[i]);
142 
143  if ((ret = av_channel_layout_from_mask(&channel_layout, 1ULL << channel)) < 0 ||
144  (ret = ff_add_channel_layout(&out_layouts, &channel_layout)) < 0 ||
145  (ret = ff_channel_layouts_ref(out_layouts, &ctx->outputs[i]->incfg.channel_layouts)) < 0)
146  return ret;
147  }
148 
149  return 0;
150 }
151 
152 static int filter_frame(AVFilterLink *outlink, AVFrame *buf)
153 {
154  AVFilterContext *ctx = outlink->src;
155  ChannelSplitContext *s = ctx->priv;
156  const int i = FF_OUTLINK_IDX(outlink);
158  int ret;
159 
160  AVFrame *buf_out = av_frame_clone(buf);
161  if (!buf_out)
162  return AVERROR(ENOMEM);
163 
164  buf_out->data[0] = buf_out->extended_data[0] = buf_out->extended_data[s->map[i]];
165  ret = av_channel_layout_from_mask(&buf_out->ch_layout, 1ULL << channel);
166  if (ret < 0) {
167  av_frame_free(&buf_out);
168  return ret;
169  }
170 
171  return ff_filter_frame(ctx->outputs[i], buf_out);
172 }
173 
175 {
176  AVFilterLink *inlink = ctx->inputs[0];
177  int status, ret;
178  AVFrame *in;
179  int64_t pts;
180 
181  for (int i = 0; i < ctx->nb_outputs; i++) {
183  }
184 
186  if (ret < 0)
187  return ret;
188  if (ret > 0) {
189  for (int i = 0; i < ctx->nb_outputs; i++) {
190  if (ff_outlink_get_status(ctx->outputs[i]))
191  continue;
192 
193  ret = filter_frame(ctx->outputs[i], in);
194  if (ret < 0)
195  break;
196  }
197 
198  av_frame_free(&in);
199  if (ret < 0)
200  return ret;
201  }
202 
204  for (int i = 0; i < ctx->nb_outputs; i++) {
205  if (ff_outlink_get_status(ctx->outputs[i]))
206  continue;
207  ff_outlink_set_status(ctx->outputs[i], status, pts);
208  }
209  return 0;
210  }
211 
212  for (int i = 0; i < ctx->nb_outputs; i++) {
213  if (ff_outlink_get_status(ctx->outputs[i]))
214  continue;
215 
216  if (ff_outlink_frame_wanted(ctx->outputs[i])) {
218  return 0;
219  }
220  }
221 
222  return FFERROR_NOT_READY;
223 }
224 
226  .name = "channelsplit",
227  .description = NULL_IF_CONFIG_SMALL("Split audio into per-channel streams."),
228  .priv_size = sizeof(ChannelSplitContext),
229  .priv_class = &channelsplit_class,
230  .init = init,
231  .activate = activate,
232  .uninit = uninit,
234  .outputs = NULL,
237 };
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:1015
ff_channel_layouts_ref
int ff_channel_layouts_ref(AVFilterChannelLayouts *f, AVFilterChannelLayouts **ref)
Add *ref as a new reference to f.
Definition: formats.c:674
FFERROR_NOT_READY
return FFERROR_NOT_READY
Definition: filter_design.txt:204
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:160
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:375
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:665
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:822
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:396
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:1442
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:776
pts
static int64_t pts
Definition: transcode_aac.c:644
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:33
F
#define F
Definition: af_channelsplit.c:51
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:50
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:868
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:645
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:1568
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:243
OFFSET
#define OFFSET(x)
Definition: af_channelsplit.c:49
filters.h
ctx
AVFormatContext * ctx
Definition: movenc.c:49
av_frame_clone
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:593
ChannelSplitContext
Definition: af_channelsplit.c:40
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:152
activate
static int activate(AVFilterContext *ctx)
Definition: af_channelsplit.c:174
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:522
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:1389
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:94
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:60
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:303
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
channelsplit_options
static const AVOption channelsplit_options[]
Definition: af_channelsplit.c:52
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:98
AVFrame::extended_data
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:436
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:594
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:117
AVFilterPad::type
enum AVMediaType type
AVFilterPad type.
Definition: internal.h:44
status
ov_status_e status
Definition: dnn_backend_openvino.c:121
channel_layout.h
ChannelSplitContext::channels_str
char * channels_str
Definition: af_channelsplit.c:44
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:705
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:433
ChannelSplitContext::channel_layout
AVChannelLayout channel_layout
Definition: af_channelsplit.c:43
ff_outlink_get_status
int ff_outlink_get_status(AVFilterLink *link)
Get the status on an output link.
Definition: avfilter.c:1593
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:440
ChannelSplitContext::map
int map[64]
Definition: af_channelsplit.c:46
av_strdup
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:272
mem.h
audio.h
ff_append_outpad
int ff_append_outpad(AVFilterContext *f, AVFilterPad *p)
Definition: avfilter.c:138
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:38
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:225
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:124
channel
channel
Definition: ebur128.h:39