Libav
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 
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;
42 
43 #define OFFSET(x) offsetof(ChannelSplitContext, x)
44 #define A AV_OPT_FLAG_AUDIO_PARAM
45 static const AVOption options[] = {
46  { "channel_layout", "Input channel layout.", OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, { .str = "stereo" }, .flags = A },
47  { NULL },
48 };
49 
50 static const AVClass channelsplit_class = {
51  .class_name = "channelsplit filter",
52  .item_name = av_default_item_name,
53  .option = options,
54  .version = LIBAVUTIL_VERSION_INT,
55 };
56 
57 static av_cold int init(AVFilterContext *ctx)
58 {
59  ChannelSplitContext *s = ctx->priv;
60  int nb_channels;
61  int ret = 0, i;
62 
64  av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout '%s'.\n",
66  ret = AVERROR(EINVAL);
67  goto fail;
68  }
69 
71  for (i = 0; i < nb_channels; i++) {
72  uint64_t channel = av_channel_layout_extract_channel(s->channel_layout, i);
73  AVFilterPad pad = { 0 };
74 
76  pad.name = av_get_channel_name(channel);
77 
78  ff_insert_outpad(ctx, i, &pad);
79  }
80 
81 fail:
82  return ret;
83 }
84 
86 {
87  ChannelSplitContext *s = ctx->priv;
88  AVFilterChannelLayouts *in_layouts = NULL;
89  int i;
90 
93 
94  ff_add_channel_layout(&in_layouts, s->channel_layout);
95  ff_channel_layouts_ref(in_layouts, &ctx->inputs[0]->out_channel_layouts);
96 
97  for (i = 0; i < ctx->nb_outputs; i++) {
98  AVFilterChannelLayouts *out_layouts = NULL;
99  uint64_t channel = av_channel_layout_extract_channel(s->channel_layout, i);
100 
101  ff_add_channel_layout(&out_layouts, channel);
102  ff_channel_layouts_ref(out_layouts, &ctx->outputs[i]->in_channel_layouts);
103  }
104 
105  return 0;
106 }
107 
108 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
109 {
110  AVFilterContext *ctx = inlink->dst;
111  int i, ret = 0;
112 
113  for (i = 0; i < ctx->nb_outputs; i++) {
114  AVFrame *buf_out = av_frame_clone(buf);
115 
116  if (!buf_out) {
117  ret = AVERROR(ENOMEM);
118  break;
119  }
120 
121  buf_out->data[0] = buf_out->extended_data[0] = buf_out->extended_data[i];
122  buf_out->channel_layout =
124 
125  ret = ff_filter_frame(ctx->outputs[i], buf_out);
126  if (ret < 0)
127  break;
128  }
129  av_frame_free(&buf);
130  return ret;
131 }
132 
134  {
135  .name = "default",
136  .type = AVMEDIA_TYPE_AUDIO,
137  .filter_frame = filter_frame,
138  },
139  { NULL }
140 };
141 
143  .name = "channelsplit",
144  .description = NULL_IF_CONFIG_SMALL("Split audio into per-channel streams"),
145  .priv_size = sizeof(ChannelSplitContext),
146  .priv_class = &channelsplit_class,
147 
148  .init = init,
150 
151  .inputs = avfilter_af_channelsplit_inputs,
152  .outputs = NULL,
153 
155 };
This structure describes decoded (raw) audio or video data.
Definition: frame.h:135
AVOption.
Definition: opt.h:234
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:232
Main libavfilter public API header.
enum AVMediaType type
AVFilterPad type.
Definition: internal.h:47
Macro definitions for various function/variable attributes.
const char * name
Pad name.
Definition: internal.h:42
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:571
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:733
#define av_cold
Definition: attributes.h:66
#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:261
static int flags
Definition: log.c:44
#define AVFILTER_FLAG_DYNAMIC_OUTPUTS
The number of the filter outputs is not determined just by AVFilter.outputs.
Definition: avfilter.h:410
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:379
A filter pad used for either input or output.
Definition: internal.h:36
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
int ff_add_channel_layout(AVFilterChannelLayouts **l, uint64_t channel_layout)
Definition: formats.c:204
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:69
unsigned nb_outputs
number of output pads
Definition: avfilter.h:582
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:145
void * priv
private data for use by the filter
Definition: avfilter.h:584
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:169
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:381
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:183
static const AVOption options[]
AVFilterFormats * ff_planar_sample_fmts(void)
Construct a formats list containing all planar sample formats.
Definition: formats.c:230
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:270
static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
NULL
Definition: eval.c:55
av_default_item_name
Definition: dnxhdenc.c:52
Describe the class of an AVClass context structure.
Definition: log.h:33
Filter definition.
Definition: avfilter.h:421
static const AVFilterPad inputs[]
Definition: af_ashowinfo.c:221
const char * name
Filter name.
Definition: avfilter.h:425
static av_cold int init(AVFilterContext *ctx)
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:578
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:242
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:141
void ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:367
static int query_formats(AVFilterContext *ctx)
AVFilter ff_af_channelsplit
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:563
int nb_channels
internal API functions
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:169