af_channelsplit.c
Go to the documentation of this file.
1 /*
2  * This file is part of Libav.
3  *
4  * Libav 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  * Libav 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 Libav; 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 
27 #include "libavutil/internal.h"
28 #include "libavutil/opt.h"
29 
30 #include "audio.h"
31 #include "avfilter.h"
32 #include "formats.h"
33 #include "internal.h"
34 
35 typedef struct ChannelSplitContext {
36  const AVClass *class;
37 
38  uint64_t channel_layout;
41 
42 #define OFFSET(x) offsetof(ChannelSplitContext, x)
43 #define A AV_OPT_FLAG_AUDIO_PARAM
44 #define F AV_OPT_FLAG_FILTERING_PARAM
45 static const AVOption channelsplit_options[] = {
46  { "channel_layout", "Input channel layout.", OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, { .str = "stereo" }, .flags = A|F },
47  { NULL },
48 };
49 
50 AVFILTER_DEFINE_CLASS(channelsplit);
51 
52 static int init(AVFilterContext *ctx)
53 {
54  ChannelSplitContext *s = ctx->priv;
55  int nb_channels;
56  int ret = 0, i;
57 
59  av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout '%s'.\n",
61  ret = AVERROR(EINVAL);
62  goto fail;
63  }
64 
66  for (i = 0; i < nb_channels; i++) {
67  uint64_t channel = av_channel_layout_extract_channel(s->channel_layout, i);
68  AVFilterPad pad = { 0 };
69 
71  pad.name = av_get_channel_name(channel);
72 
73  ff_insert_outpad(ctx, i, &pad);
74  }
75 
76 fail:
77  return ret;
78 }
79 
81 {
82  ChannelSplitContext *s = ctx->priv;
83  AVFilterChannelLayouts *in_layouts = NULL;
84  int i;
85 
88 
89  ff_add_channel_layout(&in_layouts, s->channel_layout);
90  ff_channel_layouts_ref(in_layouts, &ctx->inputs[0]->out_channel_layouts);
91 
92  for (i = 0; i < ctx->nb_outputs; i++) {
93  AVFilterChannelLayouts *out_layouts = NULL;
94  uint64_t channel = av_channel_layout_extract_channel(s->channel_layout, i);
95 
96  ff_add_channel_layout(&out_layouts, channel);
97  ff_channel_layouts_ref(out_layouts, &ctx->outputs[i]->in_channel_layouts);
98  }
99 
100  return 0;
101 }
102 
103 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
104 {
105  AVFilterContext *ctx = inlink->dst;
106  int i, ret = 0;
107 
108  for (i = 0; i < ctx->nb_outputs; i++) {
109  AVFrame *buf_out = av_frame_clone(buf);
110 
111  if (!buf_out) {
112  ret = AVERROR(ENOMEM);
113  break;
114  }
115 
116  buf_out->data[0] = buf_out->extended_data[0] = buf_out->extended_data[i];
117  buf_out->channel_layout =
119  av_frame_set_channels(buf_out, 1);
120 
121  ret = ff_filter_frame(ctx->outputs[i], buf_out);
122  if (ret < 0)
123  break;
124  }
125  av_frame_free(&buf);
126  return ret;
127 }
128 
130  {
131  .name = "default",
132  .type = AVMEDIA_TYPE_AUDIO,
133  .filter_frame = filter_frame,
134  },
135  { NULL }
136 };
137 
139  .name = "channelsplit",
140  .description = NULL_IF_CONFIG_SMALL("Split audio into per-channel streams."),
141  .priv_size = sizeof(ChannelSplitContext),
142  .priv_class = &channelsplit_class,
143 
144  .init = init,
146 
147  .inputs = avfilter_af_channelsplit_inputs,
148  .outputs = NULL,
149 
151 };
const char * s
Definition: avisynth_c.h:668
AVFilter avfilter_af_channelsplit
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
AVOption.
Definition: opt.h:251
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:117
external API header
enum AVMediaType type
AVFilterPad type.
const char * name
Pad name.
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:532
#define F
it can be given away to ff_start_frame *A reference passed to ff_filter_frame(or the deprecated ff_start_frame) is given away and must no longer be used.*A reference created with avfilter_ref_buffer belongs to the code that created it.*A reference obtained with ff_get_video_buffer or ff_get_audio_buffer belongs to the code that requested it.*A reference given as return value by the get_video_buffer or get_audio_buffer method is given away and must no longer be used.Link reference fields---------------------The AVFilterLink structure has a few AVFilterBufferRef fields.The cur_buf and out_buf were used with the deprecated start_frame/draw_slice/end_frame API and should no longer be used.src_buf
#define A
AVOptions.
uint64_t av_channel_layout_extract_channel(uint64_t channel_layout, int index)
Get the channel with the given index in channel_layout.
void ff_channel_layouts_ref(AVFilterChannelLayouts *f, AVFilterChannelLayouts **ref)
Add *ref as a new reference to f.
Definition: formats.c:427
#define AVFILTER_FLAG_DYNAMIC_OUTPUTS
The number of the filter outputs is not determined just by AVFilter.outputs.
Definition: avfilter.h:430
void 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:545
A filter pad used for either input or output.
AVFILTER_DEFINE_CLASS(channelsplit)
int ff_add_channel_layout(AVFilterChannelLayouts **l, uint64_t channel_layout)
Definition: formats.c:350
unsigned nb_outputs
number of output pads
Definition: avfilter.h:543
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
void * priv
private data for use by the filter
Definition: avfilter.h:545
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
void av_frame_set_channels(AVFrame *frame, int val)
static const AVFilterPad avfilter_af_channelsplit_inputs[]
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
const char * av_get_channel_name(uint64_t channel)
Get the name of a given channel.
uint64_t channel_layout
Channel layout of the audio data.
Definition: frame.h:331
common internal API header
#define OFFSET(x)
audio channel layout utility functions
ret
Definition: avfilter.c:821
static void ff_insert_outpad(AVFilterContext *f, unsigned index, AVFilterPad *p)
Insert a new output pad for the filter.
AVFilterFormats * ff_planar_sample_fmts(void)
Construct a formats list containing all planar sample formats.
Definition: formats.c:384
A list of supported channel layouts.
Definition: formats.h:85
struct ChannelSplitContext ChannelSplitContext
static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
AVFrame * av_frame_clone(AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:317
NULL
Definition: eval.c:55
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
static const AVOption channelsplit_options[]
void * buf
Definition: avisynth_c.h:594
Describe the class of an AVClass context structure.
Definition: log.h:50
Filter definition.
Definition: avfilter.h:436
synthesis window for stochastic i
const char * name
filter name
Definition: avfilter.h:437
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer 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.Buffer references ownership and permissions
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:539
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:396
static int flags
Definition: cpu.c:23
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:87
void ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:533
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:108
static int init(AVFilterContext *ctx)
static int query_formats(AVFilterContext *ctx)
uint64_t av_get_channel_layout(const char *name)
Return a channel layout id that matches name, or 0 if no match is found.
An instance of a filter.
Definition: avfilter.h:524
int nb_channels
internal API functions
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:117
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several inputs