mp3: Properly use AVCodecContext API
[platform/upstream/libav.git] / 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 "libavutil/opt.h"
32
33 #include "avfilter.h"
34 #include "formats.h"
35 #include "internal.h"
36 #include "video.h"
37
38 typedef struct FormatContext {
39     const AVClass *class;
40     char *pix_fmts;
41
42     /**
43      * pix_fmts parsed into AVPixelFormats and terminated with
44      * AV_PIX_FMT_NONE
45      */
46     enum AVPixelFormat *formats;
47 } FormatContext;
48
49 static av_cold void uninit(AVFilterContext *ctx)
50 {
51     FormatContext *s = ctx->priv;
52     av_freep(&s->formats);
53 }
54
55 static av_cold int init(AVFilterContext *ctx)
56 {
57     FormatContext *s = ctx->priv;
58     char *cur, *sep;
59     int nb_formats = 1;
60     int i;
61
62     if (!s->pix_fmts) {
63         av_log(ctx, AV_LOG_ERROR, "Empty output format string.\n");
64         return AVERROR(EINVAL);
65     }
66
67     /* count the formats */
68     cur = s->pix_fmts;
69     while ((cur = strchr(cur, '|'))) {
70         nb_formats++;
71         if (*cur)
72             cur++;
73     }
74
75     s->formats = av_malloc_array(nb_formats + 1, sizeof(*s->formats));
76     if (!s->formats)
77         return AVERROR(ENOMEM);
78
79     /* parse the list of formats */
80     cur = s->pix_fmts;
81     for (i = 0; i < nb_formats; i++) {
82         sep = strchr(cur, '|');
83         if (sep)
84             *sep++ = 0;
85
86         s->formats[i] = av_get_pix_fmt(cur);
87         if (s->formats[i] == AV_PIX_FMT_NONE) {
88             av_log(ctx, AV_LOG_ERROR, "Unknown pixel format: %s\n", cur);
89             return AVERROR(EINVAL);
90         }
91
92         cur = sep;
93     }
94     s->formats[nb_formats] = AV_PIX_FMT_NONE;
95
96     if (!strcmp(ctx->filter->name, "noformat")) {
97         const AVPixFmtDescriptor *desc = NULL;
98         enum AVPixelFormat *formats_allowed;
99         int nb_formats_lavu = 0, nb_formats_allowed = 0;;
100
101         /* count the formats known to lavu */
102         while ((desc = av_pix_fmt_desc_next(desc)))
103             nb_formats_lavu++;
104
105         formats_allowed = av_malloc_array(nb_formats_lavu + 1, sizeof(*formats_allowed));
106         if (!formats_allowed)
107             return AVERROR(ENOMEM);
108
109         /* for each format known to lavu, check if it's in the list of
110          * forbidden formats */
111         while ((desc = av_pix_fmt_desc_next(desc))) {
112             enum AVPixelFormat pix_fmt = av_pix_fmt_desc_get_id(desc);
113
114             for (i = 0; i < nb_formats; i++) {
115                 if (s->formats[i] == pix_fmt)
116                     break;
117             }
118             if (i < nb_formats)
119                 continue;
120
121             formats_allowed[nb_formats_allowed++] = pix_fmt;
122         }
123         formats_allowed[nb_formats_allowed] = AV_PIX_FMT_NONE;
124         av_freep(&s->formats);
125         s->formats = formats_allowed;
126     }
127
128     return 0;
129 }
130
131 static int query_formats(AVFilterContext *ctx)
132 {
133     FormatContext *s = ctx->priv;
134     AVFilterFormats *formats = ff_make_format_list(s->formats);
135
136     if (!formats)
137         return AVERROR(ENOMEM);
138
139     ff_set_common_formats(ctx, formats);
140     return 0;
141 }
142
143
144 #define OFFSET(x) offsetof(FormatContext, x)
145 static const AVOption options[] = {
146     { "pix_fmts", "A '|'-separated list of pixel formats", OFFSET(pix_fmts), AV_OPT_TYPE_STRING, .flags = AV_OPT_FLAG_VIDEO_PARAM },
147     { NULL },
148 };
149
150 #if CONFIG_FORMAT_FILTER
151 static const AVClass format_class = {
152     .class_name = "format",
153     .item_name  = av_default_item_name,
154     .option     = options,
155     .version    = LIBAVUTIL_VERSION_INT,
156 };
157
158 static const AVFilterPad avfilter_vf_format_inputs[] = {
159     {
160         .name             = "default",
161         .type             = AVMEDIA_TYPE_VIDEO,
162         .get_video_buffer = ff_null_get_video_buffer,
163     },
164     { NULL }
165 };
166
167 static const AVFilterPad avfilter_vf_format_outputs[] = {
168     {
169         .name = "default",
170         .type = AVMEDIA_TYPE_VIDEO
171     },
172     { NULL }
173 };
174
175 AVFilter ff_vf_format = {
176     .name      = "format",
177     .description = NULL_IF_CONFIG_SMALL("Convert the input video to one of the specified pixel formats."),
178
179     .init      = init,
180     .uninit    = uninit,
181
182     .query_formats = query_formats,
183
184     .priv_size = sizeof(FormatContext),
185     .priv_class = &format_class,
186
187     .inputs    = avfilter_vf_format_inputs,
188     .outputs   = avfilter_vf_format_outputs,
189 };
190 #endif /* CONFIG_FORMAT_FILTER */
191
192 #if CONFIG_NOFORMAT_FILTER
193 static const AVClass noformat_class = {
194     .class_name = "noformat",
195     .item_name  = av_default_item_name,
196     .option     = options,
197     .version    = LIBAVUTIL_VERSION_INT,
198 };
199
200 static const AVFilterPad avfilter_vf_noformat_inputs[] = {
201     {
202         .name             = "default",
203         .type             = AVMEDIA_TYPE_VIDEO,
204         .get_video_buffer = ff_null_get_video_buffer,
205     },
206     { NULL }
207 };
208
209 static const AVFilterPad avfilter_vf_noformat_outputs[] = {
210     {
211         .name = "default",
212         .type = AVMEDIA_TYPE_VIDEO
213     },
214     { NULL }
215 };
216
217 AVFilter ff_vf_noformat = {
218     .name      = "noformat",
219     .description = NULL_IF_CONFIG_SMALL("Force libavfilter not to use any of the specified pixel formats for the input to the next filter."),
220
221     .init      = init,
222     .uninit    = uninit,
223
224     .query_formats = query_formats,
225
226     .priv_size = sizeof(FormatContext),
227     .priv_class = &noformat_class,
228
229     .inputs    = avfilter_vf_noformat_inputs,
230     .outputs   = avfilter_vf_noformat_outputs,
231 };
232 #endif /* CONFIG_NOFORMAT_FILTER */