]> git.sesse.net Git - ffmpeg/blob - libavfilter/af_aformat.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavfilter / af_aformat.c
1 /*
2  * Copyright (c) 2011 Mina Nagy Zaki
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 /**
22  * @file
23  * format audio filter
24  */
25
26 #include "libavutil/audioconvert.h"
27 #include "libavutil/avstring.h"
28 #include "avfilter.h"
29 #include "internal.h"
30
31 typedef struct {
32     AVFilterFormats *formats, *chlayouts, *packing;
33 } AFormatContext;
34
35 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
36 {
37     AFormatContext * const aformat = ctx->priv;
38     char *fmts_str = NULL, *fmt_str, *ptr = NULL;
39     int64_t fmt;
40     int ret;
41
42     if (!args)
43         goto arg_fail;
44
45     fmts_str = av_get_token(&args, ":");
46     if (!fmts_str || !*fmts_str)
47         goto arg_fail;
48     if (!strcmp(fmts_str, "all")) {
49         aformat->formats = avfilter_all_formats(AVMEDIA_TYPE_AUDIO);
50     } else {
51         for (fmt_str = fmts_str;
52              fmt_str = strtok_r(fmt_str, ",", &ptr); fmt_str = NULL) {
53             if ((ret = ff_parse_sample_format((int*)&fmt, fmt_str, ctx)) < 0) {
54                 av_freep(&fmts_str);
55                 return ret;
56             }
57             avfilter_add_format(&aformat->formats, fmt);
58         }
59     }
60     av_freep(&fmts_str);
61
62     if (*args)
63         args++;
64     fmts_str = av_get_token(&args, ":");
65     if (!fmts_str || !*fmts_str)
66         goto arg_fail;
67     if (!strcmp(fmts_str, "all")) {
68         aformat->chlayouts = avfilter_all_channel_layouts();
69     } else {
70         for (fmt_str = fmts_str;
71              fmt_str = strtok_r(fmt_str, ",", &ptr); fmt_str = NULL) {
72             if ((ret = ff_parse_channel_layout(&fmt, fmt_str, ctx)) < 0) {
73                 av_freep(&fmts_str);
74                 return ret;
75             }
76             avfilter_add_format(&aformat->chlayouts, fmt);
77         }
78     }
79     av_freep(&fmts_str);
80
81     if (*args)
82         args++;
83     fmts_str = av_get_token(&args, ":");
84     if (!fmts_str || !*fmts_str)
85         goto arg_fail;
86     if (!strcmp(fmts_str, "all")) {
87         aformat->packing = avfilter_all_packing_formats();
88     } else {
89         for (fmt_str = fmts_str;
90              fmt_str = strtok_r(fmt_str, ",", &ptr); fmt_str = NULL) {
91             if ((ret = ff_parse_packing_format((int*)&fmt, fmt_str, ctx)) < 0) {
92                 av_freep(&fmts_str);
93                 return ret;
94             }
95             avfilter_add_format(&aformat->packing, fmt);
96         }
97     }
98     av_freep(&fmts_str);
99
100     return 0;
101
102 arg_fail:
103     av_log(ctx, AV_LOG_ERROR, "Invalid arguments, they must be of the form "
104                               "sample_fmts:channel_layouts:packing_fmts\n");
105     av_freep(&fmts_str);
106     return AVERROR(EINVAL);
107 }
108
109 static int query_formats(AVFilterContext *ctx)
110 {
111     AFormatContext * const aformat = ctx->priv;
112
113     avfilter_set_common_sample_formats (ctx, aformat->formats);
114     avfilter_set_common_channel_layouts(ctx, aformat->chlayouts);
115     avfilter_set_common_packing_formats(ctx, aformat->packing);
116     return 0;
117 }
118
119 static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamplesref)
120 {
121     avfilter_filter_samples(inlink->dst->outputs[0], insamplesref);
122 }
123
124 AVFilter avfilter_af_aformat = {
125     .name          = "aformat",
126     .description   = NULL_IF_CONFIG_SMALL("Convert the input audio to one of the specified formats."),
127     .init          = init,
128     .query_formats = query_formats,
129     .priv_size     = sizeof(AFormatContext),
130
131     .inputs        = (AVFilterPad[]) {{ .name            = "default",
132                                         .type            = AVMEDIA_TYPE_AUDIO,
133                                         .filter_samples  = filter_samples},
134                                       { .name = NULL}},
135     .outputs       = (AVFilterPad[]) {{ .name            = "default",
136                                         .type            = AVMEDIA_TYPE_AUDIO},
137                                       { .name = NULL}},
138 };