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 
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 static const AVOption options[] = {
45  { "channel_layout", "Input channel layout.", OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, { .str = "stereo" }, .flags = A },
46  { NULL },
47 };
48 
49 static const AVClass channelsplit_class = {
50  .class_name = "channelsplit filter",
51  .item_name = av_default_item_name,
52  .option = options,
53  .version = LIBAVUTIL_VERSION_INT,
54 };
55 
56 static int init(AVFilterContext *ctx, const char *arg)
57 {
58  ChannelSplitContext *s = ctx->priv;
59  int nb_channels;
60  int ret = 0, i;
61 
64  if ((ret = av_set_options_string(s, arg, "=", ":")) < 0) {
65  av_log(ctx, AV_LOG_ERROR, "Error parsing options string '%s'.\n", arg);
66  return ret;
67  }
69  av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout '%s'.\n",
71  ret = AVERROR(EINVAL);
72  goto fail;
73  }
74 
76  for (i = 0; i < nb_channels; i++) {
77  uint64_t channel = av_channel_layout_extract_channel(s->channel_layout, i);
78  AVFilterPad pad = { 0 };
79 
81  pad.name = av_get_channel_name(channel);
82 
83  ff_insert_outpad(ctx, i, &pad);
84  }
85 
86 fail:
87  av_opt_free(s);
88  return ret;
89 }
90 
92 {
93  ChannelSplitContext *s = ctx->priv;
94  AVFilterChannelLayouts *in_layouts = NULL;
95  int i;
96 
99 
100  ff_add_channel_layout(&in_layouts, s->channel_layout);
101  ff_channel_layouts_ref(in_layouts, &ctx->inputs[0]->out_channel_layouts);
102 
103  for (i = 0; i < ctx->nb_outputs; i++) {
104  AVFilterChannelLayouts *out_layouts = NULL;
105  uint64_t channel = av_channel_layout_extract_channel(s->channel_layout, i);
106 
107  ff_add_channel_layout(&out_layouts, channel);
108  ff_channel_layouts_ref(out_layouts, &ctx->outputs[i]->in_channel_layouts);
109  }
110 
111  return 0;
112 }
113 
114 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *buf)
115 {
116  AVFilterContext *ctx = inlink->dst;
117  int i, ret = 0;
118 
119  for (i = 0; i < ctx->nb_outputs; i++) {
121 
122  if (!buf_out) {
123  ret = AVERROR(ENOMEM);
124  break;
125  }
126 
127  buf_out->data[0] = buf_out->extended_data[0] = buf_out->extended_data[i];
128  buf_out->audio->channel_layout =
130 
131  ret = ff_filter_frame(ctx->outputs[i], buf_out);
132  if (ret < 0)
133  break;
134  }
136  return ret;
137 }
138 
140  {
141  .name = "default",
142  .type = AVMEDIA_TYPE_AUDIO,
143  .filter_frame = filter_frame,
144  },
145  { NULL }
146 };
147 
149  .name = "channelsplit",
150  .description = NULL_IF_CONFIG_SMALL("Split audio into per-channel streams"),
151  .priv_size = sizeof(ChannelSplitContext),
152 
153  .init = init,
155 
156  .inputs = avfilter_af_channelsplit_inputs,
157  .outputs = NULL,
158 };
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: avfilter.h:156
static int init(AVFilterContext *ctx, const char *arg)
AVFilter avfilter_af_channelsplit
const AVClass * class
AVOption.
Definition: opt.h:233
AVFilterBufferRefAudioProps * audio
audio buffer specific properties
Definition: avfilter.h:160
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:122
void av_opt_set_defaults(void *s)
Set the values of all AVOption fields to their default values.
Definition: opt.c:505
int ff_filter_frame(AVFilterLink *link, AVFilterBufferRef *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:459
int av_set_options_string(void *ctx, const char *opts, const char *key_val_sep, const char *pairs_sep)
Parse the key/value pairs list in opts.
Definition: opt.c:587
enum AVMediaType type
AVFilterPad type.
Definition: internal.h:44
void avfilter_unref_buffer(AVFilterBufferRef *ref)
Remove a reference to a buffer.
Definition: buffer.c:75
const char * name
Pad name.
Definition: internal.h:39
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:38
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:426
#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.
static const AVClass channelsplit_class
void ff_channel_layouts_ref(AVFilterChannelLayouts *f, AVFilterChannelLayouts **ref)
Add *ref as a new reference to f.
Definition: formats.c:257
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:375
A filter pad used for either input or output.
Definition: internal.h:33
int ff_add_channel_layout(AVFilterChannelLayouts **l, uint64_t channel_layout)
Definition: formats.c:204
unsigned nb_outputs
number of output pads
Definition: avfilter.h:437
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
void * priv
private data for use by the filter
Definition: avfilter.h:439
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
static const AVFilterPad avfilter_af_channelsplit_inputs[]
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
const char * av_get_channel_name(uint64_t channel)
Get the name of a given channel.
AVFilterBufferRef * avfilter_ref_buffer(AVFilterBufferRef *ref, int pmask)
Add a new reference to a buffer.
Definition: buffer.c:35
common internal API header
#define OFFSET(x)
audio channel layout utility functions
static void ff_insert_outpad(AVFilterContext *f, unsigned index, AVFilterPad *p)
Insert a new output pad for the filter.
Definition: internal.h:180
static const AVOption options[]
AVFilterFormats * ff_planar_sample_fmts(void)
Construct a formats list containing all planar sample formats.
Definition: formats.c:226
LIBAVUTIL_VERSION_INT
Definition: eval.c:52
struct ChannelSplitContext ChannelSplitContext
A reference to an AVFilterBuffer.
Definition: avfilter.h:139
NULL
Definition: eval.c:52
av_default_item_name
Definition: dnxhdenc.c:43
Describe the class of an AVClass context structure.
Definition: log.h:33
Filter definition.
Definition: avfilter.h:371
static const AVFilterPad inputs[]
Definition: af_ashowinfo.c:110
const char * name
filter name
Definition: avfilter.h:372
uint64_t channel_layout
channel layout of audio buffer
Definition: avfilter.h:110
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:433
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:238
void ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:363
void av_opt_free(void *obj)
Free all string and binary options in obj.
Definition: opt.c:607
static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *buf)
#define AV_PERM_WRITE
can write to the buffer
Definition: avfilter.h:98
static int query_formats(AVFilterContext *ctx)
uint8_t * data[8]
picture/audio data for each plane
Definition: avfilter.h:141
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:418
int nb_channels
internal API functions