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 "formats.h"
34 #include "internal.h"
35 
36 typedef struct ChannelSplitContext {
37  const AVClass *class;
38 
39  uint64_t channel_layout;
41  char *channels_str;
42 
43  int map[64];
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_str), AV_OPT_TYPE_STRING, { .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  uint64_t channel_layout;
61  int nb_channels;
62  int all = 0, ret = 0, i;
63 
64  if (!(s->channel_layout = av_get_channel_layout(s->channel_layout_str))) {
65  av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout '%s'.\n",
66  s->channel_layout_str);
67  ret = AVERROR(EINVAL);
68  goto fail;
69  }
70 
71 
72  if (!strcmp(s->channels_str, "all")) {
74  channel_layout = s->channel_layout;
75  all = 1;
76  } else {
77  if ((ret = av_get_extended_channel_layout(s->channels_str, &channel_layout, &nb_channels)) < 0)
78  return ret;
79  }
80 
81  for (i = 0; i < nb_channels; i++) {
82  uint64_t channel = av_channel_layout_extract_channel(channel_layout, i);
83  AVFilterPad pad = { 0 };
84 
87 
88  if (all) {
89  s->map[i] = i;
90  } else {
91  if ((ret = av_get_channel_layout_channel_index(s->channel_layout, channel)) < 0) {
92  av_log(ctx, AV_LOG_ERROR, "Channel name '%s' not present in channel layout '%s'.\n",
93  av_get_channel_name(channel), s->channel_layout_str);
94  return ret;
95  }
96 
97  s->map[i] = ret;
98  }
99 
100  if ((ret = ff_append_outpad(ctx, &pad)) < 0)
101  return ret;
102  }
103 
104 fail:
105  return ret;
106 }
107 
109 {
110  ChannelSplitContext *s = ctx->priv;
111  AVFilterChannelLayouts *in_layouts = NULL;
112  int i, ret;
113 
116  return ret;
117 
118  if ((ret = ff_add_channel_layout(&in_layouts, s->channel_layout)) < 0 ||
119  (ret = ff_channel_layouts_ref(in_layouts, &ctx->inputs[0]->outcfg.channel_layouts)) < 0)
120  return ret;
121 
122  for (i = 0; i < ctx->nb_outputs; i++) {
123  AVFilterChannelLayouts *out_layouts = NULL;
124  uint64_t channel = av_channel_layout_extract_channel(s->channel_layout, s->map[i]);
125 
126  if ((ret = ff_add_channel_layout(&out_layouts, channel)) < 0 ||
127  (ret = ff_channel_layouts_ref(out_layouts, &ctx->outputs[i]->incfg.channel_layouts)) < 0)
128  return ret;
129  }
130 
131  return 0;
132 }
133 
135 {
136  AVFilterContext *ctx = inlink->dst;
137  ChannelSplitContext *s = ctx->priv;
138  int i, ret = 0;
139 
140  for (i = 0; i < ctx->nb_outputs; i++) {
141  AVFrame *buf_out = av_frame_clone(buf);
142 
143  if (!buf_out) {
144  ret = AVERROR(ENOMEM);
145  break;
146  }
147 
148  buf_out->data[0] = buf_out->extended_data[0] = buf_out->extended_data[s->map[i]];
149  buf_out->channel_layout =
151  buf_out->channels = 1;
152 
153  ret = ff_filter_frame(ctx->outputs[i], buf_out);
154  if (ret < 0)
155  break;
156  }
157  av_frame_free(&buf);
158  return ret;
159 }
160 
162  {
163  .name = "default",
164  .type = AVMEDIA_TYPE_AUDIO,
165  .filter_frame = filter_frame,
166  },
167 };
168 
170  .name = "channelsplit",
171  .description = NULL_IF_CONFIG_SMALL("Split audio into per-channel streams."),
172  .priv_size = sizeof(ChannelSplitContext),
173  .priv_class = &channelsplit_class,
174  .init = init,
176  .outputs = NULL,
179 };
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:550
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:109
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:317
AVOption
AVOption.
Definition: opt.h:247
FILTER_QUERY_FUNC
#define FILTER_QUERY_FUNC(func)
Definition: internal.h:168
ChannelSplitContext::channel_layout_str
char * channel_layout_str
Definition: af_channelsplit.c:40
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:689
av_get_channel_layout
uint64_t av_get_channel_layout(const char *name)
Return a channel layout id that matches name, or 0 if no match is found.
Definition: channel_layout.c:145
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:169
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:338
formats.h
fail
#define fail()
Definition: checkasm.h:127
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:50
F
#define F
Definition: af_channelsplit.c:48
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:47
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:699
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(channelsplit)
ff_add_channel_layout
int ff_add_channel_layout(AVFilterChannelLayouts **l, uint64_t channel_layout)
Definition: formats.c:426
s
#define s(width, name)
Definition: cbs_vp9.c:257
AVFrame::channels
int channels
number of audio channels, only used for audio.
Definition: frame.h:628
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
OFFSET
#define OFFSET(x)
Definition: af_channelsplit.c:46
av_get_channel_name
const char * av_get_channel_name(uint64_t channel)
Get the name of a given channel.
Definition: channel_layout.c:249
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:422
ChannelSplitContext
Definition: af_channelsplit.c:36
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:191
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:161
av_get_channel_layout_nb_channels
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
Definition: channel_layout.c:226
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:116
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
AVFrame::channel_layout
uint64_t channel_layout
Channel layout of the audio data.
Definition: frame.h:499
attributes.h
init
static av_cold int init(AVFilterContext *ctx)
Definition: af_channelsplit.c:57
internal.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
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:271
channelsplit_options
static const AVOption channelsplit_options[]
Definition: af_channelsplit.c:49
internal.h
AVFrame::extended_data
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:378
av_get_channel_layout_channel_index
int av_get_channel_layout_channel_index(uint64_t channel_layout, uint64_t channel)
Get the index of a channel in channel_layout.
Definition: channel_layout.c:239
ff_planar_sample_fmts
AVFilterFormats * ff_planar_sample_fmts(void)
Construct a formats list containing all planar sample formats.
Definition: formats.c:497
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:56
AVFilter
Filter definition.
Definition: avfilter.h:165
ret
ret
Definition: filter_design.txt:187
AVFilterPad::type
enum AVMediaType type
AVFilterPad type.
Definition: internal.h:61
channel_layout.h
ChannelSplitContext::channels_str
char * channels_str
Definition: af_channelsplit.c:41
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
Definition: af_channelsplit.c:134
avfilter.h
av_get_extended_channel_layout
int av_get_extended_channel_layout(const char *name, uint64_t *channel_layout, int *nb_channels)
Return a channel layout and the number of channels based on the specified name.
Definition: channel_layout.c:161
AVFilterContext
An instance of a filter.
Definition: avfilter.h:402
ChannelSplitContext::map
int map[64]
Definition: af_channelsplit.c:43
audio.h
ff_append_outpad
int ff_append_outpad(AVFilterContext *f, AVFilterPad *p)
Definition: avfilter.c:150
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AV_OPT_TYPE_STRING
@ AV_OPT_TYPE_STRING
Definition: opt.h:228
ff_af_channelsplit
const AVFilter ff_af_channelsplit
Definition: af_channelsplit.c:169
query_formats
static int query_formats(AVFilterContext *ctx)
Definition: af_channelsplit.c:108
ChannelSplitContext::channel_layout
uint64_t channel_layout
Definition: af_channelsplit.c:39
channel
channel
Definition: ebur128.h:39
nb_channels
int nb_channels
Definition: channel_layout.c:81