Statistics
| Branch: | Revision:

ffmpeg / libavfilter / vf_format.c @ c0bc2fed

History | View | Annotate | Download (5.39 KB)

1
/*
2
 * copyright (c) 2007 Bobby Bingham
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 libavfilter/vf_format.c
23
 * format and noformat video filters
24
 */
25

    
26
#include "libavutil/pixdesc.h"
27
#include "avfilter.h"
28

    
29
typedef struct {
30
    /**
31
     * List of flags telling if a given image format has been listed
32
     * as argument to the filter.
33
     */
34
    int listed_pix_fmt_flags[PIX_FMT_NB];
35
} FormatContext;
36

    
37
#define PIX_FMT_NAME_MAXSIZE 32
38

    
39
static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
40
{
41
    FormatContext *format = ctx->priv;
42
    const char *cur, *sep;
43
    char             pix_fmt_name[PIX_FMT_NAME_MAXSIZE];
44
    int              pix_fmt_name_len;
45
    enum PixelFormat pix_fmt;
46

    
47
    /* parse the list of formats */
48
    for (cur = args; cur; cur = sep ? sep+1 : NULL) {
49
        if (!(sep = strchr(cur, ':')))
50
            pix_fmt_name_len = strlen(cur);
51
        else
52
            pix_fmt_name_len = sep - cur;
53
        if (pix_fmt_name_len >= PIX_FMT_NAME_MAXSIZE) {
54
            av_log(ctx, AV_LOG_ERROR, "Format name too long\n");
55
            return -1;
56
        }
57

    
58
        memcpy(pix_fmt_name, cur, pix_fmt_name_len);
59
        pix_fmt_name[pix_fmt_name_len] = 0;
60
        pix_fmt = av_get_pix_fmt(pix_fmt_name);
61

    
62
        if (pix_fmt == PIX_FMT_NONE) {
63
            av_log(ctx, AV_LOG_ERROR, "Unknown pixel format: %s\n", pix_fmt_name);
64
            return -1;
65
        }
66

    
67
        format->listed_pix_fmt_flags[pix_fmt] = 1;
68
    }
69

    
70
    return 0;
71
}
72

    
73
static AVFilterFormats *make_format_list(FormatContext *format, int flag)
74
{
75
    AVFilterFormats *formats;
76
    enum PixelFormat pix_fmt;
77

    
78
    formats = av_mallocz(sizeof(AVFilterFormats));
79
    formats->formats = av_malloc(sizeof(enum PixelFormat) * PIX_FMT_NB);
80

    
81
    for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++)
82
        if (format->listed_pix_fmt_flags[pix_fmt] == flag)
83
            formats->formats[formats->format_count++] = pix_fmt;
84

    
85
    return formats;
86
}
87

    
88
static AVFilterPicRef *get_video_buffer(AVFilterLink *link, int perms,
89
                                        int w, int h)
90
{
91
    return avfilter_get_video_buffer(link->dst->outputs[0], perms, w, h);
92
}
93

    
94
static void start_frame(AVFilterLink *link, AVFilterPicRef *picref)
95
{
96
    avfilter_start_frame(link->dst->outputs[0], picref);
97
}
98

    
99
static void end_frame(AVFilterLink *link)
100
{
101
    avfilter_end_frame(link->dst->outputs[0]);
102
}
103

    
104
static void draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
105
{
106
    avfilter_draw_slice(link->dst->outputs[0], y, h, slice_dir);
107
}
108

    
109
#if CONFIG_FORMAT_FILTER
110
static int query_formats_format(AVFilterContext *ctx)
111
{
112
    avfilter_set_common_formats(ctx, make_format_list(ctx->priv, 1));
113
    return 0;
114
}
115

    
116
AVFilter avfilter_vf_format = {
117
    .name      = "format",
118
    .description = "Convert the input video to one of the specified pixel formats.",
119

    
120
    .init      = init,
121

    
122
    .query_formats = query_formats_format,
123

    
124
    .priv_size = sizeof(FormatContext),
125

    
126
    .inputs    = (AVFilterPad[]) {{ .name            = "default",
127
                                    .type            = CODEC_TYPE_VIDEO,
128
                                    .get_video_buffer= get_video_buffer,
129
                                    .start_frame     = start_frame,
130
                                    .draw_slice      = draw_slice,
131
                                    .end_frame       = end_frame, },
132
                                  { .name = NULL}},
133
    .outputs   = (AVFilterPad[]) {{ .name            = "default",
134
                                    .type            = CODEC_TYPE_VIDEO },
135
                                  { .name = NULL}},
136
};
137
#endif /* CONFIG_FORMAT_FILTER */
138

    
139
#if CONFIG_NOFORMAT_FILTER
140
static int query_formats_noformat(AVFilterContext *ctx)
141
{
142
    avfilter_set_common_formats(ctx, make_format_list(ctx->priv, 0));
143
    return 0;
144
}
145

    
146
AVFilter avfilter_vf_noformat = {
147
    .name      = "noformat",
148
    .description = "Force libavfilter not to use any of the specified pixel formats for the input to the next filter.",
149

    
150
    .init      = init,
151

    
152
    .query_formats = query_formats_noformat,
153

    
154
    .priv_size = sizeof(FormatContext),
155

    
156
    .inputs    = (AVFilterPad[]) {{ .name            = "default",
157
                                    .type            = CODEC_TYPE_VIDEO,
158
                                    .get_video_buffer= get_video_buffer,
159
                                    .start_frame     = start_frame,
160
                                    .draw_slice      = draw_slice,
161
                                    .end_frame       = end_frame, },
162
                                  { .name = NULL}},
163
    .outputs   = (AVFilterPad[]) {{ .name            = "default",
164
                                    .type            = CODEC_TYPE_VIDEO },
165
                                  { .name = NULL}},
166
};
167
#endif /* CONFIG_NOFORMAT_FILTER */
168