vf_format.c 5.9 KB
Newer Older
1
/*
2
 * Copyright (c) 2007 Bobby Bingham
3
 *
4
 * This file is part of Libav.
5
 *
6
 * Libav is free software; you can redistribute it and/or
7 8 9 10
 * 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.
 *
11
 * Libav is distributed in the hope that it will be useful,
12 13 14 15 16
 * 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
17
 * License along with Libav; if not, write to the Free Software
18 19 20 21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/**
22
 * @file
Stefano Sabatini's avatar
Stefano Sabatini committed
23
 * format and noformat video filters
24 25
 */

26 27 28 29
#include <string.h>

#include "libavutil/internal.h"
#include "libavutil/mem.h"
30
#include "libavutil/pixdesc.h"
31 32
#include "libavutil/opt.h"

33
#include "avfilter.h"
34
#include "formats.h"
35
#include "internal.h"
36
#include "video.h"
37

38
typedef struct FormatContext {
39 40
    const AVClass *class;
    char *pix_fmts;
41

42
    /**
43 44
     * pix_fmts parsed into AVPixelFormats and terminated with
     * AV_PIX_FMT_NONE
45
     */
46
    enum AVPixelFormat *formats;
47 48
} FormatContext;

49 50 51 52 53
static av_cold void uninit(AVFilterContext *ctx)
{
    FormatContext *s = ctx->priv;
    av_freep(&s->formats);
}
54

55
static av_cold int init(AVFilterContext *ctx)
56
{
57
    FormatContext *s = ctx->priv;
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
    char *cur, *sep;
    int nb_formats = 1;
    int i;

    /* count the formats */
    cur = s->pix_fmts;
    while ((cur = strchr(cur, '|'))) {
        nb_formats++;
        if (*cur)
            cur++;
    }

    s->formats = av_malloc_array(nb_formats + 1, sizeof(*s->formats));
    if (!s->formats)
        return AVERROR(ENOMEM);
73 74

    /* parse the list of formats */
75 76 77 78 79 80 81 82 83 84
    cur = s->pix_fmts;
    for (i = 0; i < nb_formats; i++) {
        sep = strchr(cur, '|');
        if (sep)
            *sep++ = 0;

        s->formats[i] = av_get_pix_fmt(cur);
        if (s->formats[i] == AV_PIX_FMT_NONE) {
            av_log(ctx, AV_LOG_ERROR, "Unknown pixel format: %s\n", cur);
            return AVERROR(EINVAL);
85 86
        }

87 88 89 90 91 92 93 94
        cur = sep;
    }
    s->formats[nb_formats] = AV_PIX_FMT_NONE;

    if (!strcmp(ctx->filter->name, "noformat")) {
        const AVPixFmtDescriptor *desc = NULL;
        enum AVPixelFormat *formats_allowed;
        int nb_formats_lavu = 0, nb_formats_allowed = 0;;
95

96 97 98 99 100 101 102 103 104 105 106 107
        /* count the formats known to lavu */
        while ((desc = av_pix_fmt_desc_next(desc)))
            nb_formats_lavu++;

        formats_allowed = av_malloc_array(nb_formats_lavu + 1, sizeof(*formats_allowed));
        if (!formats_allowed)
            return AVERROR(ENOMEM);

        /* for each format known to lavu, check if it's in the list of
         * forbidden formats */
        while ((desc = av_pix_fmt_desc_next(desc))) {
            enum AVPixelFormat pix_fmt = av_pix_fmt_desc_get_id(desc);
108

109 110 111 112 113 114 115 116 117 118 119 120
            for (i = 0; i < nb_formats; i++) {
                if (s->formats[i] == pix_fmt)
                    break;
            }
            if (i < nb_formats)
                continue;

            formats_allowed[nb_formats_allowed++] = pix_fmt;
        }
        formats_allowed[nb_formats_allowed] = AV_PIX_FMT_NONE;
        av_freep(&s->formats);
        s->formats = formats_allowed;
121 122 123 124 125
    }

    return 0;
}

126
static int query_formats(AVFilterContext *ctx)
127
{
128 129 130 131 132
    FormatContext *s = ctx->priv;
    AVFilterFormats *formats = ff_make_format_list(s->formats);

    if (!formats)
        return AVERROR(ENOMEM);
133

134 135
    ff_set_common_formats(ctx, formats);
    return 0;
136 137
}

138

139 140 141 142 143 144
#define OFFSET(x) offsetof(FormatContext, x)
static const AVOption options[] = {
    { "pix_fmts", "A '|'-separated list of pixel formats", OFFSET(pix_fmts), AV_OPT_TYPE_STRING, .flags = AV_OPT_FLAG_VIDEO_PARAM },
    { NULL },
};

145
#if CONFIG_FORMAT_FILTER
146 147 148 149 150 151 152
static const AVClass format_class = {
    .class_name = "format",
    .item_name  = av_default_item_name,
    .option     = options,
    .version    = LIBAVUTIL_VERSION_INT,
};

153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
static const AVFilterPad avfilter_vf_format_inputs[] = {
    {
        .name             = "default",
        .type             = AVMEDIA_TYPE_VIDEO,
        .get_video_buffer = ff_null_get_video_buffer,
    },
    { NULL }
};

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

170
AVFilter ff_vf_format = {
171
    .name      = "format",
172
    .description = NULL_IF_CONFIG_SMALL("Convert the input video to one of the specified pixel formats."),
173 174

    .init      = init,
175
    .uninit    = uninit,
176

177
    .query_formats = query_formats,
178 179

    .priv_size = sizeof(FormatContext),
180
    .priv_class = &format_class,
181

182 183
    .inputs    = avfilter_vf_format_inputs,
    .outputs   = avfilter_vf_format_outputs,
184
};
185
#endif /* CONFIG_FORMAT_FILTER */
186

187
#if CONFIG_NOFORMAT_FILTER
188 189 190 191 192 193 194
static const AVClass noformat_class = {
    .class_name = "noformat",
    .item_name  = av_default_item_name,
    .option     = options,
    .version    = LIBAVUTIL_VERSION_INT,
};

195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
static const AVFilterPad avfilter_vf_noformat_inputs[] = {
    {
        .name             = "default",
        .type             = AVMEDIA_TYPE_VIDEO,
        .get_video_buffer = ff_null_get_video_buffer,
    },
    { NULL }
};

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

212
AVFilter ff_vf_noformat = {
213
    .name      = "noformat",
214
    .description = NULL_IF_CONFIG_SMALL("Force libavfilter not to use any of the specified pixel formats for the input to the next filter."),
215 216

    .init      = init,
217
    .uninit    = uninit,
218

219
    .query_formats = query_formats,
220 221

    .priv_size = sizeof(FormatContext),
222
    .priv_class = &noformat_class,
223

224 225
    .inputs    = avfilter_vf_noformat_inputs,
    .outputs   = avfilter_vf_noformat_outputs,
226
};
227
#endif /* CONFIG_NOFORMAT_FILTER */