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/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 
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_str), AV_OPT_TYPE_STRING, { .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 ((ret = av_channel_layout_from_string(&s->channel_layout, s->channel_layout_str)) < 0) {
67  av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout '%s'.\n",
68  s->channel_layout_str);
69  ret = AVERROR(EINVAL);
70  goto fail;
71  }
72 
73  if (!strcmp(s->channels_str, "all")) {
74  if ((ret = av_channel_layout_copy(&channel_layout, &s->channel_layout)) < 0)
75  goto fail;
76  all = 1;
77  } else {
78  if ((ret = av_channel_layout_from_string(&channel_layout, s->channels_str)) < 0)
79  goto fail;
80  }
81 
82  if (channel_layout.nb_channels > MAX_CH) {
83  av_log(ctx, AV_LOG_ERROR, "Too many channels\n");
84  goto fail;
85  }
86 
87  for (i = 0; i < channel_layout.nb_channels; i++) {
89  char buf[64];
91 
92  av_channel_name(buf, sizeof(buf), channel);
94  pad.name = av_strdup(buf);
95  if (!pad.name) {
96  ret = AVERROR(ENOMEM);
97  goto fail;
98  }
99 
100  if (all) {
101  s->map[i] = i;
102  } else {
103  if ((ret = av_channel_layout_index_from_channel(&s->channel_layout, channel)) < 0) {
104  av_log(ctx, AV_LOG_ERROR, "Channel name '%s' not present in channel layout '%s'.\n",
105  pad.name, s->channel_layout_str);
106  av_freep(&pad.name);
107  goto fail;
108  }
109 
110  s->map[i] = ret;
111  }
112 
113  if ((ret = ff_append_outpad(ctx, &pad)) < 0)
114  goto fail;
115  }
116 
117 fail:
118  av_channel_layout_uninit(&channel_layout);
119  return ret;
120 }
121 
123 {
124  ChannelSplitContext *s = ctx->priv;
125 
126  av_channel_layout_uninit(&s->channel_layout);
127 }
128 
130 {
131  ChannelSplitContext *s = ctx->priv;
132  AVFilterChannelLayouts *in_layouts = NULL;
133  int i, ret;
134 
137  return ret;
138 
139  if ((ret = ff_add_channel_layout(&in_layouts, &s->channel_layout)) < 0 ||
140  (ret = ff_channel_layouts_ref(in_layouts, &ctx->inputs[0]->outcfg.channel_layouts)) < 0)
141  return ret;
142 
143  for (i = 0; i < ctx->nb_outputs; i++) {
144  AVChannelLayout channel_layout = { 0 };
145  AVFilterChannelLayouts *out_layouts = NULL;
146  enum AVChannel channel = av_channel_layout_channel_from_index(&s->channel_layout, s->map[i]);
147 
148  if ((ret = av_channel_layout_from_mask(&channel_layout, 1ULL << channel)) < 0 ||
149  (ret = ff_add_channel_layout(&out_layouts, &channel_layout)) < 0 ||
150  (ret = ff_channel_layouts_ref(out_layouts, &ctx->outputs[i]->incfg.channel_layouts)) < 0)
151  return ret;
152  }
153 
154  return 0;
155 }
156 
157 static int filter_frame(AVFilterLink *outlink, AVFrame *buf)
158 {
159  AVFilterContext *ctx = outlink->src;
160  ChannelSplitContext *s = ctx->priv;
161  const int i = FF_OUTLINK_IDX(outlink);
163  int ret;
164 
165  AVFrame *buf_out = av_frame_clone(buf);
166  if (!buf_out)
167  return AVERROR(ENOMEM);
168 
169  buf_out->data[0] = buf_out->extended_data[0] = buf_out->extended_data[s->map[i]];
170  ret = av_channel_layout_from_mask(&buf_out->ch_layout, 1ULL << channel);
171  if (ret < 0)
172  return ret;
173 #if FF_API_OLD_CHANNEL_LAYOUT
175  buf_out->channel_layout =
177  buf_out->channels = 1;
179 #endif
180 
181  return ff_filter_frame(ctx->outputs[i], buf_out);
182 }
183 
185 {
186  AVFilterLink *inlink = ctx->inputs[0];
187  int status, ret;
188  AVFrame *in;
189  int64_t pts;
190 
191  for (int i = 0; i < ctx->nb_outputs; i++) {
193  }
194 
196  if (ret < 0)
197  return ret;
198  if (ret > 0) {
199  for (int i = 0; i < ctx->nb_outputs; i++) {
200  if (ff_outlink_get_status(ctx->outputs[i]))
201  continue;
202 
203  ret = filter_frame(ctx->outputs[i], in);
204  if (ret < 0)
205  break;
206  }
207 
208  av_frame_free(&in);
209  if (ret < 0)
210  return ret;
211  }
212 
214  for (int i = 0; i < ctx->nb_outputs; i++) {
215  if (ff_outlink_get_status(ctx->outputs[i]))
216  continue;
217  ff_outlink_set_status(ctx->outputs[i], status, pts);
218  }
219  return 0;
220  }
221 
222  for (int i = 0; i < ctx->nb_outputs; i++) {
223  if (ff_outlink_get_status(ctx->outputs[i]))
224  continue;
225 
226  if (ff_outlink_frame_wanted(ctx->outputs[i])) {
228  return 0;
229  }
230  }
231 
232  return FFERROR_NOT_READY;
233 }
234 
236  {
237  .name = "default",
238  .type = AVMEDIA_TYPE_AUDIO,
239  },
240 };
241 
243  .name = "channelsplit",
244  .description = NULL_IF_CONFIG_SMALL("Split audio into per-channel streams."),
245  .priv_size = sizeof(ChannelSplitContext),
246  .priv_class = &channelsplit_class,
247  .init = init,
248  .activate = activate,
249  .uninit = uninit,
251  .outputs = NULL,
254 };
FF_ENABLE_DEPRECATION_WARNINGS
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:83
AVFilterChannelLayouts
A list of supported channel layouts.
Definition: formats.h:85
status
they must not be accessed directly The fifo field contains the frames that are queued in the input for processing by the filter The status_in and status_out fields contains the queued status(EOF or error) of the link
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:999
ff_channel_layouts_ref
int ff_channel_layouts_ref(AVFilterChannelLayouts *f, AVFilterChannelLayouts **ref)
Add *ref as a new reference to f.
Definition: formats.c:591
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:111
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:325
AVOption
AVOption.
Definition: opt.h:251
FILTER_QUERY_FUNC
#define FILTER_QUERY_FUNC(func)
Definition: internal.h:167
ChannelSplitContext::channel_layout_str
char * channel_layout_str
Definition: af_channelsplit.c:43
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:739
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:175
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:300
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:346
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:637
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:1394
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
AVChannel
AVChannel
Definition: channel_layout.h:41
fail
#define fail()
Definition: checkasm.h:131
AVFrame::ch_layout
AVChannelLayout ch_layout
Channel layout of the audio data.
Definition: frame.h:704
pts
static int64_t pts
Definition: transcode_aac.c:654
AVFrame::channels
attribute_deprecated int channels
number of audio channels, only used for audio.
Definition: frame.h:643
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:49
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
AVFrame::channel_layout
attribute_deprecated uint64_t channel_layout
Channel layout of the audio data.
Definition: frame.h:510
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:749
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:1511
s
#define s(width, name)
Definition: cbs_vp9.c:256
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
FF_OUTLINK_IDX
#define FF_OUTLINK_IDX(link)
Definition: internal.h:334
OFFSET
#define OFFSET(x)
Definition: af_channelsplit.c:49
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:464
ChannelSplitContext
Definition: af_channelsplit.c:39
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:190
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
avfilter_af_channelsplit_inputs
static const AVFilterPad avfilter_af_channelsplit_inputs[]
Definition: af_channelsplit.c:235
filter_frame
static int filter_frame(AVFilterLink *outlink, AVFrame *buf)
Definition: af_channelsplit.c:157
activate
static int activate(AVFilterContext *ctx)
Definition: af_channelsplit.c:184
ff_add_channel_layout
int ff_add_channel_layout(AVFilterChannelLayouts **l, const AVChannelLayout *channel_layout)
Definition: formats.c:466
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:1348
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:630
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:122
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:117
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:290
attributes.h
init
static av_cold int init(AVFilterContext *ctx)
Definition: af_channelsplit.c:60
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:101
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
channelsplit_options
static const AVOption channelsplit_options[]
Definition: af_channelsplit.c:52
internal.h
AVFrame::extended_data
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:386
AVFilterPad::flags
int flags
A combination of AVFILTERPAD_FLAG_* flags.
Definition: internal.h:78
ff_planar_sample_fmts
AVFilterFormats * ff_planar_sample_fmts(void)
Construct a formats list containing all planar sample formats.
Definition: formats.c:538
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:55
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:834
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:794
AVFilter
Filter definition.
Definition: avfilter.h:171
av_channel_layout_from_mask
FF_ENABLE_DEPRECATION_WARNINGS 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:389
ret
ret
Definition: filter_design.txt:187
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_channelsplit.c:122
AVFilterPad::type
enum AVMediaType type
AVFilterPad type.
Definition: internal.h:60
channel_layout.h
ChannelSplitContext::channels_str
char * channels_str
Definition: af_channelsplit.c:44
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:402
avfilter.h
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:1534
AVFilterContext
An instance of a filter.
Definition: avfilter.h:408
ChannelSplitContext::map
int map[64]
Definition: af_channelsplit.c:46
FF_DISABLE_DEPRECATION_WARNINGS
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:82
av_strdup
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:280
audio.h
ff_append_outpad
int ff_append_outpad(AVFilterContext *f, AVFilterPad *p)
Definition: avfilter.c:129
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:229
ff_af_channelsplit
const AVFilter ff_af_channelsplit
Definition: af_channelsplit.c:242
AVFILTERPAD_FLAG_FREE_NAME
#define AVFILTERPAD_FLAG_FREE_NAME
The pad's name is allocated and should be freed generically.
Definition: internal.h:73
query_formats
static int query_formats(AVFilterContext *ctx)
Definition: af_channelsplit.c:129
av_channel_layout_extract_channel
uint64_t av_channel_layout_extract_channel(uint64_t channel_layout, int index)
Get the channel with the given index in channel_layout.
Definition: channel_layout.c:363
channel
channel
Definition: ebur128.h:39