]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_format.c
adpcmdec: set AVCodec.sample_fmts
[ffmpeg] / libavfilter / vf_format.c
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  * 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  * Libav 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 Libav; 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 and noformat video filters
24  */
25
26 #include <string.h>
27
28 #include "libavutil/internal.h"
29 #include "libavutil/mem.h"
30 #include "libavutil/pixdesc.h"
31 #include "avfilter.h"
32 #include "formats.h"
33 #include "internal.h"
34 #include "video.h"
35
36 typedef struct {
37     /**
38      * List of flags telling if a given image format has been listed
39      * as argument to the filter.
40      */
41     int listed_pix_fmt_flags[PIX_FMT_NB];
42 } FormatContext;
43
44 #define PIX_FMT_NAME_MAXSIZE 32
45
46 static av_cold int init(AVFilterContext *ctx, const char *args)
47 {
48     FormatContext *format = ctx->priv;
49     const char *cur, *sep;
50     char             pix_fmt_name[PIX_FMT_NAME_MAXSIZE];
51     int              pix_fmt_name_len;
52     enum PixelFormat pix_fmt;
53
54     /* parse the list of formats */
55     for (cur = args; cur; cur = sep ? sep+1 : NULL) {
56         if (!(sep = strchr(cur, ':')))
57             pix_fmt_name_len = strlen(cur);
58         else
59             pix_fmt_name_len = sep - cur;
60         if (pix_fmt_name_len >= PIX_FMT_NAME_MAXSIZE) {
61             av_log(ctx, AV_LOG_ERROR, "Format name too long\n");
62             return -1;
63         }
64
65         memcpy(pix_fmt_name, cur, pix_fmt_name_len);
66         pix_fmt_name[pix_fmt_name_len] = 0;
67         pix_fmt = av_get_pix_fmt(pix_fmt_name);
68
69         if (pix_fmt == PIX_FMT_NONE) {
70             av_log(ctx, AV_LOG_ERROR, "Unknown pixel format: %s\n", pix_fmt_name);
71             return -1;
72         }
73
74         format->listed_pix_fmt_flags[pix_fmt] = 1;
75     }
76
77     return 0;
78 }
79
80 static AVFilterFormats *make_format_list(FormatContext *format, int flag)
81 {
82     AVFilterFormats *formats;
83     enum PixelFormat pix_fmt;
84
85     formats = av_mallocz(sizeof(AVFilterFormats));
86     formats->formats = av_malloc(sizeof(enum PixelFormat) * PIX_FMT_NB);
87
88     for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++)
89         if (format->listed_pix_fmt_flags[pix_fmt] == flag)
90             formats->formats[formats->format_count++] = pix_fmt;
91
92     return formats;
93 }
94
95 #if CONFIG_FORMAT_FILTER
96 static int query_formats_format(AVFilterContext *ctx)
97 {
98     ff_set_common_formats(ctx, make_format_list(ctx->priv, 1));
99     return 0;
100 }
101
102 AVFilter avfilter_vf_format = {
103     .name      = "format",
104     .description = NULL_IF_CONFIG_SMALL("Convert the input video to one of the specified pixel formats."),
105
106     .init      = init,
107
108     .query_formats = query_formats_format,
109
110     .priv_size = sizeof(FormatContext),
111
112     .inputs    = (const AVFilterPad[]) {{ .name            = "default",
113                                           .type            = AVMEDIA_TYPE_VIDEO,
114                                           .get_video_buffer= ff_null_get_video_buffer,
115                                           .start_frame     = ff_null_start_frame,
116                                           .draw_slice      = ff_null_draw_slice,
117                                           .end_frame       = ff_null_end_frame, },
118                                         { .name = NULL}},
119     .outputs   = (const AVFilterPad[]) {{ .name            = "default",
120                                           .type            = AVMEDIA_TYPE_VIDEO },
121                                         { .name = NULL}},
122 };
123 #endif /* CONFIG_FORMAT_FILTER */
124
125 #if CONFIG_NOFORMAT_FILTER
126 static int query_formats_noformat(AVFilterContext *ctx)
127 {
128     ff_set_common_formats(ctx, make_format_list(ctx->priv, 0));
129     return 0;
130 }
131
132 AVFilter avfilter_vf_noformat = {
133     .name      = "noformat",
134     .description = NULL_IF_CONFIG_SMALL("Force libavfilter not to use any of the specified pixel formats for the input to the next filter."),
135
136     .init      = init,
137
138     .query_formats = query_formats_noformat,
139
140     .priv_size = sizeof(FormatContext),
141
142     .inputs    = (const AVFilterPad[]) {{ .name            = "default",
143                                           .type            = AVMEDIA_TYPE_VIDEO,
144                                           .get_video_buffer= ff_null_get_video_buffer,
145                                           .start_frame     = ff_null_start_frame,
146                                           .draw_slice      = ff_null_draw_slice,
147                                           .end_frame       = ff_null_end_frame, },
148                                         { .name = NULL}},
149     .outputs   = (const AVFilterPad[]) {{ .name            = "default",
150                                           .type            = AVMEDIA_TYPE_VIDEO },
151                                         { .name = NULL}},
152 };
153 #endif /* CONFIG_NOFORMAT_FILTER */