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, const char *args0)
43 AConvertContext *aconvert = ctx->priv;
44 char *arg, *ptr = NULL;
46 char *args = av_strdup(args0);
48 aconvert->out_sample_fmt = AV_SAMPLE_FMT_NONE;
49 aconvert->out_chlayout = 0;
51 if ((arg = av_strtok(args, ":", &ptr)) && strcmp(arg, "auto")) {
52 if ((ret = ff_parse_sample_format(&aconvert->out_sample_fmt, arg, ctx)) < 0)
55 if ((arg = av_strtok(NULL, ":", &ptr)) && strcmp(arg, "auto")) {
56 if ((ret = ff_parse_channel_layout(&aconvert->out_chlayout, arg, ctx)) < 0)
65 static av_cold void uninit(AVFilterContext *ctx)
67 AConvertContext *aconvert = ctx->priv;
68 swr_free(&aconvert->swr);
71 static int query_formats(AVFilterContext *ctx)
73 AVFilterFormats *formats = NULL;
74 AConvertContext *aconvert = ctx->priv;
75 AVFilterLink *inlink = ctx->inputs[0];
76 AVFilterLink *outlink = ctx->outputs[0];
77 AVFilterChannelLayouts *layouts;
79 ff_formats_ref(ff_all_formats(AVMEDIA_TYPE_AUDIO),
80 &inlink->out_formats);
81 if (aconvert->out_sample_fmt != AV_SAMPLE_FMT_NONE) {
83 ff_add_format(&formats, aconvert->out_sample_fmt);
84 ff_formats_ref(formats, &outlink->in_formats);
86 ff_formats_ref(ff_all_formats(AVMEDIA_TYPE_AUDIO),
87 &outlink->in_formats);
89 ff_channel_layouts_ref(ff_all_channel_layouts(),
90 &inlink->out_channel_layouts);
91 if (aconvert->out_chlayout != 0) {
93 ff_add_channel_layout(&layouts, aconvert->out_chlayout);
94 ff_channel_layouts_ref(layouts, &outlink->in_channel_layouts);
96 ff_channel_layouts_ref(ff_all_channel_layouts(),
97 &outlink->in_channel_layouts);
102 static int config_output(AVFilterLink *outlink)
105 AVFilterContext *ctx = outlink->src;
106 AVFilterLink *inlink = ctx->inputs[0];
107 AConvertContext *aconvert = ctx->priv;
108 char buf1[64], buf2[64];
110 /* if not specified in args, use the format and layout of the output */
111 if (aconvert->out_sample_fmt == AV_SAMPLE_FMT_NONE)
112 aconvert->out_sample_fmt = outlink->format;
113 if (aconvert->out_chlayout == 0)
114 aconvert->out_chlayout = outlink->channel_layout;
116 aconvert->swr = swr_alloc_set_opts(aconvert->swr,
117 aconvert->out_chlayout, aconvert->out_sample_fmt, inlink->sample_rate,
118 inlink->channel_layout, inlink->format, inlink->sample_rate,
121 return AVERROR(ENOMEM);
122 ret = swr_init(aconvert->swr);
126 av_get_channel_layout_string(buf1, sizeof(buf1),
127 -1, inlink ->channel_layout);
128 av_get_channel_layout_string(buf2, sizeof(buf2),
129 -1, outlink->channel_layout);
130 av_log(ctx, AV_LOG_VERBOSE,
131 "fmt:%s cl:%s -> fmt:%s cl:%s\n",
132 av_get_sample_fmt_name(inlink ->format), buf1,
133 av_get_sample_fmt_name(outlink->format), buf2);
138 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *insamplesref)
140 AConvertContext *aconvert = inlink->dst->priv;
141 const int n = insamplesref->audio->nb_samples;
142 AVFilterLink *const outlink = inlink->dst->outputs[0];
143 AVFilterBufferRef *outsamplesref = ff_get_audio_buffer(outlink, AV_PERM_WRITE, n);
146 swr_convert(aconvert->swr, outsamplesref->data, n,
147 (void *)insamplesref->data, n);
149 avfilter_copy_buffer_ref_props(outsamplesref, insamplesref);
150 outsamplesref->audio->channels = outlink->channels;
151 outsamplesref->audio->channel_layout = outlink->channel_layout;
153 ret = ff_filter_frame(outlink, outsamplesref);
154 avfilter_unref_buffer(insamplesref);
158 static const AVFilterPad aconvert_inputs[] = {
161 .type = AVMEDIA_TYPE_AUDIO,
162 .filter_frame = filter_frame,
163 .min_perms = AV_PERM_READ,
168 static const AVFilterPad aconvert_outputs[] = {
171 .type = AVMEDIA_TYPE_AUDIO,
172 .config_props = config_output,
177 AVFilter avfilter_af_aconvert = {
179 .description = NULL_IF_CONFIG_SMALL("Convert the input audio to sample_fmt:channel_layout."),
180 .priv_size = sizeof(AConvertContext),
183 .query_formats = query_formats,
184 .inputs = aconvert_inputs,
185 .outputs = aconvert_outputs,