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 "libswresample/swresample.h"
34 enum AVSampleFormat out_sample_fmt;
36 struct SwrContext *swr;
39 static av_cold int init(AVFilterContext *ctx, const char *args0, void *opaque)
41 AConvertContext *aconvert = ctx->priv;
42 char *arg, *ptr = NULL;
44 char *args = av_strdup(args0);
46 aconvert->out_sample_fmt = AV_SAMPLE_FMT_NONE;
47 aconvert->out_chlayout = 0;
49 if ((arg = av_strtok(args, ":", &ptr)) && strcmp(arg, "auto")) {
50 if ((ret = ff_parse_sample_format(&aconvert->out_sample_fmt, arg, ctx)) < 0)
53 if ((arg = av_strtok(NULL, ":", &ptr)) && strcmp(arg, "auto")) {
54 if ((ret = ff_parse_channel_layout(&aconvert->out_chlayout, arg, ctx)) < 0)
63 static av_cold void uninit(AVFilterContext *ctx)
65 AConvertContext *aconvert = ctx->priv;
66 swr_free(&aconvert->swr);
69 static int query_formats(AVFilterContext *ctx)
71 AVFilterFormats *formats = NULL;
72 AConvertContext *aconvert = ctx->priv;
73 AVFilterLink *inlink = ctx->inputs[0];
74 AVFilterLink *outlink = ctx->outputs[0];
75 int out_packing = av_sample_fmt_is_planar(aconvert->out_sample_fmt);
77 avfilter_formats_ref(avfilter_make_all_formats(AVMEDIA_TYPE_AUDIO),
78 &inlink->out_formats);
79 if (aconvert->out_sample_fmt != AV_SAMPLE_FMT_NONE) {
81 avfilter_add_format(&formats, aconvert->out_sample_fmt);
82 avfilter_formats_ref(formats, &outlink->in_formats);
84 avfilter_formats_ref(avfilter_make_all_formats(AVMEDIA_TYPE_AUDIO),
85 &outlink->in_formats);
87 avfilter_formats_ref(avfilter_make_all_channel_layouts(),
88 &inlink->out_chlayouts);
89 if (aconvert->out_chlayout != 0) {
91 avfilter_add_format(&formats, aconvert->out_chlayout);
92 avfilter_formats_ref(formats, &outlink->in_chlayouts);
94 avfilter_formats_ref(avfilter_make_all_channel_layouts(),
95 &outlink->in_chlayouts);
97 avfilter_formats_ref(avfilter_make_all_packing_formats(),
98 &inlink->out_packing);
100 avfilter_add_format(&formats, out_packing);
101 avfilter_formats_ref(formats, &outlink->in_packing);
106 static int config_output(AVFilterLink *outlink)
109 AVFilterContext *ctx = outlink->src;
110 AVFilterLink *inlink = ctx->inputs[0];
111 AConvertContext *aconvert = ctx->priv;
112 char buf1[64], buf2[64];
114 /* if not specified in args, use the format and layout of the output */
115 if (aconvert->out_sample_fmt == AV_SAMPLE_FMT_NONE)
116 aconvert->out_sample_fmt = outlink->format;
117 if (aconvert->out_chlayout == 0)
118 aconvert->out_chlayout = outlink->channel_layout;
120 aconvert->swr = swr_alloc_set_opts(aconvert->swr,
121 aconvert->out_chlayout, aconvert->out_sample_fmt, inlink->sample_rate,
122 inlink->channel_layout, inlink->format, inlink->sample_rate,
125 return AVERROR(ENOMEM);
126 ret = swr_init(aconvert->swr);
130 av_get_channel_layout_string(buf1, sizeof(buf1),
131 -1, inlink ->channel_layout);
132 av_get_channel_layout_string(buf2, sizeof(buf2),
133 -1, outlink->channel_layout);
134 av_log(ctx, AV_LOG_INFO,
135 "fmt:%s cl:%s planar:%i -> fmt:%s cl:%s planar:%i\n",
136 av_get_sample_fmt_name(inlink ->format), buf1, inlink ->planar,
137 av_get_sample_fmt_name(outlink->format), buf2, outlink->planar);
142 static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamplesref)
144 AConvertContext *aconvert = inlink->dst->priv;
145 const int n = insamplesref->audio->nb_samples;
146 AVFilterLink *const outlink = inlink->dst->outputs[0];
147 AVFilterBufferRef *outsamplesref = avfilter_get_audio_buffer(outlink, AV_PERM_WRITE, n);
149 swr_convert(aconvert->swr, outsamplesref->data, n,
150 (void *)insamplesref->data, n);
152 avfilter_copy_buffer_ref_props(outsamplesref, insamplesref);
153 outsamplesref->audio->channel_layout = outlink->channel_layout;
154 outsamplesref->audio->planar = outlink->planar;
156 avfilter_filter_samples(outlink, outsamplesref);
157 avfilter_unref_buffer(insamplesref);
160 AVFilter avfilter_af_aconvert = {
162 .description = NULL_IF_CONFIG_SMALL("Convert the input audio to sample_fmt:channel_layout:packed_fmt."),
163 .priv_size = sizeof(AConvertContext),
166 .query_formats = query_formats,
168 .inputs = (const AVFilterPad[]) {{ .name = "default",
169 .type = AVMEDIA_TYPE_AUDIO,
170 .filter_samples = filter_samples,
171 .min_perms = AV_PERM_READ, },
173 .outputs = (const AVFilterPad[]) {{ .name = "default",
174 .type = AVMEDIA_TYPE_AUDIO,
175 .config_props = config_output, },