FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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;
42 
43 #define OFFSET(x) offsetof(ChannelSplitContext, x)
44 #define A AV_OPT_FLAG_AUDIO_PARAM
45 #define F AV_OPT_FLAG_FILTERING_PARAM
46 static const AVOption channelsplit_options[] = {
47  { "channel_layout", "Input channel layout.", OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, { .str = "stereo" }, .flags = A|F },
48  { NULL }
49 };
50 
51 AVFILTER_DEFINE_CLASS(channelsplit);
52 
54 {
55  ChannelSplitContext *s = ctx->priv;
56  int nb_channels;
57  int ret = 0, i;
58 
60  av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout '%s'.\n",
62  ret = AVERROR(EINVAL);
63  goto fail;
64  }
65 
67  for (i = 0; i < nb_channels; i++) {
69  AVFilterPad pad = { 0 };
70 
72  pad.name = av_get_channel_name(channel);
73 
74  if ((ret = ff_insert_outpad(ctx, i, &pad)) < 0) {
75  return ret;
76  }
77  }
78 
79 fail:
80  return ret;
81 }
82 
84 {
85  ChannelSplitContext *s = ctx->priv;
86  AVFilterChannelLayouts *in_layouts = NULL;
87  int i, ret;
88 
89  if ((ret = ff_set_common_formats(ctx, ff_planar_sample_fmts())) < 0 ||
91  return ret;
92 
93  if ((ret = ff_add_channel_layout(&in_layouts, s->channel_layout)) < 0 ||
94  (ret = ff_channel_layouts_ref(in_layouts, &ctx->inputs[0]->out_channel_layouts)) < 0)
95  return ret;
96 
97  for (i = 0; i < ctx->nb_outputs; i++) {
98  AVFilterChannelLayouts *out_layouts = NULL;
100 
101  if ((ret = ff_add_channel_layout(&out_layouts, channel)) < 0 ||
102  (ret = ff_channel_layouts_ref(out_layouts, &ctx->outputs[i]->in_channel_layouts)) < 0)
103  return ret;
104  }
105 
106  return 0;
107 }
108 
109 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
110 {
111  AVFilterContext *ctx = inlink->dst;
112  int i, ret = 0;
113 
114  for (i = 0; i < ctx->nb_outputs; i++) {
115  AVFrame *buf_out = av_frame_clone(buf);
116 
117  if (!buf_out) {
118  ret = AVERROR(ENOMEM);
119  break;
120  }
121 
122  buf_out->data[0] = buf_out->extended_data[0] = buf_out->extended_data[i];
123  buf_out->channel_layout =
125  buf_out->channels = 1;
126 
127  ret = ff_filter_frame(ctx->outputs[i], buf_out);
128  if (ret < 0)
129  break;
130  }
131  av_frame_free(&buf);
132  return ret;
133 }
134 
136  {
137  .name = "default",
138  .type = AVMEDIA_TYPE_AUDIO,
139  .filter_frame = filter_frame,
140  },
141  { NULL }
142 };
143 
145  .name = "channelsplit",
146  .description = NULL_IF_CONFIG_SMALL("Split audio into per-channel streams."),
147  .priv_size = sizeof(ChannelSplitContext),
148  .priv_class = &channelsplit_class,
149  .init = init,
151  .inputs = avfilter_af_channelsplit_inputs,
152  .outputs = NULL,
154 };
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:768
This structure describes decoded (raw) audio or video data.
Definition: frame.h:201
AVOption.
Definition: opt.h:246
Main libavfilter public API header.
enum AVMediaType type
AVFilterPad type.
Definition: internal.h:65
Macro definitions for various function/variable attributes.
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
const char * name
Pad name.
Definition: internal.h:60
uint64_t av_get_channel_layout(const char *name)
Return a channel layout id that matches name, or 0 if no match is found.
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:346
int ff_channel_layouts_ref(AVFilterChannelLayouts *f, AVFilterChannelLayouts **ref)
Add *ref as a new reference to f.
Definition: formats.c:435
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1151
#define F
#define av_cold
Definition: attributes.h:82
#define A
AVOptions.
static int flags
Definition: log.c:57
#define AVFILTER_FLAG_DYNAMIC_OUTPUTS
The number of the filter outputs is not determined just by AVFilter.outputs.
Definition: avfilter.h:111
#define av_log(a,...)
A filter pad used for either input or output.
Definition: internal.h:54
AVFILTER_DEFINE_CLASS(channelsplit)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
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:568
int ff_add_channel_layout(AVFilterChannelLayouts **l, uint64_t channel_layout)
Definition: formats.c:343
#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:163
unsigned nb_outputs
number of output pads
Definition: avfilter.h:351
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:179
void * priv
private data for use by the filter
Definition: avfilter.h:353
static const AVFilterPad avfilter_af_channelsplit_inputs[]
#define fail()
Definition: checkasm.h:109
uint64_t channel_layout
Channel layout of the audio data.
Definition: frame.h:379
common internal API header
#define OFFSET(x)
int channels
number of audio channels, only used for audio.
Definition: frame.h:506
audio channel layout utility functions
AVFormatContext * ctx
Definition: movenc.c:48
AVFilterFormats * ff_planar_sample_fmts(void)
Construct a formats list containing all planar sample formats.
Definition: formats.c:382
static const AVFilterPad outputs[]
Definition: af_afftfilt.c:389
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:489
A list of supported channel layouts.
Definition: formats.h:85
static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
static const AVFilterPad inputs[]
Definition: af_afftfilt.c:379
static const AVOption channelsplit_options[]
void * buf
Definition: avisynth_c.h:690
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:144
const char * name
Filter name.
Definition: avfilter.h:148
static av_cold int init(AVFilterContext *ctx)
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:350
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:395
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:215
uint64_t av_channel_layout_extract_channel(uint64_t channel_layout, int index)
Get the channel with the given index in channel_layout.
channel
Use these values when setting the channel map with ebur128_set_channel().
Definition: ebur128.h:39
const char * av_get_channel_name(uint64_t channel)
Get the name of a given channel.
static int query_formats(AVFilterContext *ctx)
AVFilter ff_af_channelsplit
An instance of a filter.
Definition: avfilter.h:338
int nb_channels
internal API functions
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:248
static int ff_insert_outpad(AVFilterContext *f, unsigned index, AVFilterPad *p)
Insert a new output pad for the filter.
Definition: internal.h:285
int ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:556