]> git.sesse.net Git - ffmpeg/blob - libavfilter/af_aconvert.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavfilter / af_aconvert.c
1 /*
2  * Copyright (c) 2010 S.N. Hemanth Meenakshisundaram <smeenaks@ucsd.edu>
3  * Copyright (c) 2011 Stefano Sabatini
4  * Copyright (c) 2011 Mina Nagy Zaki
5  *
6  * This file is part of FFmpeg.
7  *
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.
12  *
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.
17  *
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
21  */
22
23 /**
24  * @file
25  * sample format and channel layout conversion audio filter
26  */
27
28 #include "libavutil/avstring.h"
29 #include "libswresample/swresample.h"
30 #include "avfilter.h"
31 #include "audio.h"
32 #include "internal.h"
33
34 typedef struct {
35     enum AVSampleFormat  out_sample_fmt;
36     int64_t              out_chlayout;
37     struct SwrContext *swr;
38 } AConvertContext;
39
40 static av_cold int init(AVFilterContext *ctx, const char *args0, void *opaque)
41 {
42     AConvertContext *aconvert = ctx->priv;
43     char *arg, *ptr = NULL;
44     int ret = 0;
45     char *args = av_strdup(args0);
46
47     aconvert->out_sample_fmt  = AV_SAMPLE_FMT_NONE;
48     aconvert->out_chlayout    = 0;
49
50     if ((arg = av_strtok(args, ":", &ptr)) && strcmp(arg, "auto")) {
51         if ((ret = ff_parse_sample_format(&aconvert->out_sample_fmt, arg, ctx)) < 0)
52             goto end;
53     }
54     if ((arg = av_strtok(NULL, ":", &ptr)) && strcmp(arg, "auto")) {
55         if ((ret = ff_parse_channel_layout(&aconvert->out_chlayout, arg, ctx)) < 0)
56             goto end;
57     }
58
59 end:
60     av_freep(&args);
61     return ret;
62 }
63
64 static av_cold void uninit(AVFilterContext *ctx)
65 {
66     AConvertContext *aconvert = ctx->priv;
67     swr_free(&aconvert->swr);
68 }
69
70 static int query_formats(AVFilterContext *ctx)
71 {
72     AVFilterFormats *formats = NULL;
73     AConvertContext *aconvert = ctx->priv;
74     AVFilterLink *inlink  = ctx->inputs[0];
75     AVFilterLink *outlink = ctx->outputs[0];
76     AVFilterChannelLayouts *layouts;
77
78     avfilter_formats_ref(avfilter_make_all_formats(AVMEDIA_TYPE_AUDIO),
79                          &inlink->out_formats);
80     if (aconvert->out_sample_fmt != AV_SAMPLE_FMT_NONE) {
81         formats = NULL;
82         avfilter_add_format(&formats, aconvert->out_sample_fmt);
83         avfilter_formats_ref(formats, &outlink->in_formats);
84     } else
85         avfilter_formats_ref(avfilter_make_all_formats(AVMEDIA_TYPE_AUDIO),
86                              &outlink->in_formats);
87
88     ff_channel_layouts_ref(ff_all_channel_layouts(),
89                          &inlink->out_channel_layouts);
90     if (aconvert->out_chlayout != 0) {
91         layouts = NULL;
92         ff_add_channel_layout(&layouts, aconvert->out_chlayout);
93         ff_channel_layouts_ref(layouts, &outlink->in_channel_layouts);
94     } else
95         ff_channel_layouts_ref(ff_all_channel_layouts(),
96                              &outlink->in_channel_layouts);
97
98     return 0;
99 }
100
101 static int config_output(AVFilterLink *outlink)
102 {
103     int ret;
104     AVFilterContext *ctx = outlink->src;
105     AVFilterLink *inlink = ctx->inputs[0];
106     AConvertContext *aconvert = ctx->priv;
107     char buf1[64], buf2[64];
108
109     /* if not specified in args, use the format and layout of the output */
110     if (aconvert->out_sample_fmt == AV_SAMPLE_FMT_NONE)
111         aconvert->out_sample_fmt = outlink->format;
112     if (aconvert->out_chlayout   == 0)
113         aconvert->out_chlayout   = outlink->channel_layout;
114
115     aconvert->swr = swr_alloc_set_opts(aconvert->swr,
116                                        aconvert->out_chlayout, aconvert->out_sample_fmt, inlink->sample_rate,
117                                        inlink->channel_layout, inlink->format,           inlink->sample_rate,
118                                        0, ctx);
119     if (!aconvert->swr)
120         return AVERROR(ENOMEM);
121     ret = swr_init(aconvert->swr);
122     if (ret < 0)
123         return ret;
124
125     av_get_channel_layout_string(buf1, sizeof(buf1),
126                                  -1, inlink ->channel_layout);
127     av_get_channel_layout_string(buf2, sizeof(buf2),
128                                  -1, outlink->channel_layout);
129     av_log(ctx, AV_LOG_INFO,
130            "fmt:%s cl:%s -> fmt:%s cl:%s\n",
131            av_get_sample_fmt_name(inlink ->format), buf1,
132            av_get_sample_fmt_name(outlink->format), buf2);
133
134     return 0;
135 }
136
137 static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamplesref)
138 {
139     AConvertContext *aconvert = inlink->dst->priv;
140     const int n = insamplesref->audio->nb_samples;
141     AVFilterLink *const outlink = inlink->dst->outputs[0];
142     AVFilterBufferRef *outsamplesref = ff_get_audio_buffer(outlink, AV_PERM_WRITE, n);
143
144     swr_convert(aconvert->swr, outsamplesref->data, n,
145                         (void *)insamplesref->data, n);
146
147     avfilter_copy_buffer_ref_props(outsamplesref, insamplesref);
148     outsamplesref->audio->channel_layout = outlink->channel_layout;
149
150     ff_filter_samples(outlink, outsamplesref);
151     avfilter_unref_buffer(insamplesref);
152 }
153
154 AVFilter avfilter_af_aconvert = {
155     .name          = "aconvert",
156     .description   = NULL_IF_CONFIG_SMALL("Convert the input audio to sample_fmt:channel_layout."),
157     .priv_size     = sizeof(AConvertContext),
158     .init          = init,
159     .uninit        = uninit,
160     .query_formats = query_formats,
161
162     .inputs    = (const AVFilterPad[]) {{ .name      = "default",
163                                     .type            = AVMEDIA_TYPE_AUDIO,
164                                     .filter_samples  = filter_samples,
165                                     .min_perms       = AV_PERM_READ, },
166                                   { .name = NULL}},
167     .outputs   = (const AVFilterPad[]) {{ .name      = "default",
168                                     .type            = AVMEDIA_TYPE_AUDIO,
169                                     .config_props    = config_output, },
170                                   { .name = NULL}},
171 };