2 * Copyright (c) 2008 Vitor Sessak
4 * This file is part of FFmpeg.
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.
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.
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
23 * memory buffer source filter
29 #include "vsrc_buffer.h"
30 #include "libavutil/imgutils.h"
33 AVFilterBufferRef *picref;
35 enum PixelFormat pix_fmt;
36 AVRational time_base; ///< time_base to set in the output link
37 AVRational sample_aspect_ratio;
39 } BufferSourceContext;
41 int av_vsrc_buffer_add_video_buffer_ref(AVFilterContext *buffer_filter,
42 AVFilterBufferRef *picref, int flags)
44 BufferSourceContext *c = buffer_filter->priv;
45 AVFilterLink *outlink = buffer_filter->outputs[0];
49 if (flags & AV_VSRC_BUF_FLAG_OVERWRITE) {
50 avfilter_unref_buffer(c->picref);
53 av_log(buffer_filter, AV_LOG_ERROR,
54 "Buffering several frames is not supported. "
55 "Please consume all available frames before adding a new one.\n");
56 return AVERROR(EINVAL);
60 if (picref->video->w != c->w || picref->video->h != c->h || picref->format != c->pix_fmt) {
61 AVFilterContext *scale = buffer_filter->outputs[0]->dst;
63 char scale_param[1024];
65 av_log(buffer_filter, AV_LOG_INFO,
66 "Buffer video input changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s\n",
67 c->w, c->h, av_pix_fmt_descriptors[c->pix_fmt].name,
68 picref->video->w, picref->video->h, av_pix_fmt_descriptors[picref->format].name);
70 if (!scale || strcmp(scale->filter->name, "scale")) {
71 AVFilter *f = avfilter_get_by_name("scale");
73 av_log(buffer_filter, AV_LOG_INFO, "Inserting scaler filter\n");
74 if ((ret = avfilter_open(&scale, f, "Input equalizer")) < 0)
77 snprintf(scale_param, sizeof(scale_param)-1, "%d:%d:%s", c->w, c->h, c->sws_param);
78 if ((ret = avfilter_init_filter(scale, scale_param, NULL)) < 0) {
83 if ((ret = avfilter_insert_filter(buffer_filter->outputs[0], scale, 0, 0)) < 0) {
87 scale->outputs[0]->time_base = scale->inputs[0]->time_base;
89 scale->outputs[0]->format= c->pix_fmt;
90 } else if (!strcmp(scale->filter->name, "scale")) {
91 snprintf(scale_param, sizeof(scale_param)-1, "%d:%d:%s",
92 scale->outputs[0]->w, scale->outputs[0]->h, c->sws_param);
93 scale->filter->init(scale, scale_param, NULL);
96 c->pix_fmt = scale->inputs[0]->format = picref->format;
97 c->w = scale->inputs[0]->w = picref->video->w;
98 c->h = scale->inputs[0]->h = picref->video->h;
100 link = scale->outputs[0];
101 if ((ret = link->srcpad->config_props(link)) < 0)
105 c->picref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE,
106 picref->video->w, picref->video->h);
107 av_image_copy(c->picref->data, c->picref->linesize,
108 (void*)picref->data, picref->linesize,
109 picref->format, picref->video->w, picref->video->h);
110 avfilter_copy_buffer_ref_props(c->picref, picref);
118 int av_vsrc_buffer_add_frame(AVFilterContext *buffer_src,
119 const AVFrame *frame, int flags)
122 AVFilterBufferRef *picref =
123 avfilter_get_video_buffer_ref_from_frame(frame, AV_PERM_WRITE);
125 return AVERROR(ENOMEM);
126 ret = av_vsrc_buffer_add_video_buffer_ref(buffer_src, picref, flags);
127 picref->buf->data[0] = NULL;
128 avfilter_unref_buffer(picref);
134 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
136 BufferSourceContext *c = ctx->priv;
137 char pix_fmt_str[128];
142 (n = sscanf(args, "%d:%d:%127[^:]:%d:%d:%d:%d:%255c", &c->w, &c->h, pix_fmt_str,
143 &c->time_base.num, &c->time_base.den,
144 &c->sample_aspect_ratio.num, &c->sample_aspect_ratio.den, c->sws_param)) < 7) {
145 av_log(ctx, AV_LOG_ERROR, "Expected at least 7 arguments, but only %d found in '%s'\n", n, args);
146 return AVERROR(EINVAL);
149 if ((ret = ff_parse_pixel_format(&c->pix_fmt, pix_fmt_str, ctx)) < 0)
152 av_log(ctx, AV_LOG_INFO, "w:%d h:%d pixfmt:%s tb:%d/%d sar:%d/%d sws_param:%s\n",
153 c->w, c->h, av_pix_fmt_descriptors[c->pix_fmt].name,
154 c->time_base.num, c->time_base.den,
155 c->sample_aspect_ratio.num, c->sample_aspect_ratio.den, c->sws_param);
159 static int query_formats(AVFilterContext *ctx)
161 BufferSourceContext *c = ctx->priv;
162 enum PixelFormat pix_fmts[] = { c->pix_fmt, PIX_FMT_NONE };
164 avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
168 static int config_props(AVFilterLink *link)
170 BufferSourceContext *c = link->src->priv;
174 link->sample_aspect_ratio = c->sample_aspect_ratio;
175 link->time_base = c->time_base;
180 static int request_frame(AVFilterLink *link)
182 BufferSourceContext *c = link->src->priv;
185 av_log(link->src, AV_LOG_WARNING,
186 "request_frame() called with no available frame!\n");
187 return AVERROR(EINVAL);
190 avfilter_start_frame(link, avfilter_ref_buffer(c->picref, ~0));
191 avfilter_draw_slice(link, 0, link->h, 1);
192 avfilter_end_frame(link);
193 avfilter_unref_buffer(c->picref);
199 static int poll_frame(AVFilterLink *link)
201 BufferSourceContext *c = link->src->priv;
202 return !!(c->picref);
205 AVFilter avfilter_vsrc_buffer = {
207 .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
208 .priv_size = sizeof(BufferSourceContext),
209 .query_formats = query_formats,
213 .inputs = (const AVFilterPad[]) {{ .name = NULL }},
214 .outputs = (const AVFilterPad[]) {{ .name = "default",
215 .type = AVMEDIA_TYPE_VIDEO,
216 .request_frame = request_frame,
217 .poll_frame = poll_frame,
218 .config_props = config_props, },