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