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/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 
37 typedef struct ChannelSplitContext {
38  const AVClass *class;
39 
41  char *channels_str;
42 
43  int *map;
45 
46 #define OFFSET(x) offsetof(ChannelSplitContext, x)
47 #define A AV_OPT_FLAG_AUDIO_PARAM
48 #define F AV_OPT_FLAG_FILTERING_PARAM
49 static const AVOption channelsplit_options[] = {
50  { "channel_layout", "Input channel layout.", OFFSET(channel_layout), AV_OPT_TYPE_CHLAYOUT, { .str = "stereo" }, .flags = A|F },
51  { "channels", "Channels to extract.", OFFSET(channels_str), AV_OPT_TYPE_STRING, { .str = "all" }, .flags = A|F },
52  { NULL }
53 };
54 
55 AVFILTER_DEFINE_CLASS(channelsplit);
56 
58 {
59  ChannelSplitContext *s = ctx->priv;
60  AVChannelLayout channel_layout = { 0 };
61  int all = 0, ret = 0, i;
62 
63  if (!strcmp(s->channels_str, "all")) {
64  if ((ret = av_channel_layout_copy(&channel_layout, &s->channel_layout)) < 0)
65  goto fail;
66  all = 1;
67  } else {
68  if ((ret = av_channel_layout_from_string(&channel_layout, s->channels_str)) < 0)
69  goto fail;
70  }
71 
72  s->map = av_calloc(channel_layout.nb_channels, sizeof(*s->map));
73  if (!s->map)
74  return AVERROR(ENOMEM);
75 
76  for (i = 0; i < channel_layout.nb_channels; i++) {
78  char buf[64];
80 
81  av_channel_name(buf, sizeof(buf), channel);
83  pad.name = av_strdup(buf);
84  if (!pad.name) {
85  ret = AVERROR(ENOMEM);
86  goto fail;
87  }
88 
89  if (all) {
90  s->map[i] = i;
91  } else {
92  char buf[128];
93  av_channel_layout_describe(&s->channel_layout, buf, sizeof(buf));
94  if ((ret = av_channel_layout_index_from_channel(&s->channel_layout, channel)) < 0) {
95  av_log(ctx, AV_LOG_ERROR, "Channel name '%s' not present in channel layout '%s'.\n",
96  pad.name, buf);
97  av_freep(&pad.name);
98  goto fail;
99  }
100 
101  s->map[i] = ret;
102  }
103 
104  if ((ret = ff_append_outpad(ctx, &pad)) < 0)
105  goto fail;
106  }
107 
108 fail:
109  av_channel_layout_uninit(&channel_layout);
110  return ret;
111 }
112 
114 {
115  ChannelSplitContext *s = ctx->priv;
116 
117  av_channel_layout_uninit(&s->channel_layout);
118  av_freep(&s->map);
119 }
120 
122  AVFilterFormatsConfig **cfg_in,
123  AVFilterFormatsConfig **cfg_out)
124 {
125  ChannelSplitContext *s = ctx->priv;
126  AVFilterChannelLayouts *in_layouts = NULL;
127  int i, ret;
128 
129  ret = ff_set_common_formats2(ctx, cfg_in, cfg_out, ff_planar_sample_fmts());
130  if (ret < 0)
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, &cfg_in[0]->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  channel_layout.u.map = av_mallocz(sizeof(*channel_layout.u.map));
143  if (!channel_layout.u.map)
144  return AVERROR(ENOMEM);
145 
146  channel_layout.u.map[0].id = channel;
147  channel_layout.nb_channels = 1;
148  channel_layout.order = AV_CHANNEL_ORDER_CUSTOM;
149 
151  if (ret < 0) {
152  av_channel_layout_uninit(&channel_layout);
153  return ret;
154  }
155 
156  ret = ff_add_channel_layout(&out_layouts, &channel_layout);
157  av_channel_layout_uninit(&channel_layout);
158  if (ret < 0)
159  return ret;
160 
161  ret = ff_channel_layouts_ref(out_layouts, &cfg_out[i]->channel_layouts);
162  if (ret < 0)
163  return ret;
164  }
165 
166  return 0;
167 }
168 
169 static int filter_frame(AVFilterLink *outlink, AVFrame *buf)
170 {
171  AVFrame *buf_out;
172  AVFilterContext *ctx = outlink->src;
173  ChannelSplitContext *s = ctx->priv;
174  const int i = FF_OUTLINK_IDX(outlink);
175  int ret;
176 
177  buf_out = av_frame_clone(buf);
178  if (!buf_out)
179  return AVERROR(ENOMEM);
180 
181  buf_out->data[0] = buf_out->extended_data[0] = buf_out->extended_data[s->map[i]];
182 
184  ret = av_channel_layout_copy(&buf_out->ch_layout, &outlink->ch_layout);
185  if (ret < 0) {
186  av_frame_free(&buf_out);
187  return ret;
188  }
189 
190  return ff_filter_frame(ctx->outputs[i], buf_out);
191 }
192 
194 {
195  AVFilterLink *inlink = ctx->inputs[0];
196  int status, ret;
197  AVFrame *in;
198  int64_t pts;
199 
200  for (int i = 0; i < ctx->nb_outputs; i++) {
202  }
203 
205  if (ret < 0)
206  return ret;
207  if (ret > 0) {
208  for (int i = 0; i < ctx->nb_outputs; i++) {
209  if (ff_outlink_get_status(ctx->outputs[i]))
210  continue;
211 
212  ret = filter_frame(ctx->outputs[i], in);
213  if (ret < 0)
214  break;
215  }
216 
217  av_frame_free(&in);
218  if (ret < 0)
219  return ret;
220  }
221 
223  for (int i = 0; i < ctx->nb_outputs; i++) {
224  if (ff_outlink_get_status(ctx->outputs[i]))
225  continue;
226  ff_outlink_set_status(ctx->outputs[i], status, pts);
227  }
228  return 0;
229  }
230 
231  for (int i = 0; i < ctx->nb_outputs; i++) {
232  if (ff_outlink_get_status(ctx->outputs[i]))
233  continue;
234 
235  if (ff_outlink_frame_wanted(ctx->outputs[i])) {
237  return 0;
238  }
239  }
240 
241  return FFERROR_NOT_READY;
242 }
243 
245  .name = "channelsplit",
246  .description = NULL_IF_CONFIG_SMALL("Split audio into per-channel streams."),
247  .priv_size = sizeof(ChannelSplitContext),
248  .priv_class = &channelsplit_class,
249  .init = init,
250  .activate = activate,
251  .uninit = uninit,
253  .outputs = NULL,
256 };
AVChannelLayout::u
union AVChannelLayout::@427 u
Details about which channels are present in this layout.
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_OUTLINK_IDX
#define FF_OUTLINK_IDX(link)
Definition: filters.h:214
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1062
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
ff_set_common_formats2
int ff_set_common_formats2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, AVFilterFormats *formats)
Definition: formats.c:1007
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
AVChannelLayout::map
AVChannelCustom * map
This member must be used when the channel order is AV_CHANNEL_ORDER_CUSTOM.
Definition: channel_layout.h:368
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:162
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: filters.h:262
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
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:671
AVOption
AVOption.
Definition: opt.h:429
AVChannelLayout::order
enum AVChannelOrder order
Channel order used in this layout.
Definition: channel_layout.h:322
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:205
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:327
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:410
AVFILTERPAD_FLAG_FREE_NAME
#define AVFILTERPAD_FLAG_FREE_NAME
The pad's name is allocated and should be freed generically.
Definition: filters.h:62
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:1491
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:447
fail
#define fail()
Definition: checkasm.h:189
AVFrame::ch_layout
AVChannelLayout ch_layout
Channel layout of the audio data.
Definition: frame.h:790
pts
static int64_t pts
Definition: transcode_aac.c:644
AVFilterPad
A filter pad used for either input or output.
Definition: filters.h:38
F
#define F
Definition: af_channelsplit.c:48
avassert.h
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
av_cold
#define av_cold
Definition: attributes.h:90
A
#define A
Definition: af_channelsplit.c:47
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:651
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:424
ff_inlink_request_frame
void ff_inlink_request_frame(AVFilterLink *link)
Mark that a frame is wanted on the link.
Definition: avfilter.c:1594
s
#define s(width, name)
Definition: cbs_vp9.c:198
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
OFFSET
#define OFFSET(x)
Definition: af_channelsplit.c:46
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:597
ChannelSplitContext
Definition: af_channelsplit.c:37
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:75
NULL
#define NULL
Definition: coverity.c:32
filter_frame
static int filter_frame(AVFilterLink *outlink, AVFrame *buf)
Definition: af_channelsplit.c:169
ChannelSplitContext::map
int * map
Definition: af_channelsplit.c:43
AV_CHANNEL_LAYOUT_RETYPE_FLAG_CANONICAL
#define AV_CHANNEL_LAYOUT_RETYPE_FLAG_CANONICAL
The specified retype target order is ignored and the simplest possible (canonical) order is used for ...
Definition: channel_layout.h:717
activate
static int activate(AVFilterContext *ctx)
Definition: af_channelsplit.c:193
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:34
AV_OPT_TYPE_CHLAYOUT
@ AV_OPT_TYPE_CHLAYOUT
Underlying C type is AVChannelLayout.
Definition: opt.h:331
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:1438
AVFilterFormatsConfig
Lists of formats / etc.
Definition: avfilter.h:111
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:147
query_formats
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition: af_channelsplit.c:121
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:317
av_channel_layout_retype
int av_channel_layout_retype(AVChannelLayout *channel_layout, enum AVChannelOrder order, int flags)
Change the AVChannelOrder of a channel layout.
Definition: channel_layout.c:883
attributes.h
init
static av_cold int init(AVFilterContext *ctx)
Definition: af_channelsplit.c:57
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:310
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:49
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:104
AVFrame::extended_data
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:450
FILTER_QUERY_FUNC2
#define FILTER_QUERY_FUNC2(func)
Definition: filters.h:239
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:256
AVFilterPad::flags
int flags
A combination of AVFILTERPAD_FLAG_* flags.
Definition: filters.h:67
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: filters.h:44
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:264
AVFilter
Filter definition.
Definition: avfilter.h:201
ret
ret
Definition: filter_design.txt:187
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_channelsplit.c:113
AVFilterPad::type
enum AVMediaType type
AVFilterPad type.
Definition: filters.h:49
status
ov_status_e status
Definition: dnn_backend_openvino.c:100
AV_CHANNEL_ORDER_CUSTOM
@ AV_CHANNEL_ORDER_CUSTOM
The channel order does not correspond to any other predefined order and is stored as an explicit map.
Definition: channel_layout.h:132
channel_layout.h
ChannelSplitContext::channels_str
char * channels_str
Definition: af_channelsplit.c:41
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:711
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:440
ChannelSplitContext::channel_layout
AVChannelLayout channel_layout
Definition: af_channelsplit.c:40
ff_outlink_get_status
int ff_outlink_get_status(AVFilterLink *link)
Get the status on an output link.
Definition: avfilter.c:1619
AVFilterContext
An instance of a filter.
Definition: avfilter.h:457
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:447
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
channel_layouts
static const uint16_t channel_layouts[7]
Definition: dca_lbr.c:112
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
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
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
Definition: opt.h:276
ff_af_channelsplit
const AVFilter ff_af_channelsplit
Definition: af_channelsplit.c:244
AVChannelCustom::id
enum AVChannel id
Definition: channel_layout.h:282
channel
channel
Definition: ebur128.h:39