e8926355e2de97a29bce4e4b63cc378d1f05ed12
[ffmpeg.git] / 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 #define ADD_FORMATS(all_formats, fmt_name, fmt_type, fmts_list)         \
46     fmts_str = av_get_token(&args, ":");                                \
47     if (!fmts_str || !*fmts_str)                                        \
48         goto arg_fail;                                                  \
49     if (!strcmp(fmts_str, "all")) {                                     \
50         aformat->fmts_list = all_formats;                               \
51     } else {                                                            \
52         for (fmt_str = fmts_str;                                        \
53              fmt_str = av_strtok(fmt_str, ",", &ptr); fmt_str = NULL) { \
54             if ((ret = ff_parse_##fmt_name((fmt_type *)&fmt,            \
55                                            fmt_str, ctx)) < 0) {        \
56                 av_freep(&fmts_str);                                    \
57                 return ret;                                             \
58             }                                                           \
59             avfilter_add_format(&aformat->fmts_list, fmt);              \
60         }                                                               \
61     }                                                                   \
62     av_freep(&fmts_str);                                                \
63     if (*args)                                                          \
64         args++;
65
66     ADD_FORMATS(avfilter_make_all_formats(AVMEDIA_TYPE_AUDIO), sample_format, int, formats);
67     ADD_FORMATS(avfilter_make_all_channel_layouts(), channel_layout, int64_t, chlayouts);
68     ADD_FORMATS(avfilter_make_all_packing_formats(), packing_format, int, packing);
69
70     return 0;
71
72 arg_fail:
73     av_log(ctx, AV_LOG_ERROR, "Invalid arguments, they must be of the form "
74                               "sample_fmts:channel_layouts:packing_fmts\n");
75     av_freep(&fmts_str);
76     return AVERROR(EINVAL);
77 }
78
79 static int query_formats(AVFilterContext *ctx)
80 {
81     AFormatContext * const aformat = ctx->priv;
82
83     avfilter_set_common_sample_formats (ctx, aformat->formats);
84     avfilter_set_common_channel_layouts(ctx, aformat->chlayouts);
85     avfilter_set_common_packing_formats(ctx, aformat->packing);
86     return 0;
87 }
88
89 static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamplesref)
90 {
91     avfilter_filter_samples(inlink->dst->outputs[0], insamplesref);
92 }
93
94 AVFilter avfilter_af_aformat = {
95     .name          = "aformat",
96     .description   = NULL_IF_CONFIG_SMALL("Convert the input audio to one of the specified formats."),
97     .init          = init,
98     .query_formats = query_formats,
99     .priv_size     = sizeof(AFormatContext),
100
101     .inputs        = (const AVFilterPad[]) {{ .name      = "default",
102                                         .type            = AVMEDIA_TYPE_AUDIO,
103                                         .filter_samples  = filter_samples},
104                                       { .name = NULL}},
105     .outputs       = (const AVFilterPad[]) {{ .name      = "default",
106                                         .type            = AVMEDIA_TYPE_AUDIO},
107                                       { .name = NULL}},
108 };