2 * Copyright (c) 2010 S.N. Hemanth Meenakshisundaram <smeenaks@ucsd.edu>
3 * Copyright (c) 2011 Stefano Sabatini
4 * Copyright (c) 2011 Mina Nagy Zaki
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * sample format and channel layout conversion audio filter
28 #include "libavutil/avstring.h"
29 #include "libavutil/channel_layout.h"
30 #include "libswresample/swresample.h"
36 enum AVSampleFormat out_sample_fmt;
38 struct SwrContext *swr;
41 static av_cold int init(AVFilterContext *ctx)
43 AConvertContext *aconvert = ctx->priv;
44 char *arg, *ptr = NULL;
46 char *args = av_strdup(NULL);
48 av_log(ctx, AV_LOG_WARNING, "This filter is deprecated, use aformat instead\n");
50 aconvert->out_sample_fmt = AV_SAMPLE_FMT_NONE;
51 aconvert->out_chlayout = 0;
53 if ((arg = av_strtok(args, ":", &ptr)) && strcmp(arg, "auto")) {
54 if ((ret = ff_parse_sample_format(&aconvert->out_sample_fmt, arg, ctx)) < 0)
57 if ((arg = av_strtok(NULL, ":", &ptr)) && strcmp(arg, "auto")) {
58 if ((ret = ff_parse_channel_layout(&aconvert->out_chlayout, arg, ctx)) < 0)
67 static av_cold void uninit(AVFilterContext *ctx)
69 AConvertContext *aconvert = ctx->priv;
70 swr_free(&aconvert->swr);
73 static int query_formats(AVFilterContext *ctx)
75 AVFilterFormats *formats = NULL;
76 AConvertContext *aconvert = ctx->priv;
77 AVFilterLink *inlink = ctx->inputs[0];
78 AVFilterLink *outlink = ctx->outputs[0];
79 AVFilterChannelLayouts *layouts;
81 ff_formats_ref(ff_all_formats(AVMEDIA_TYPE_AUDIO),
82 &inlink->out_formats);
83 if (aconvert->out_sample_fmt != AV_SAMPLE_FMT_NONE) {
85 ff_add_format(&formats, aconvert->out_sample_fmt);
86 ff_formats_ref(formats, &outlink->in_formats);
88 ff_formats_ref(ff_all_formats(AVMEDIA_TYPE_AUDIO),
89 &outlink->in_formats);
91 ff_channel_layouts_ref(ff_all_channel_layouts(),
92 &inlink->out_channel_layouts);
93 if (aconvert->out_chlayout != 0) {
95 ff_add_channel_layout(&layouts, aconvert->out_chlayout);
96 ff_channel_layouts_ref(layouts, &outlink->in_channel_layouts);
98 ff_channel_layouts_ref(ff_all_channel_layouts(),
99 &outlink->in_channel_layouts);
104 static int config_output(AVFilterLink *outlink)
107 AVFilterContext *ctx = outlink->src;
108 AVFilterLink *inlink = ctx->inputs[0];
109 AConvertContext *aconvert = ctx->priv;
110 char buf1[64], buf2[64];
112 /* if not specified in args, use the format and layout of the output */
113 if (aconvert->out_sample_fmt == AV_SAMPLE_FMT_NONE)
114 aconvert->out_sample_fmt = outlink->format;
115 if (aconvert->out_chlayout == 0)
116 aconvert->out_chlayout = outlink->channel_layout;
118 aconvert->swr = swr_alloc_set_opts(aconvert->swr,
119 aconvert->out_chlayout, aconvert->out_sample_fmt, inlink->sample_rate,
120 inlink->channel_layout, inlink->format, inlink->sample_rate,
123 return AVERROR(ENOMEM);
124 ret = swr_init(aconvert->swr);
128 av_get_channel_layout_string(buf1, sizeof(buf1),
129 -1, inlink ->channel_layout);
130 av_get_channel_layout_string(buf2, sizeof(buf2),
131 -1, outlink->channel_layout);
132 av_log(ctx, AV_LOG_VERBOSE,
133 "fmt:%s cl:%s -> fmt:%s cl:%s\n",
134 av_get_sample_fmt_name(inlink ->format), buf1,
135 av_get_sample_fmt_name(outlink->format), buf2);
140 static int filter_frame(AVFilterLink *inlink, AVFrame *insamplesref)
142 AConvertContext *aconvert = inlink->dst->priv;
143 const int n = insamplesref->nb_samples;
144 AVFilterLink *const outlink = inlink->dst->outputs[0];
145 AVFrame *outsamplesref = ff_get_audio_buffer(outlink, n);
149 return AVERROR(ENOMEM);
150 swr_convert(aconvert->swr, outsamplesref->extended_data, n,
151 (void *)insamplesref->extended_data, n);
153 av_frame_copy_props(outsamplesref, insamplesref);
154 av_frame_set_channels(outsamplesref, outlink->channels);
155 outsamplesref->channel_layout = outlink->channel_layout;
157 ret = ff_filter_frame(outlink, outsamplesref);
158 av_frame_free(&insamplesref);
162 static const AVFilterPad aconvert_inputs[] = {
165 .type = AVMEDIA_TYPE_AUDIO,
166 .filter_frame = filter_frame,
171 static const AVFilterPad aconvert_outputs[] = {
174 .type = AVMEDIA_TYPE_AUDIO,
175 .config_props = config_output,
180 AVFilter avfilter_af_aconvert = {
182 .description = NULL_IF_CONFIG_SMALL("Convert the input audio to sample_fmt:channel_layout."),
183 .priv_size = sizeof(AConvertContext),
186 .query_formats = query_formats,
187 .inputs = aconvert_inputs,
188 .outputs = aconvert_outputs,