af_aformat.c 5.84 KB
Newer Older
Anton Khirnov's avatar
Anton Khirnov committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 * Copyright (c) 2011 Mina Nagy Zaki
 *
 * This file is part of Libav.
 *
 * Libav is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * Libav is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Libav; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/**
 * @file
 * format audio filter
 */

#include "libavutil/avstring.h"
27
#include "libavutil/channel_layout.h"
28
#include "libavutil/common.h"
Anton Khirnov's avatar
Anton Khirnov committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
#include "libavutil/opt.h"

#include "audio.h"
#include "avfilter.h"
#include "formats.h"
#include "internal.h"

typedef struct AFormatContext {
    const AVClass   *class;

    AVFilterFormats *formats;
    AVFilterFormats *sample_rates;
    AVFilterChannelLayouts *channel_layouts;

    char *formats_str;
    char *sample_rates_str;
    char *channel_layouts_str;
} AFormatContext;

#define OFFSET(x) offsetof(AFormatContext, x)
#define A AV_OPT_FLAG_AUDIO_PARAM
static const AVOption options[] = {
    { "sample_fmts",     "A comma-separated list of sample formats.",  OFFSET(formats_str),         AV_OPT_TYPE_STRING, .flags = A },
    { "sample_rates",    "A comma-separated list of sample rates.",    OFFSET(sample_rates_str),    AV_OPT_TYPE_STRING, .flags = A },
    { "channel_layouts", "A comma-separated list of channel layouts.", OFFSET(channel_layouts_str), AV_OPT_TYPE_STRING, .flags = A },
    { NULL },
};

static const AVClass aformat_class = {
    .class_name = "aformat filter",
    .item_name  = av_default_item_name,
    .option     = options,
    .version    = LIBAVUTIL_VERSION_INT,
};

#define PARSE_FORMATS(str, type, list, add_to_list, get_fmt, none, desc)    \
do {                                                                        \
66 67 68 69 70 71 72 73 74
    char *next, *cur = str, sep;                                            \
                                                                            \
    if (str && strchr(str, ',')) {                                          \
        av_log(ctx, AV_LOG_WARNING, "This syntax is deprecated, use '|' to "\
               "separate %s.\n", desc);                                     \
        sep = ',';                                                          \
    } else                                                                  \
        sep = '|';                                                          \
                                                                            \
Anton Khirnov's avatar
Anton Khirnov committed
75 76
    while (cur) {                                                           \
        type fmt;                                                           \
77
        next = strchr(cur, sep);                                            \
Anton Khirnov's avatar
Anton Khirnov committed
78 79 80 81 82
        if (next)                                                           \
            *next++ = 0;                                                    \
                                                                            \
        if ((fmt = get_fmt(cur)) == none) {                                 \
            av_log(ctx, AV_LOG_ERROR, "Error parsing " desc ": %s.\n", cur);\
83
            return AVERROR(EINVAL);                                         \
Anton Khirnov's avatar
Anton Khirnov committed
84 85 86 87 88 89 90 91 92 93 94 95 96
        }                                                                   \
        add_to_list(&list, fmt);                                            \
                                                                            \
        cur = next;                                                         \
    }                                                                       \
} while (0)

static int get_sample_rate(const char *samplerate)
{
    int ret = strtol(samplerate, NULL, 0);
    return FFMAX(ret, 0);
}

97
static av_cold int init(AVFilterContext *ctx)
Anton Khirnov's avatar
Anton Khirnov committed
98 99 100 101
{
    AFormatContext *s = ctx->priv;

    PARSE_FORMATS(s->formats_str, enum AVSampleFormat, s->formats,
102 103
                  ff_add_format, av_get_sample_fmt, AV_SAMPLE_FMT_NONE, "sample format");
    PARSE_FORMATS(s->sample_rates_str, int, s->sample_rates, ff_add_format,
Anton Khirnov's avatar
Anton Khirnov committed
104 105 106 107 108
                  get_sample_rate, 0, "sample rate");
    PARSE_FORMATS(s->channel_layouts_str, uint64_t, s->channel_layouts,
                  ff_add_channel_layout, av_get_channel_layout, 0,
                  "channel layout");

109
    return 0;
Anton Khirnov's avatar
Anton Khirnov committed
110 111 112 113 114 115
}

static int query_formats(AVFilterContext *ctx)
{
    AFormatContext *s = ctx->priv;

116
    ff_set_common_formats(ctx, s->formats ? s->formats :
117
                                            ff_all_formats(AVMEDIA_TYPE_AUDIO));
Anton Khirnov's avatar
Anton Khirnov committed
118 119 120 121 122 123 124 125
    ff_set_common_samplerates(ctx, s->sample_rates ? s->sample_rates :
                                                     ff_all_samplerates());
    ff_set_common_channel_layouts(ctx, s->channel_layouts ? s->channel_layouts :
                                                            ff_all_channel_layouts());

    return 0;
}

126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
static const AVFilterPad avfilter_af_aformat_inputs[] = {
    {
        .name = "default",
        .type = AVMEDIA_TYPE_AUDIO,
    },
    { NULL }
};

static const AVFilterPad avfilter_af_aformat_outputs[] = {
    {
        .name = "default",
        .type = AVMEDIA_TYPE_AUDIO
    },
    { NULL }
};

Anton Khirnov's avatar
Anton Khirnov committed
142 143 144 145 146 147
AVFilter avfilter_af_aformat = {
    .name          = "aformat",
    .description   = NULL_IF_CONFIG_SMALL("Convert the input audio to one of the specified formats."),
    .init          = init,
    .query_formats = query_formats,
    .priv_size     = sizeof(AFormatContext),
148
    .priv_class    = &aformat_class,
Anton Khirnov's avatar
Anton Khirnov committed
149

150 151
    .inputs        = avfilter_af_aformat_inputs,
    .outputs       = avfilter_af_aformat_outputs,
Anton Khirnov's avatar
Anton Khirnov committed
152
};