]> git.sesse.net Git - ffmpeg/blob - libavfilter/vsrc_buffer.c
lavfi: add avfilter_graph_dump.
[ffmpeg] / libavfilter / vsrc_buffer.c
1 /*
2  * Copyright (c) 2008 Vitor Sessak
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
23  * memory buffer source filter
24  */
25
26 #include "avfilter.h"
27 #include "internal.h"
28 #include "avcodec.h"
29 #include "buffersrc.h"
30 #include "vsrc_buffer.h"
31 #include "libavutil/imgutils.h"
32
33 typedef struct {
34     AVFilterBufferRef *picref;
35     int               h, w;
36     enum PixelFormat  pix_fmt;
37     AVRational        time_base;     ///< time_base to set in the output link
38     AVRational        sample_aspect_ratio;
39     char              sws_param[256];
40 } BufferSourceContext;
41
42 #define CHECK_PARAM_CHANGE(s, c, width, height, format)\
43     if (c->w != width || c->h != height || c->pix_fmt != format) {\
44         av_log(s, AV_LOG_ERROR, "Changing frame properties on the fly is not supported.\n");\
45         return AVERROR(EINVAL);\
46     }
47
48 int av_vsrc_buffer_add_video_buffer_ref(AVFilterContext *buffer_filter,
49                                         AVFilterBufferRef *picref, int flags)
50 {
51     BufferSourceContext *c = buffer_filter->priv;
52     AVFilterLink *outlink = buffer_filter->outputs[0];
53     int ret;
54
55     if (c->picref) {
56         if (flags & AV_VSRC_BUF_FLAG_OVERWRITE) {
57             avfilter_unref_buffer(c->picref);
58             c->picref = NULL;
59         } else {
60             av_log(buffer_filter, AV_LOG_ERROR,
61                    "Buffering several frames is not supported. "
62                    "Please consume all available frames before adding a new one.\n");
63             return AVERROR(EINVAL);
64         }
65     }
66
67     if (picref->video->w != c->w || picref->video->h != c->h || picref->format != c->pix_fmt) {
68         AVFilterContext *scale = buffer_filter->outputs[0]->dst;
69         AVFilterLink *link;
70         char scale_param[1024];
71
72         av_log(buffer_filter, AV_LOG_INFO,
73                "Buffer video input changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s\n",
74                c->w, c->h, av_pix_fmt_descriptors[c->pix_fmt].name,
75                picref->video->w, picref->video->h, av_pix_fmt_descriptors[picref->format].name);
76
77         if (!scale || strcmp(scale->filter->name, "scale")) {
78             AVFilter *f = avfilter_get_by_name("scale");
79
80             av_log(buffer_filter, AV_LOG_INFO, "Inserting scaler filter\n");
81             if ((ret = avfilter_open(&scale, f, "Input equalizer")) < 0)
82                 return ret;
83
84             snprintf(scale_param, sizeof(scale_param)-1, "%d:%d:%s", c->w, c->h, c->sws_param);
85             if ((ret = avfilter_init_filter(scale, scale_param, NULL)) < 0) {
86                 avfilter_free(scale);
87                 return ret;
88             }
89
90             if ((ret = avfilter_insert_filter(buffer_filter->outputs[0], scale, 0, 0)) < 0) {
91                 avfilter_free(scale);
92                 return ret;
93             }
94             scale->outputs[0]->time_base = scale->inputs[0]->time_base;
95
96             scale->outputs[0]->format= c->pix_fmt;
97         } else if (!strcmp(scale->filter->name, "scale")) {
98             snprintf(scale_param, sizeof(scale_param)-1, "%d:%d:%s",
99                      scale->outputs[0]->w, scale->outputs[0]->h, c->sws_param);
100             scale->filter->init(scale, scale_param, NULL);
101         }
102
103         c->pix_fmt = scale->inputs[0]->format = picref->format;
104         c->w       = scale->inputs[0]->w      = picref->video->w;
105         c->h       = scale->inputs[0]->h      = picref->video->h;
106
107         link = scale->outputs[0];
108         if ((ret =  link->srcpad->config_props(link)) < 0)
109             return ret;
110     }
111
112     c->picref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE,
113                                           picref->video->w, picref->video->h);
114     av_image_copy(c->picref->data, c->picref->linesize,
115                   (void*)picref->data, picref->linesize,
116                   picref->format, picref->video->w, picref->video->h);
117     avfilter_copy_buffer_ref_props(c->picref, picref);
118
119     return 0;
120 }
121
122 int av_buffersrc_buffer(AVFilterContext *s, AVFilterBufferRef *buf)
123 {
124     BufferSourceContext *c = s->priv;
125
126     if (c->picref) {
127         av_log(s, AV_LOG_ERROR,
128                "Buffering several frames is not supported. "
129                "Please consume all available frames before adding a new one.\n"
130             );
131         return AVERROR(EINVAL);
132     }
133
134 //     CHECK_PARAM_CHANGE(s, c, buf->video->w, buf->video->h, buf->format);
135
136     c->picref = buf;
137
138     return 0;
139 }
140
141 #if CONFIG_AVCODEC
142 #include "avcodec.h"
143
144 int av_vsrc_buffer_add_frame(AVFilterContext *buffer_src,
145                              const AVFrame *frame, int flags)
146 {
147     int ret;
148     AVFilterBufferRef *picref =
149         avfilter_get_video_buffer_ref_from_frame(frame, AV_PERM_WRITE);
150     if (!picref)
151         return AVERROR(ENOMEM);
152     ret = av_vsrc_buffer_add_video_buffer_ref(buffer_src, picref, flags);
153     picref->buf->data[0] = NULL;
154     avfilter_unref_buffer(picref);
155
156     return ret;
157 }
158 #endif
159
160 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
161 {
162     BufferSourceContext *c = ctx->priv;
163     char pix_fmt_str[128];
164     int ret, n = 0;
165     *c->sws_param = 0;
166
167     if (!args ||
168         (n = sscanf(args, "%d:%d:%127[^:]:%d:%d:%d:%d:%255c", &c->w, &c->h, pix_fmt_str,
169                     &c->time_base.num, &c->time_base.den,
170                     &c->sample_aspect_ratio.num, &c->sample_aspect_ratio.den, c->sws_param)) < 7) {
171         av_log(ctx, AV_LOG_ERROR, "Expected at least 7 arguments, but only %d found in '%s'\n", n, args);
172         return AVERROR(EINVAL);
173     }
174
175     if ((ret = ff_parse_pixel_format(&c->pix_fmt, pix_fmt_str, ctx)) < 0)
176         return ret;
177
178     av_log(ctx, AV_LOG_INFO, "w:%d h:%d pixfmt:%s tb:%d/%d sar:%d/%d sws_param:%s\n",
179            c->w, c->h, av_pix_fmt_descriptors[c->pix_fmt].name,
180            c->time_base.num, c->time_base.den,
181            c->sample_aspect_ratio.num, c->sample_aspect_ratio.den, c->sws_param);
182     return 0;
183 }
184
185 static av_cold void uninit(AVFilterContext *ctx)
186 {
187     BufferSourceContext *s = ctx->priv;
188     if (s->picref)
189         avfilter_unref_buffer(s->picref);
190     s->picref = NULL;
191 }
192
193 static int query_formats(AVFilterContext *ctx)
194 {
195     BufferSourceContext *c = ctx->priv;
196     enum PixelFormat pix_fmts[] = { c->pix_fmt, PIX_FMT_NONE };
197
198     avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
199     return 0;
200 }
201
202 static int config_props(AVFilterLink *link)
203 {
204     BufferSourceContext *c = link->src->priv;
205
206     link->w = c->w;
207     link->h = c->h;
208     link->sample_aspect_ratio = c->sample_aspect_ratio;
209     link->time_base = c->time_base;
210
211     return 0;
212 }
213
214 static int request_frame(AVFilterLink *link)
215 {
216     BufferSourceContext *c = link->src->priv;
217
218     if (!c->picref) {
219         av_log(link->src, AV_LOG_WARNING,
220                "request_frame() called with no available frame!\n");
221         return AVERROR(EINVAL);
222     }
223
224     avfilter_start_frame(link, avfilter_ref_buffer(c->picref, ~0));
225     avfilter_draw_slice(link, 0, link->h, 1);
226     avfilter_end_frame(link);
227     avfilter_unref_buffer(c->picref);
228     c->picref = NULL;
229
230     return 0;
231 }
232
233 static int poll_frame(AVFilterLink *link)
234 {
235     BufferSourceContext *c = link->src->priv;
236     return !!c->picref;
237 }
238
239 AVFilter avfilter_vsrc_buffer = {
240     .name      = "buffer",
241     .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
242     .priv_size = sizeof(BufferSourceContext),
243     .query_formats = query_formats,
244
245     .init      = init,
246     .uninit    = uninit,
247
248     .inputs    = (const AVFilterPad[]) {{ .name = NULL }},
249     .outputs   = (const AVFilterPad[]) {{ .name      = "default",
250                                     .type            = AVMEDIA_TYPE_VIDEO,
251                                     .request_frame   = request_frame,
252                                     .poll_frame      = poll_frame,
253                                     .config_props    = config_props, },
254                                   { .name = NULL}},
255 };