]> 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 "internal.h"
32
33 typedef struct {
34     enum AVSampleFormat  out_sample_fmt;
35     int64_t              out_chlayout;
36     struct SwrContext *swr;
37 } AConvertContext;
38
39 static av_cold int init(AVFilterContext *ctx, const char *args0, void *opaque)
40 {
41     AConvertContext *aconvert = ctx->priv;
42     char *arg, *ptr = NULL;
43     int ret = 0;
44     char *args = av_strdup(args0);
45
46     aconvert->out_sample_fmt  = AV_SAMPLE_FMT_NONE;
47     aconvert->out_chlayout    = 0;
48
49     if ((arg = av_strtok(args, ":", &ptr)) && strcmp(arg, "auto")) {
50         if ((ret = ff_parse_sample_format(&aconvert->out_sample_fmt, arg, ctx)) < 0)
51             goto end;
52     }
53     if ((arg = av_strtok(NULL, ":", &ptr)) && strcmp(arg, "auto")) {
54         if ((ret = ff_parse_channel_layout(&aconvert->out_chlayout, arg, ctx)) < 0)
55             goto end;
56     }
57
58 end:
59     av_freep(&args);
60     return ret;
61 }
62
63 static av_cold void uninit(AVFilterContext *ctx)
64 {
65     AConvertContext *aconvert = ctx->priv;
66     swr_free(&aconvert->swr);
67 }
68
69 static int query_formats(AVFilterContext *ctx)
70 {
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);
76
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) {
80         formats = NULL;
81         avfilter_add_format(&formats, aconvert->out_sample_fmt);
82         avfilter_formats_ref(formats, &outlink->in_formats);
83     } else
84         avfilter_formats_ref(avfilter_make_all_formats(AVMEDIA_TYPE_AUDIO),
85                              &outlink->in_formats);
86
87     avfilter_formats_ref(avfilter_make_all_channel_layouts(),
88                          &inlink->out_chlayouts);
89     if (aconvert->out_chlayout != 0) {
90         formats = NULL;
91         avfilter_add_format(&formats, aconvert->out_chlayout);
92         avfilter_formats_ref(formats, &outlink->in_chlayouts);
93     } else
94         avfilter_formats_ref(avfilter_make_all_channel_layouts(),
95                              &outlink->in_chlayouts);
96
97     avfilter_formats_ref(avfilter_make_all_packing_formats(),
98                          &inlink->out_packing);
99     formats = NULL;
100     avfilter_add_format(&formats, out_packing);
101     avfilter_formats_ref(formats, &outlink->in_packing);
102
103     return 0;
104 }
105
106 static int config_output(AVFilterLink *outlink)
107 {
108     int ret;
109     AVFilterContext *ctx = outlink->src;
110     AVFilterLink *inlink = ctx->inputs[0];
111     AConvertContext *aconvert = ctx->priv;
112     char buf1[64], buf2[64];
113
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;
119
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,
123                                        0, ctx);
124     if (!aconvert->swr)
125         return AVERROR(ENOMEM);
126     ret = swr_init(aconvert->swr);
127     if (ret < 0)
128         return ret;
129
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);
138
139     return 0;
140 }
141
142 static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamplesref)
143 {
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);
148
149     swr_convert(aconvert->swr, outsamplesref->data, n,
150                         (void *)insamplesref->data, n);
151
152     avfilter_copy_buffer_ref_props(outsamplesref, insamplesref);
153     outsamplesref->audio->channel_layout = outlink->channel_layout;
154     outsamplesref->audio->planar         = outlink->planar;
155
156     avfilter_filter_samples(outlink, outsamplesref);
157     avfilter_unref_buffer(insamplesref);
158 }
159
160 AVFilter avfilter_af_aconvert = {
161     .name          = "aconvert",
162     .description   = NULL_IF_CONFIG_SMALL("Convert the input audio to sample_fmt:channel_layout:packed_fmt."),
163     .priv_size     = sizeof(AConvertContext),
164     .init          = init,
165     .uninit        = uninit,
166     .query_formats = query_formats,
167
168     .inputs    = (const AVFilterPad[]) {{ .name      = "default",
169                                     .type            = AVMEDIA_TYPE_AUDIO,
170                                     .filter_samples  = filter_samples,
171                                     .min_perms       = AV_PERM_READ, },
172                                   { .name = NULL}},
173     .outputs   = (const AVFilterPad[]) {{ .name      = "default",
174                                     .type            = AVMEDIA_TYPE_AUDIO,
175                                     .config_props    = config_output, },
176                                   { .name = NULL}},
177 };